开发者

Pass Credentials to WMI Call in VBScript

I have VBScript inside a HTA getting the ping status from a local WMI call.. I also have a function to get the last reboot time of the remote开发者_Go百科 pc..

Function GetReboot(strComputer)
    Set objWMIService = GetObject _
        ("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colOperatingSystems = objWMIService.ExecQuery _
        ("Select * from Win32_OperatingSystem")
    For Each objOS in colOperatingSystems
        dtmBootup = objOS.LastBootUpTime
        dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
        dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)
        Wscript.Echo dtmSystemUptime
    Next
    GetReboot = dtmLastbootupTime
End Function

That is roughly what it is, obviously i declare the variables and have another date function etc..

This works if I WMI call "localhost" as I am a local admin.. however when doing the WMI call to a remote server this only works if I am logged in to my local machine with an AD admin account.

Is there a way I can prompt the user for a username and password and then pass that to the WMI call so it works as anyone just with the admin details?


It looks like the SWbemLocator.ConnectServer Method allows you to specify a username and password when connecting to remote computers. It looks like the syntax that you need to use is similar, you just need to create your objWMIService object differently:

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") 
Set objWMIService = objSWbemLocator.ConnectServer("MachineName", "root\cimv2", "Username", "Password")

More documentation is available if you search for "WbemScripting.SWbemLocator" on Google


As a domain admin, you would be able to add an additional right to your account, which would allow you to start processes as any other user. This right in AD is called "Allow User to be Trusted for Delegation". It is extremely powerful. It is the only legitimate way to run your task as another user. See this link to set the right:

http://support.microsoft.com/kb/932455

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜