Jump to content

Service Information


Recommended Posts

Hello,

I want a (possible short) script to display the currently running servicename, servicedescription and servicestatus.

I currently only have the servicename:

Func _RetrieveServices($s_Machine)
    Local Const $wbemFlagReturnImmediately = 0x10
    Local Const $wbemFlagForwardOnly = 0x20
    Local $colItems = "", $even = 1, $objItem, $services
    Local $objWMIService = ObjGet("winmgmts:\\" & $s_Machine & "\root\CIMV2")
    If @error Then
        MsgBox(16, "_RetrieveServices", "ObjGet Error: winmgmts")
        Return
    EndIf
    $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_Service", "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If @error Then
        MsgBox(16, "_RetrieveServices", "ExecQuery Error: SELECT * FROM Win32_Service")
        Return
    EndIf
    If IsObj($colItems) Then
        For $objItem In $colItems
            If IsArray($services) Then
                ReDim $services[UBound($services) + 1]
            Else
                Dim $services[2]
            EndIf
            $services[0] = UBound($services) - 1
            $services[UBound($services) - 1] = $objItem.Name
        Next
        Return $services
    EndIf
EndFunc

And I use it like this:

$a_services = _RetrieveServices(@ComputerName)
If Not @error And IsArray($a_services) Then
    For $x = 1 To $a_services[0]
        ConsoleWrite($a_services[$x] & @LF)
    Next
EndIf

My questions, how can I also get the status and description of the services aswell, and is it

possible to shorten the code above a little bit?

Link to comment
Share on other sites

Can't test at the moment but I think this should be possible using WMI.

Search the Example Scripts forum for "Scriptomatic". This tool generates AutoIt code for you to query WMI.

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

After startup wait until you can select a WMI class in the right dropdown (can take 1-2 minutes). Select "Win32_Service", tehn click"Run".

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

My questions, how can I also get the status and description of the services aswell, and is it

possible to shorten the code above a little bit?

Just change line

$services[UBound($services) - 1] = $objItem.Name

to

$services[UBound($services) - 1] = $objItem.Name & "|"&$objItem.Status& "|"&$objItem.description

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

I got it.

Func _ServicesGetDetails($Computer = ".")
    Local $Rtn, $aRet[1] = [0]
    Local $Service = ObjGet("winmgmts:\\" & $Computer & "\root\cimv2")
    Local $sItems = $Service.ExecQuery ("Select * from Win32_Service")
    For $objService In $sItems
        Local $i = UBound($aRet)
        ReDim $aRet[$i+1]
        ConsoleWrite($objService.Name &  ' | ' & $objService.State & ' | ' & $objService.Caption & ' | ' & $objService.ServiceType & ' | ' & $objService.PathName & @LF)
    Next
    Return $aRet
EndFunc

Link to comment
Share on other sites

#include 'Array.au3'
#RequireAdmin

Opt('MustDeclareVars', 1)

Local $aRtn = _GetRunningServices(@ComputerName)
If IsArray($aRtn) Then _ArrayDisplay($aRtn, 'Running Services')
Exit

Func _GetRunningServices($sComputerName)
    Local $objWMI = ObjGet('winmgmts:\\' & $sComputerName & '\root\cimv2')
    Local $objClass = $objWMI.InstancesOf('Win32_Service Where State="Running"')
    Local $idx = $objClass.Count
    Local $array[$idx + 1][6] = [[$idx, 'DisplayName', 'Description', 'PathName', 'ProcessId', 'Status']]
    $idx = 0
    ;
    For $objItem In $objClass
        $idx += 1
        $array[$idx][0] = $objItem.Name
        $array[$idx][1] = $objItem.DisplayName
        $array[$idx][2] = $objItem.Description
        $array[$idx][3] = $objItem.PathName
        $array[$idx][4] = $objItem.ProcessId
        $array[$idx][5] = $objItem.Status
    Next
    _ArraySort($array, 0, 1)
    Return $array
EndFunc

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

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