Jump to content

Recommended Posts

Posted (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 by rich3
Posted

Maybe try:

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$objWMIService.ExecQuery("SELECT * FROM Win32_Service", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
Posted

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

Posted (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 by spudw2k
Posted (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 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
  • 1 year later...
Posted (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

:D

Edited 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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...