Jump to content
Sign in to follow this  
lcty

WMI query Win32_Service with AutoIT

Recommended Posts

lcty

Hi guys,

I'm trying to query a service's status on a remote computer using the WMI.

That's what I have so far:

Func getServiceState()
    $name = "myServer"

        Dim $obj_WMIService = ObjGet("winmgmts:{impersonationLevel=impersonate,authenticationLevel=PktPrivacy}!" & $name & "rootcimv2")
    If (IsObj($Obj_WMIService)) And (Not @error) Then
        ;Dim $Col_Items = $Obj_WMIService.ExecQuery('Select * from Win32_Service where Name = "dhcp"')
    Dim $Col_Items = $Obj_WMIService.ExecQuery('Select * from Win32_Service')
    Local $Obj_Item
        For $Obj_Item In $Col_Items
            ConsoleWrite($Obj_Item.Status & @CRLF)            
        Next
    Else
        ConsoleWrite(@error)
    EndIf
EndFunc
 

(please excuse the code box didn't work for me today :/)

It works fine with workstations but with servers (especially Server 2003) I receive an Error 0.

Anyone of you familiar with that interface and knows what I'm doing wrong?

I would be very thankful for your answers!

Best reagards

lcty
 

Share this post


Link to post
Share on other sites
JLogan3o13

Hi, Icty. You have to ensure your account has privileges over WMI on the server. This works just fine for me on both workstations and servers (2k8R2):

$sServer = "WIN-U8GBUDPJ2O6"
$wbemFlagReturnImmediately = "&h10"
$wbemFlagForwardOnly = "&h20"

$WMI = ObjGet("winmgmts:\\" & $sServer & "\root\CIMV2")
$aItems = $WMI.ExecQuery("SELECT * FROM Win32_Service", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

   For $element in $aItems
      MsgBox(0, "", "Name: " & $element.Name)
 Next

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
water

Search the Examples forum for "Scriptomatic". This script generates WMI AutoIt code for you.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
trancexx

JLogan3o13 you are actually setting wbemFlagBidirectional flag for the query because you defined wrong the other two flags.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
JLogan3o13

Can you elaborate? The way I set the other two flags is how it is done in the AutoIt version of Scriptomatic, as well as the original Microsoft version, as well as the Microsoft documentation online. No problem fixing it if I am wrong, just curious why it is wrong :)

Edit: My mistake, in the AutoIt version they are set to 0x10 and 0x20 respectively, although I believe this is categorically equal to what I have.

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
trancexx

Then they are all wrong.

wbemFlagReturnImmediately is 0x10 and wbemFlagForwardOnly is 0x20.

If you define former as "&h10" and later as "&h20" then when you do:

$wbemFlagReturnImmediately + $wbemFlagForwardOnly
...you get 0. That's the value of wbemFlagBidirectional or wbemFlagReturnWhenComplete if you wish. Edited by trancexx

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
JLogan3o13

That is a lot of people wrong :)

Thanks for the input.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
trancexx

That is a lot of people wrong :)

 

Thanks for the input.

It's actually only you. :)

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
JLogan3o13

That's okay, I've been married long enough to be used to being told I'm wrong - you learn to just smile and nod and let them feel like they're right.

Edit: Last hijacking of this thread by me :)


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
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
Sign in to follow this  

×