Jump to content
Sign in to follow this  
MadaraUchiha

Service Information

Recommended Posts

MadaraUchiha

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?


Share this post


Link to post
Share on other sites
water

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 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
MadaraUchiha

Hm... In which section I have to look? In The Scriptomatic Tool, I mean?


Share this post


Link to post
Share on other sites
water

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 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Exit

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

My UDFs, Abbrevs and Snippets
If you like my post, just click the like button :) 

Share this post


Link to post
Share on other sites
MadaraUchiha

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

Share this post


Link to post
Share on other sites
ripdad

#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

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  

×