Jump to content

WMI Service and Remote Computer Error


Recommended Posts

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
Link to comment
Share on other sites

Maybe try:

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$objWMIService.ExecQuery("SELECT * FROM Win32_Service", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
Link to comment
Share on other sites

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

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
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

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
Link to comment
Share on other sites

  • 1 year later...

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
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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