Jump to content

WMI query Win32_Service with AutoIT


lcty
 Share

Recommended Posts

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
 

Link to comment
Share on other sites

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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