Sign in to follow this  
Followers 0
Scriptonize

How to query / change the state of services

8 posts in this topic

#1 ·  Posted (edited)

Hi,

I was looking for a way to use AutoIT for starting and stopping windows services.

Using the 'net start' or 'net stop' commands from the command line was an to easy solution (or using the sc command).

I found at MSoft some script samples and 'translated' them into AutoItScripts.

They were all tested on Windows XP-SP3.

I hope you find some use for them.

Sample 1:

List all services that are stopped and/or running

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.12.1
 Author:         Sriptonize

 Script Function:
    Sample how to query the state for all local installed services

#ce ----------------------------------------------------------------------------
#include <Array.au3>

Dim $MyServices
Dim $MyServicesList
Dim $arrServices[1]


;--------------------------------------Query for all stopped services---------------------
;The service state to query for...
$myServiceQuery = "Stopped"

;Object handles
$MyServices = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
$MyServicesList = $MyServices.ExecQuery("SELECT * FROM Win32_Service")

If IsObj($MyServicesList) then
    
    For $MyServices in $MyServicesList
        
        If $MyServices.State = $myServiceQuery Then
            _ArrayAdd($arrServices,$MyServices.Name & " is " & $MyServices.State)
        EndIf
    Next
EndIf

;Show all services in this state
_ArrayDisplay($arrServices)


;--------------------------Query again, but now show all running services---------------------
ReDim $arrServices[1]

;The service state to query for...
$myServiceQuery = "Running"

If IsObj($MyServicesList) then
    
    For $MyServices in $MyServicesList
        
        If $MyServices.State = $myServiceQuery Then
            _ArrayAdd($arrServices,$MyServices.Name & " is " & $MyServices.State)
        EndIf
    Next
EndIf

;Show all services in this state
_ArrayDisplay($arrServices)

Sample 2:

Stop a running service

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.12.1
 Author:         Sriptonize

 Script Function:
    Sample how to stop a local installed service

#ce ----------------------------------------------------------------------------


Dim $MyService  ;name of the service to stop
Dim $objService 
Dim $objWMIService  
Dim $MyCurrentServiceState = "Running"

$MyService = "Alerter"

$objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & @ComputerName & "\root\cimv2")

If @error = 1 Then 
    MsgBox(0,"ObjGetError","An error occurred while trying to get access to OBJ: winmgmts")
    Exit
EndIf

If Not IsObj($objWMIService) then
    MsgBox(0,"$objWMIService","$objWMIService= " & $objWMIService)
    Exit
Else
    $objService = $objWMIService.Get("Win32_Service.Name='" & $MyService & "'")
    If $objService.State = $MyCurrentServiceState Then
        $objService.StopService()
        MsgBox(0,"$objService State","$objService= " & $objService.State)
    Else
        MsgBox(0,"$objService State","$objService= " & $objService.State)
    EndIf
EndIf

Sample 3:

Start a stopped service

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.12.1
 Author:         Sriptonize

 Script Function:
    Sample how to start a local installed service

#ce ----------------------------------------------------------------------------

Dim $MyService  ;name of the service to start
Dim $objService 
Dim $objWMIService
Dim $MyCurrentServiceState = "Stopped"

$MyService = "Alerter"

;Object handle
$objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & @ComputerName & "\root\cimv2")


If Not IsObj($objWMIService) then
    MsgBox(0,"$objWMIService","$objWMIService= " & $objWMIService)
    Exit
Else
    $objService = $objWMIService.Get("Win32_Service.Name='" & $MyService & "'")
    
    If $objService.State = $MyCurrentServiceState Then
        $objService.StartService()
        MsgBox(0,"$objService State","$objService= " & $objService.State)
    Else
        MsgBox(0,"$objService State","$objService= " & $objService.State)
    EndIf
    
EndIf

Added on 11-08-2009 (dd-mm-yyyy):

Sample 4:

find a service and show some info about it

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.12.1
 Author:         Sriptonize

 Script Function:
    Sample how to search for a particular service

#ce ----------------------------------------------------------------------------

Dim $MyServices
Dim $MyServicesList

;--------------------------------------Query for the wanted service---------------------
;The service name to query for...
$myServiceQuery = "wuauserv"

;Object handles
$MyServices = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
$MyServicesList = $MyServices.ExecQuery("SELECT * FROM Win32_Service")

If IsObj($MyServicesList) then
    For $MyServices in $MyServicesList

        If $MyServices.Name = $myServiceQuery Then
            MsgBox(0,$myServiceQuery & " Service Info","Service '" & $myServiceQuery & "' was found!" & @CRLF & _
            "It's state is: " & $MyServices.State & @CRLF & _
            "It's Process-ID is: " & $MyServices.ProcessID & @CRLF & _
            "It's display-name is: " & $MyServices.DisplayName & @CRLF & _
            "It's service-type is: " & $MyServices.ServiceType & @CRLF & _
            "It's path-name is: " & $MyServices.PathName & @CRLF )
            Exit
        EndIf
    Next
EndIf

MsgBox(0,"Result of query:","Service '" & $myServiceQuery &  "' was not found on " & @ComputerName )

[[EDIT: Corrected some minor errors]]

[[EDIT 2: Added sample 4]]

Edited by Scriptonize

If you learn from It, it's not a mistake

Share this post


Link to post
Share on other sites



This is exactly what I've been looking for. However, is there any way to check if just one service is running (or not running)? Or even better, can you specify a selection of services to check?

Share this post


Link to post
Share on other sites

This is exactly what I've been looking for. However, is there any way to check if just one service is running (or not running)? Or even better, can you specify a selection of services to check?

Yes that is possible.

If you take a closer look at the last 2 examples ("stop a started service" & "start a stopped service"), you will see that it is

exactly what the samples do.

Just replace the value in "$MyService = "Alerter" for the name of the service you wish to stop or start.

Multiple services stopping or starting?

Make use of an array:

Dim $MyService  ;name of the service to stop
 Dim $objService
 Dim $objWMIService
 Dim $MyCurrentServiceState = "Running"
 Dim $arrMyServices[3]

 $arrMyServices[0] = "Alerter"
 $arrMyServices[1] = "W32Time"
 $arrMyServices[2] = "Fax"

 $objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & @ComputerName & "\root\cimv2")

 For $i = 0 to 2

  $MyService =  $arrMyServices[$i]

  If Not IsObj($objWMIService) then
    MsgBox(0,"$objWMIService","$objWMIService= " & $objWMIService)
    Exit
  Else
    $objService = $objWMIService.Get("Win32_Service.Name='" & $MyService & "'")

    If $objService.State = $MyCurrentServiceState Then
        $objService.StopService()
        MsgBox(0,"$objService State","$objService= " & $objService.State)
    Else
        MsgBox(0,"$objService State","$objService= " & $objService.State)
    EndIf
 EndIf

Next

If you learn from It, it's not a mistake

Share this post


Link to post
Share on other sites

Yes that is possible.

If you take a closer look at the last 2 examples ("stop a started service" & "start a stopped service"), you will see that it is

exactly what the samples do.

Just replace the value in "$MyService = "Alerter" for the name of the service you wish to stop or start.

Multiple services stopping or starting?

Make use of an array:

Dim $MyService  ;name of the service to stop
 Dim $objService
 Dim $objWMIService
 Dim $MyCurrentServiceState = "Running"
 Dim $arrMyServices[3]

 $arrMyServices[0] = "Alerter"
 $arrMyServices[1] = "W32Time"
 $arrMyServices[2] = "Fax"

 $objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & @ComputerName & "\root\cimv2")

 For $i = 0 to 2

  $MyService =  $arrMyServices[$i]

  If Not IsObj($objWMIService) then
    MsgBox(0,"$objWMIService","$objWMIService= " & $objWMIService)
    Exit
  Else
    $objService = $objWMIService.Get("Win32_Service.Name='" & $MyService & "'")

    If $objService.State = $MyCurrentServiceState Then
        $objService.StopService()
        MsgBox(0,"$objService State","$objService= " & $objService.State)
    Else
        MsgBox(0,"$objService State","$objService= " & $objService.State)
    EndIf
 EndIf

Next

Ah yes I didn't look at the "stop a started service" & "start a stopped service". I have it working now, thanks.

Share this post


Link to post
Share on other sites

Great script

Is there any way to check if a service exisits ?

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Great script

Is there any way to check if a service exisits ?

Also possible.

Please take a close look at my samples.

I've modified one to achieve just that.

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.12.1
 Author:         Sriptonize

 Script Function:
    Sample how to search for a particular service

#ce ----------------------------------------------------------------------------

Dim $MyServices
Dim $MyServicesList

;--------------------------------------Query for the wanted service---------------------
;The service name to query for...

$myServiceQuery = "wuauserv"

;Object handles
$MyServices = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
$MyServicesList = $MyServices.ExecQuery("SELECT * FROM Win32_Service")

If IsObj($MyServicesList) then
    For $MyServices in $MyServicesList

        If $MyServices.Name = $myServiceQuery Then
            MsgBox(0,$myServiceQuery & " Service Info","Service '" & $myServiceQuery &  "' was found and it's state is " & $MyServices.State & @CRLF & _
            "It's state is: " & $MyServices.State & @CRLF & _
            "It's Process-ID is: " & $MyServices.ProcessID & @CRLF & _
            "It's display-name is: " & $MyServices.DisplayName & @CRLF & _
            "It's service-type is: " & $MyServices.ServiceType & @CRLF & _
            "It's path-name is: " & $MyServices.PathName & @CRLF )
            Exit
        EndIf
    Next
EndIf

MsgBox(0,"Found","Service '" & $myServiceQuery &  "' was not found on " & @ComputerName )

[[EDIT: Minor Change: Wrong comment fixed ]]

Edited by Scriptonize

If you learn from It, it's not a mistake

Share this post


Link to post
Share on other sites

Thanks works a treat. >_

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