rich3 Posted May 29, 2008 Share Posted May 29, 2008 (edited) I'm working on a script that will query services on local and remote computers that will report back if the service is down, but I am encountering an error when I try to check the services on any remote computer. Here's my code: #include <GUIConstants.au3> ;Reads server names from the .ini file $svrs=IniReadSectionNames("C:\Program Files\AutoIt3\Examples\Services.ini") ;Determines the number of servers that are being worked with For $svr = 1 To $svrs[0] ;Server Name $svcss=$svrs[$svr] ;Reads the section and value of the .ini file $svcs=IniReadSection("C:\Program Files\AutoIt3\Examples\Services.ini",$svcss) For $svc = 1 To $svcs[0][0] $objWMIService = ObjGet("winmgmts:" _ & "{impersonationLevel=Impersonate}!\\" _ & $svcss & "\root\cimv2") $colServiceList = $objWMIService.ExecQuery _ ("Select * from Win32_Service") For $objItem in $colServiceList Select Case $objItem.name = $svcs[$svc][0] Select Case $objItem.State="Stopped" Msgbox (0,"Error", "Service Name: " & $objItem.Name & " on " &$svrs[$svr]& " is "& $objItem.State) Case $objItem.State="Restarting" Msgbox (0,"Error", "Service Name: " & $objItem.Name & " on " &$svrs[$svr]& " is "& $objItem.State) Case Else EndSelect EndSelect Next Next Next Exit Here's the .ini file, Services.ini [LOCALPC] winvnc=winvnc.exe Spooler=spoolsv.exe [REMOTEPC1] winvnc=winvnc.exe Spooler=spoolsv.exe [REMOTEPC2] winvnc=winvnc.exe Spooler=spoolsv.exe Here's the error message I get when I try accessing a remote site. Any local service query works fine. C:\Program Files\AutoIt3\Examples\Services.au3 (14) : ==> Variable must be of type "Object".: $colServiceList = $objWMIService.ExecQuery ("Select * from Win32_Service") $colServiceList = $objWMIService^ ERROR Any help would be appreciated. Edited May 29, 2008 by rich3 Link to comment Share on other sites More sharing options...
weaponx Posted May 29, 2008 Share Posted May 29, 2008 Maybe try: $wbemFlagReturnImmediately = 0x10 $wbemFlagForwardOnly = 0x20 $objWMIService.ExecQuery("SELECT * FROM Win32_Service", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly) Link to comment Share on other sites More sharing options...
rich3 Posted May 29, 2008 Author Share Posted May 29, 2008 I'm getting the same error message. C:\Program Files\AutoIt3\Examples\Services.au3 (16) : ==> Variable must be of type "Object".: $colServiceList = $objWMIService.ExecQuery("SELECT * FROM Win32_Service", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly) $colServiceList = $objWMIService^ ERROR Link to comment Share on other sites More sharing options...
spudw2k Posted May 29, 2008 Share Posted May 29, 2008 (edited) That typically means that the connection was not established. Make sure you can reach $svcss=$svrs[$svr] first, and make sure you have admin rights on that box. edit: Variable must be of type "Object".: means the follwing code failed. $objWMIService = ObjGet("winmgmts:" _ & "{impersonationLevel=Impersonate}!\\" _ & $svcss & "\root\cimv2") Try this too. (one liner) $objWMIService = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\" & $svcss & "\root\cimv2") Edited May 29, 2008 by spudw2k Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX Builder Misc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retrieve SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose Array Projects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalc Cool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF  Link to comment Share on other sites More sharing options...
PsaltyDS Posted May 29, 2008 Share Posted May 29, 2008 (edited) I'm getting the same error message. C:\Program Files\AutoIt3\Examples\Services.au3 (16) : ==> Variable must be of type "Object".: $colServiceList = $objWMIService.ExecQuery("SELECT * FROM Win32_Service", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly) $colServiceList = $objWMIService^ ERROR Put some error handling into your script: Global $sWMIService, $objWMIService, $colServiceList, $svcss Global $wbemFlagReturnImmediately = 0x10 Global $wbemFlagForwardOnly = 0x20 Global $wbemFlags = $wbemFlagReturnImmediately + $wbemFlagForwardOnly ; INI query stuff sets $svcss $sWMIService = "winmgmts:{impersonationLevel=Impersonate}!\\" & $svcss & "\root\cimv2" $objWMIService = ObjGet($sWMIService) If IsObj($objWMIService) Then $colServiceList = $objWMIService.ExecQuery("SELECT * FROM Win32_Service", "WQL", $wbemFlags) ; etc. Else MsgBox(16, "Error", "Failed to connect to WMI at: " & $sWMIService) EndIf Edited May 29, 2008 by PsaltyDS Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
Tigerweld Posted June 23, 2009 Share Posted June 23, 2009 How can I use something like this to query just one service, locally? Thanks! Link to comment Share on other sites More sharing options...
PsaltyDS Posted June 23, 2009 Share Posted June 23, 2009 (edited) How can I use something like this to query just one service, locally? Thanks! Just update the object path to the local computer, and update the WQL query to find the service you wanted (i.e. Eventlog): Global $sWMIService, $objWMIService, $colServiceList Global $wbemFlagReturnImmediately = 0x10 Global $wbemFlagForwardOnly = 0x20 Global $wbemFlags = $wbemFlagReturnImmediately + $wbemFlagForwardOnly $sWMIService = "winmgmts:\\" & @ComputerName & "\root\cimv2" $objWMIService = ObjGet($sWMIService) If IsObj($objWMIService) Then $colServiceList = $objWMIService.ExecQuery('SELECT * FROM Win32_Service WHERE Name = "EventLog"', 'WQL', $wbemFlags) For $oSvc In $colServiceList ConsoleWrite("Debug: Name = " & $oSvc.name & ", Status = " & $oSvc.Status & @LF) Next Else MsgBox(16, "Error", "Failed to connect to WMI at: " & $sWMIService) EndIf Edited June 23, 2009 by PsaltyDS Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now