Sign in to follow this  
Followers 0
Scriptonize

_WinServices UDF (My first one)

14 posts in this topic

#1 ·  Posted (edited)

Hi everybody,

Here is my first UDF.

It's an ongoing project.

Several functions will be added in the near future.

Before adding them,

I would like you all to give it a try and comment on it.

Surely there must be made some improvement to the script.

Thanks

[[EDIT]]

As GEOSoft correctly pointed me to existing code, I will not post any updates.

(I don't want to invent the wheel twice) >_<

Feel free to comment on the existing part, I'm still eager to learn.

[[/EDIT]]

With this UDF you can start, stop, pause, resume etc your windows services (by using COM).

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

 AutoIt Version            :    3.3.0.0
 Author                    :    Sriptonize
 File name                :    _WinServices.au3
 UDF Version            :    1.3
 Updated on                :    21-08-2009 (dd-mm-yyyy)

 Function(s)            :    _IsPossiblePauseService($nService)
                            _IsPossibleStopService($nService)
                            _IsServiceState($nService,$nServiceState)
                            _PauseService($nService)
                            _ServiceExists($nService)
                            _StartService($nService)
                            _StopService($nService)

 Requirements            :    WindowsXP Pro, SP-3 (other OS versions and/or service-packs may work, but havn't been tested)

 Source                    :    [url="http://msdn.microsoft.com"]http://msdn.microsoft.com[/url]

#ce ----------------------------------------------------------------------------
#include-once
Global $DebugMode = 0

Func _IsPossiblePauseService($nService)
#cs ----------------------------------------------------------------------------
 AutoIt Version            :    3.3.0.0
 Author                    :    Sriptonize
 UDF Version            :    1.3
 Updated on                :    21-08-2009 (dd-mm-yyyy)

 Function                :    Checks if a service can be paused

 Return values            :      0 (It is possible to set this service in a pause state)
                              1 (It is not possible to set this service in a pause state)
                            110 (Object error: Not an Object)

 Requirements            :    WindowsXP Pro, SP-3 (other OS versions and/or service-packs may work, but havn't been tested)

 Source                    :    [url="http://msdn.microsoft.com"]http://msdn.microsoft.com[/url]
#ce ----------------------------------------------------------------------------

    Local $objWMIService            ; Main object
    Local $objQueryCollection        ; Result of query
    Local $hObj                        ; Current selected Object in $objQueryCollection

    If $DebugMode = 1 Then ConsoleWrite(@CR)
    If $DebugMode = 1 Then ConsoleWrite("_IsPossiblePauseService($nService)" & @CR)
    If $DebugMode = 1 Then ConsoleWrite("_IsPossiblePauseService($nService)=  " & $nService & @CR)

    ;Before proceeding, first check if the service exists
    If  _ServiceExists($nService) = -1 Then
        Return 4
    EndIf


    $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    If IsObj($objWMIService) then
        If $DebugMode = 1 Then ConsoleWrite("$objWMIService is an Object" & @CR)
        $objQueryCollection = $objWMIService.ExecQuery("SELECT * FROM Win32_Service Where AcceptPause = True AND Where Name = '" & $nService & "'")
    Else
        Return 110
    EndIf

    If IsObj($objQueryCollection) then
        If $DebugMode = 1 Then ConsoleWrite("Collection returned by Query on $objWMIService)" & @CR)

        For $hObj In $objQueryCollection
            If $hObj.Name = $nService Then                     ;If we have found the service and the service can be paused, return 0
                If $DebugMode = 1 Then ConsoleWrite("Current $objWMIService.Name= " & $objWMIService.Name() & " Current $objWMIService.AcceptPause= " & $objWMIService.AcceptPause() & " Returning 0" & @CR)
                Return 0                                    ;If we have found the service and the service can be paused, return 0
            Else
                If $DebugMode = 1 Then ConsoleWrite("Current $objWMIService.Name= " & $objWMIService.Name() & " Current $objWMIService.AcceptPause= " & $objWMIService.AcceptPause() & " Returning 1" & @CR)
                Return 1
            EndIf
        Next                                                 ;$hObj In $objQueryCollection
    Else
        Return 110                                            ;Obj error
    EndIf

EndFunc

Func _IsPossibleStopService($nService)
#cs ----------------------------------------------------------------------------
 AutoIt Version            :    3.3.0.0
 Author                    :    Sriptonize
 UDF Version            :    1.3
 Updated on                :    21-08-2009 (dd-mm-yyyy)

 Function                :    Checks if a service can be stopped

 Return values            :      0 (It is possible to set this service in a stopped state)
                              1 (It is not possible to set this service in a stopped state)
                            110 (Object error: Not an Object)

 Requirements            :    WindowsXP Pro, SP-3 (other OS versions and/or service-packs may work, but havn't been tested)

 Source                    :    [url="http://msdn.microsoft.com"]http://msdn.microsoft.com[/url]
#ce ----------------------------------------------------------------------------

    Local $objWMIService            ; Main object
    Local $objQueryCollection        ; Result of query
    Local $hObj                        ; Current selected Object in $objQueryCollection

    If $DebugMode = 1 Then ConsoleWrite(@CR)
    If $DebugMode = 1 Then ConsoleWrite("_IsPossibleStopService($nService)" & @CR)
    If $DebugMode = 1 Then ConsoleWrite("_IsPossibleStopService($nService)=  " & $nService & @CR)

    ;Before proceeding, first check if the service exists
    If  _ServiceExists($nService) = -1 Then
        Return 4
    EndIf

    $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    If IsObj($objWMIService) then
        If $DebugMode = 1 Then ConsoleWrite("$objWMIService is an Object" & @CR)
        $objQueryCollection = $objWMIService.ExecQuery("SELECT * FROM Win32_Service Where AcceptStop = True AND Where Name = '" & $nService & "'")
    Else
        Return 110
    EndIf

    If IsObj($objQueryCollection) then
        If $DebugMode = 1 Then ConsoleWrite("Collection returned by Query on $objWMIService)" & @CR)

        For $hObj In $objQueryCollection
            If $hObj.Name = $nService Then
                If $DebugMode = 1 Then ConsoleWrite("Current $objWMIService.Name= " & $objWMIService.Name() & " Current $objWMIService.AcceptPause= " & $objWMIService.AcceptPause() & " Returning 0" & @CR)
                Return 0                                    ;If we have found the service and the service can be stopped, return 0
            Else
                If $DebugMode = 1 Then ConsoleWrite("Current $objWMIService.Name= " & $objWMIService.Name() & " Current $objWMIService.AcceptPause= " & $objWMIService.AcceptPause() & " Returning 1" & @CR)
                Return 1                                    ;the service can not be stopped, return 1
            EndIf
        Next                                                 ;$hObj In $objQueryCollection
    Else
        Return 110                                            ;Obj error
    EndIf

EndFunc

Func _IsServiceState($nService,$nServiceState)
#cs ----------------------------------------------------------------------------
 AutoIt Version            :    3.3.0.0
 Author                    :    Sriptonize
 UDF Version            :    1.3
 Updated on                :    21-08-2009 (dd-mm-yyyy)

 Function                :    Checks if the state of the service equals the state as provided

 Handled Parameters for $nServiceState:
                            Stopped
                            Stopping
                            Running
                            Starting
                            Paused
                            Unknown

 Return values            :      0 (Success, service state equals the provided state)
                              1 (Failure, service state does not equal the provided state)
                            110 (Object error: Not an Object)
                            200 (Invalid state provided)

 Requirements            :    WindowsXP Pro, SP-3 (other OS versions and/or service-packs may work, but havn't been tested)

 Source                    :    [url="http://msdn.microsoft.com"]http://msdn.microsoft.com[/url]
#ce ----------------------------------------------------------------------------

    Local $objWMIService            ; Main object
    Local $objQueryCollection        ; Result of query
    Local $hObj                        ; Current selected Object in $objQueryCollection

    If $DebugMode = 1 Then ConsoleWrite(@CR)
    If $DebugMode = 1 Then ConsoleWrite("_IsServiceState($nService,$nServiceState)" & @CR)
    If $DebugMode = 1 Then ConsoleWrite("_IsServiceState($nService,$nServiceState)=  " & $nService & "," & $nServiceState & @CR)

    ;Before proceeding, first check if the service exists
    If  _ServiceExists($nService) = -1 Then
        Return 4
    EndIf

    ;Check input...
    Select
        Case $nServiceState = "Stopped"

        Case $nServiceState = "Stopping"

        Case $nServiceState = "Running"

        Case $nServiceState = "Starting"

        Case $nServiceState = "Paused"

        Case $nServiceState = "Unknown"

        Case Else
            Return 200
    EndSelect

    $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    If IsObj($objWMIService) then
        If $DebugMode = 1 Then ConsoleWrite("$objWMIService is an Object" & @CR)
        $objQueryCollection = $objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name = '" & $nService & "'")
    Else
        Return 110
    EndIf

    If IsObj($objQueryCollection) then
        If $DebugMode = 1 Then ConsoleWrite("Collection returned by Query on $objWMIService)" & @CR)
        For $hObj In $objQueryCollection
            If $hObj.Name = $nService Then
                If $DebugMode = 1 Then ConsoleWrite("Current value of $hObj.Name= '" & $hObj.Name & "' Proceeding with state" & @CR)
                If $hObj.State = $nServiceState Then                                     ;If the service is in the correct state return 0
                    If $DebugMode = 1 Then ConsoleWrite("Current value of $hObj.State= '" & $hObj.State & "' Returning 0" & @CR)
                    Return 0
                Else
                    If $DebugMode = 1 Then ConsoleWrite("Current value of $hObj.State= '" & $hObj.State & "' Returning -1" & @CR)
                    Return -1
                EndIf
            EndIf

        Next ;<== $hObj In $objQueryCollection
    Else
        Return 110
    EndIf

EndFunc

Func _PauseService($nService)
#cs ----------------------------------------------------------------------------
 AutoIt Version            :    3.3.0.0
 Author                    :    Sriptonize
 UDF Version            :    1.3
 Updated on                :    21-08-2009 (dd-mm-yyyy)
 Function                :    Starts a service

 Return values            :      0 (Success)
                              1 (Not supported)
                              2 (Access denied)
                              3 (Dependend services running)
                              4 (Invalid service control)
                              5 (Service cannot accept control)
                              6 (Service not active)
                              7 (Servie request timeout)
                              8 (Unknown failure)
                              9 (path not found)
                             10 (service allready running)
                             11 (Service database locked)
                             12 (Service dependency deleted)
                             13 (Service dependency failure)
                             14 (Service disabled)
                             15 (Service logon failure)
                             16 (Service marked for deletion)
                             17 (Service no thread)
                             18 (Status circular dependency)
                             19 (Status duplicate name)
                             20 (Status invalid name)
                             21 (Status invalid parameter)
                             22 (Status invalid service account)
                             23 (Status service exists)
                             24 (service already paused)
                            100 (Object error)
                            110 (Object error: Not an Object)
#ce ----------------------------------------------------------------------------


    Local $objWMIService            ; Main object
    Local $objQueryCollection        ; Result of query
    Local $hObj                        ; Current selected Object in $objQueryCollection

    Local $hErr,$hResult,$i

    If $DebugMode = 1 Then ConsoleWrite(@CR)
    If $DebugMode = 1 Then ConsoleWrite("_PauseService($nService)" & @CR)
    If $DebugMode = 1 Then ConsoleWrite("_PauseService($nService)=  " & $nService & @CR)

    ;Before proceeding, first check if the service exists
    If _ServiceExists($nService) = -1 Then
        Return 4
    EndIf

    ;If service does exists start the service
    $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    If IsObj($objWMIService) Then
        If $DebugMode = 1 Then ConsoleWrite("$objWMIService is an Object" & @CR)
            $objQueryCollection = $objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name = '" & $nService & "'")

        If IsObj($objQueryCollection) Then
            If $DebugMode = 1 Then ConsoleWrite("Collection returned by Query on $objWMIService)" & @CR)

            ;Try to pause the service
            For $hObj In $objQueryCollection
                $hResult = $hObj.PauseService()
                $hErr = @error
                If $DebugMode = 1 Then ConsoleWrite("$hObj.PauseService()= " & $hResult & @CR)
                If $DebugMode = 1 Then ConsoleWrite("@error= " & $hErr & @CR)
                ;Give the service some time to pause...
                For $i = 1 to 10
                    Sleep(100)
                Next
                Return $hErr
            Next
        Else
            Return 100
        EndIf
    Else
        Return 110
    EndIf

EndFunc

Func _ResumeService($nService)
#cs ----------------------------------------------------------------------------
 AutoIt Version            :    3.3.0.0
 Author                    :    Sriptonize
 UDF Version            :    1.3
 Updated on                :    21-08-2009 (dd-mm-yyyy)
 Function                :    Starts a service

 Return values            :      0 (Success)
                              1 (Not supported)
                              2 (Access denied)
                              3 (Dependend services running)
                              4 (Invalid service control)
                              5 (Service cannot accept control)
                              6 (Service not active)
                              7 (Servie request timeout)
                              8 (Unknown failure)
                              9 (path not found)
                             10 (service allready running)
                             11 (Service database locked)
                             12 (Service dependency deleted)
                             13 (Service dependency failure)
                             14 (Service disabled)
                             15 (Service logon failure)
                             16 (Service marked for deletion)
                             17 (Service no thread)
                             18 (Status circular dependency)
                             19 (Status duplicate name)
                             20 (Status invalid name)
                             21 (Status invalid parameter)
                             22 (Status invalid service account)
                             23 (Status service exists)
                             24 (service already paused)
                            100 (Object error)
                            110 (Object error: Not an Object)
#ce ----------------------------------------------------------------------------


    Local $objWMIService            ; Main object
    Local $objQueryCollection        ; Result of query
    Local $hObj                        ; Current selected Object in $objQueryCollection

    Local $hErr,$hResult,$i

    If $DebugMode = 1 Then ConsoleWrite(@CR)
    If $DebugMode = 1 Then ConsoleWrite("_ResumeService($nService)" & @CR)
    If $DebugMode = 1 Then ConsoleWrite("_ResumeService($nService)=  " & $nService & @CR)

    ;Before proceeding, first check if the service exists
    If _ServiceExists($nService) = -1 Then
        Return 4
    EndIf

    ;If service does exists start the service
    $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    If IsObj($objWMIService) Then
        If $DebugMode = 1 Then ConsoleWrite("$objWMIService is an Object" & @CR)
            $objQueryCollection = $objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name = '" & $nService & "'")

        If IsObj($objQueryCollection) Then
            If $DebugMode = 1 Then ConsoleWrite("Collection returned by Query on $objWMIService)" & @CR)

            ;Try to resume the service
            For $hObj In $objQueryCollection
                $hResult = $hObj.ResumeService()
                $hErr = @error
                If $DebugMode = 1 Then ConsoleWrite("$hObj.ResumeService()= " & $hResult & @CR)
                If $DebugMode = 1 Then ConsoleWrite("@error= " & $hErr & @CR)
                ;Give the service some time to resume...
                For $i = 1 to 10
                    Sleep(100)
                Next
                Return $hErr
            Next
        Else
            Return 100
        EndIf
    Else
        Return 110
    EndIf

EndFunc

Func _ServiceExists($nService)
#cs ----------------------------------------------------------------------------
 AutoIt Version            :    3.3.0.0
 Author                    :    Sriptonize
 UDF Version            :    1.3
 Updated on                :    21-08-2009 (dd-mm-yyyy)

 Function                :    Checks if a service exists

 Return values            :      0 (Success, service was found)
                             -1 (Failure, service was not found)
                            110 (Object error: Not an Object)

 Requirements            :    WindowsXP Pro, SP-3 (other OS versions and/or service-packs may work, but havn't been tested)

 Source                    :    [url="http://msdn.microsoft.com"]http://msdn.microsoft.com[/url]
#ce ----------------------------------------------------------------------------

    Local $objWMIService            ; Main object
    Local $objQueryCollection        ; Result of query
    Local $hObj                        ; Current selected Object in $objQueryCollection

    If $DebugMode = 1 Then ConsoleWrite(@CR)
    If $DebugMode = 1 Then ConsoleWrite("_ServiceExists($nService)" & @CR)
    If $DebugMode = 1 Then ConsoleWrite("_ServiceExists($nService)=  " & $nService & @CR)

    $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")

    If IsObj($objWMIService) Then
        If $DebugMode = 1 Then ConsoleWrite("$objWMIService is an Object" & @CR)
        $objQueryCollection = $objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name = '" & $nService & "'")
    Else
        Return 110
    EndIf

    If IsObj($objQueryCollection) then
        If $DebugMode = 1 Then ConsoleWrite("Collection returned by Query on $objWMIService)" & @CR)

        For $hObj In $objQueryCollection
            If $hObj.Name = $nService Then Return 0        ;Service found, return 0
        Next                                             ;<== $objWMIService In $hObj
    Else
        Return 110                                        ;Obj error
    EndIf


    Return -1                                                ;Service not found, return -1

EndFunc

Func _StartService($nService)
#cs ----------------------------------------------------------------------------
 AutoIt Version            :    3.3.0.0
 Author                    :    Sriptonize
 UDF Version            :    1.3
 Updated on                :    21-08-2009 (dd-mm-yyyy)
 Function                :    Starts a service

 Return values            :      0 (Success)
                              1 (Not supported)
                              2 (Access denied)
                              3 (Dependend services running)
                              4 (Invalid service control)
                              5 (Service cannot accept control)
                              6 (Service not active)
                              7 (Servie request timeout)
                              8 (Unknown failure)
                              9 (path not found)
                             10 (service allready running)
                             11 (Service database locked)
                             12 (Service dependency deleted)
                             13 (Service dependency failure)
                             14 (Service disabled)
                             15 (Service logon failure)
                             16 (Service marked for deletion)
                             17 (Service no thread)
                             18 (Status circular dependency)
                             19 (Status duplicate name)
                             20 (Status invalid name)
                             21 (Status invalid parameter)
                             22 (Status invalid service account)
                             23 (Status service exists)
                             24 (service already paused)
                            100 (Object error)
                            110 (Object error: Not an Object)
#ce ----------------------------------------------------------------------------


    Local $objWMIService            ; Main object
    Local $objQueryCollection        ; Result of query
    Local $hObj                        ; Current selected Object in $objQueryCollection

    Local $hErr,$hResult,$i

    If $DebugMode = 1 Then ConsoleWrite(@CR)
    If $DebugMode = 1 Then ConsoleWrite("_StartService($nService)" & @CR)
    If $DebugMode = 1 Then ConsoleWrite("_StartService($nService)=  " & $nService & @CR)

    ;Before proceeding, first check if the service exists
    If _ServiceExists($nService) = -1 Then
        Return 4
    EndIf

    ;If service does exists start the service
    $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    If IsObj($objWMIService) Then
        If $DebugMode = 1 Then ConsoleWrite("$objWMIService is an Object" & @CR)
            $objQueryCollection = $objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name = '" & $nService & "'")

        If IsObj($objQueryCollection) Then
            If $DebugMode = 1 Then ConsoleWrite("Collection returned by Query on $objWMIService)" & @CR)

            ;Try to start the service
            For $hObj In $objQueryCollection
                $hResult = $hObj.StartService()
                $hErr = @error
                If $DebugMode = 1 Then ConsoleWrite("$hObj.StartService()= " & $hResult & @CR)
                If $DebugMode = 1 Then ConsoleWrite("@error= " & $hErr & @CR)
                ;Give the service some time to start up...
                For $i = 1 to 10
                    Sleep(100)
                Next
                Return $hErr
            Next
        Else
            Return 100
        EndIf
    Else
        Return 110
    EndIf

EndFunc

Func _StopService($nService)
#cs ----------------------------------------------------------------------------
 AutoIt Version            :    3.3.0.0
 Author                    :    Sriptonize
 UDF Version            :    1.3
 Updated on                :    21-08-2009 (dd-mm-yyyy)
 Function                :    Stops a service

 Return values            :      0 (Success)
                              1 (Not supported)
                              2 (Access denied)
                              3 (Dependend services running)
                              4 (Invalid service control)
                              5 (Service cannot accept control)
                              6 (Service not active)
                              7 (Servie request timeout)
                              8 (Unknown failure)
                              9 (path not found)
                             10 (service allready running)
                             11 (Service database locked)
                             12 (Service dependency deleted)
                             13 (Service dependency failure)
                             14 (Service disabled)
                             15 (Service logon failure)
                             16 (Service marked for deletion)
                             17 (Service no thread)
                             18 (Status circular dependency)
                             19 (Status duplicate name)
                             20 (Status invalid name)
                             21 (Status invalid parameter)
                             22 (Status invalid service account)
                             23 (Status service exists)
                             24 (service already paused)
                            100 (Object error)
                            110 (Object error: Not an Object)
#ce ----------------------------------------------------------------------------


    Local $objWMIService            ; Main object
    Local $objQueryCollection        ; Result of query
    Local $hObj                        ; Current selected Object in $objQueryCollection

    Local $hErr,$hResult,$i

    If $DebugMode = 1 Then ConsoleWrite(@CR)
    If $DebugMode = 1 Then ConsoleWrite("_StopService($nService)" & @CR)
    If $DebugMode = 1 Then ConsoleWrite("_StopService($nService)=  " & $nService & @CR)

    ;Before proceeding, first check if the service exists
    If _ServiceExists($nService) = -1 Then
        Return 4
    EndIf

    ;If service does exists stop the service
    $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    If IsObj($objWMIService) Then
        If $DebugMode = 1 Then ConsoleWrite("$objWMIService is an Object" & @CR)
            $objQueryCollection = $objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name = '" & $nService & "'")

        If IsObj($objQueryCollection) Then
            If $DebugMode = 1 Then ConsoleWrite("Collection returned by Query on $objWMIService)" & @CR)

            ;Try to start the service
            For $hObj In $objQueryCollection
                $hResult = $hObj.StopService()
                $hErr = @error
                If $DebugMode = 1 Then ConsoleWrite("$hObj.StopService()= " & $hResult & @CR)
                If $DebugMode = 1 Then ConsoleWrite("@error= " & $hErr & @CR)
                ;Give the service some time to stop...
                For $i = 1 to 10
                    Sleep(100)
                Next
                Return $hErr
            Next
        Else
            Return 100
        EndIf
    Else
        Return 110
    EndIf

EndFunc

Here are some examples how to use them.

#include <_WinServices.au3>
Local $myService
$DebugMode = 0

$myService = "JavaQuickStarterService"

;Check if this service exists
ConsoleWrite("Does " & $myService & " exist? Result: " & _ServiceExists($myService) & @CR)

;Start $myService
ConsoleWrite($myService & " wil be started. Result: " & _StartService($myService) & @CR)

;Check if $myService is Running... 0 = yes, -1 = no
ConsoleWrite("Is " & $myService & " running? Result: " & _IsServiceState($myService,"Running") & @CR)
Sleep(10000)

;Is it possible to pause $myService 0 = yes, -1 = no
ConsoleWrite("Can " & $myService & " being paused? Result: " &_IsPossiblePauseService($myService) & @CR)

;Pauses $myService
ConsoleWrite($myService & " wil be paused. Result: " & _PauseService($myService) & @CR)

;Check if $myService is Paused... 0 = yes, -1 = no
ConsoleWrite("Is " & $myService & " paused?  Result: " & _IsServiceState($myService,"Paused") & @CR)
Sleep(10000)

;Resumes $myService
ConsoleWrite($myService & " wil be resumed. Result: " & _ResumeService($myService) & @CR)

;Check if $myService is Running... 0 = yes, -1 = no
ConsoleWrite("Is " & $myService & " running? Result: " & _IsServiceState($myService,"Running") & @CR)
Sleep(10000)

;Is it possible to stop $myService 0 = yes, -1 = no
ConsoleWrite("Can " & $myService & " being stopped? Result: " &_IsPossibleStopService($myService) & @CR)
Sleep(10000)

;Stop $myService
ConsoleWrite($myService & " wil be stopped. Result: " & _StopService($myService) & @CR)

;Check if $myService is Stopped... 0 = yes, -1 = no
ConsoleWrite("Is " & $myService & " stopped?  Result: " & _IsServiceState($myService,"Stopped") & @CR)
Sleep(10000)
Edited by Scriptonize

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

Share this post


Link to post
Share on other sites



84 Reads, 15 downloads, no comment.

Hmm, I guess that everybody is busy testing the code >_<

Meanwhile, I discovered this weekend that there is a template for UDF's.

In my next version, I will try to follow the Directions for Submitting Standard UDFs


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

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

84 Reads, 15 downloads, no comment.

Hmm, I guess that everybody is busy testing the code :(

G'day Scriptonize

Good job!

I don't have any direct use for it right now. BUT when I get some time I'd like make up a script to set the services to their "optimum" condition (ie automatic/manual/disabled). Come to think of it your script/udf in it's current stage would be useful for games (ie stop/pause all non essential services while playing and start them again when the game is over. Who needs windows updates when there are aliens to kill. >_<

How about adding a function(s) to handle "startup type" automatic/manual/disabled would be useful.

Thanks for sharing!

Edited by storme

Share this post


Link to post
Share on other sites

84 Reads, 15 downloads, no comment.

Hmm, I guess that everybody is busy testing the code >_<

Possibly because of this?

Search Results

The difference between yours and mine compared to the others?

We are using WMI do do the work and WMI is an often disabled service so if it's not running, the code won't work. The others are using DLL calls which is a better method.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Possibly because of this?

Search Results

The difference between yours and mine compared to the others?

We are using WMI do do the work and WMI is an often disabled service so if it's not running, the code won't work. The others are using DLL calls which is a better method.

It's such a shame that WMI is so unreliable as it could be such a great tool. >_<

Anyway GEOSoft, what is your suggestion as to the best (DLL) based services control script?

Thanks

Share this post


Link to post
Share on other sites

It's such a shame that WMI is so unreliable as it could be such a great tool. >_<

Anyway GEOSoft, what is your suggestion as to the best (DLL) based services control script?

Thanks

It is a great tool and I have nothing against using it for some tasks. Starting and stopping a sevice isn't one of them since you can't use WMI to start the WMI service.

This one is probably the more complete of the two. You could use it to start the WMI service if it's stopped and then do whatever you want with WMI.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

@GEOsoft and @Storme.

I agree using API is a safer approach since the outcome will be more reliable for the reason mentioned.

Also, WMI is a set of very powerful tools (too bad that it is not that often used).

However, I use the WMI approach for 3 reasons:

  • The API approach has already been done here.
  • I'm using the script(s) on our test servers (W2K3 with WMI up and running) and will deploy them in the production environment as soon asI'm satisfied with the results.
  • For learning purposes, thus improving my skills on AutoIT.
Anyway, thanks for the feedback, I appreciate it. Edited by Scriptonize

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

Share this post


Link to post
Share on other sites

The WMI approach has been done too. >_<

My website (in my sig) >> Code >> My UDF's >> Services. But like you said, it's good practice. If you plan on using a lot o WMI then you might want to take a look at the compiled version of AutoIt Scriptomatic. That's also in my signature.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

The WMI approach has been done too. >_<

My website (in my sig) >> Code >> My UDF's >> Services. But like you said, it's good practice. If you plan on using a lot o WMI then you might want to take a look at the compiled version of AutoIt Scriptomatic. That's also in my signature.

G'day GEOSoft

You mentioned before about using the services (API version) to activate WMI if it's deactivated.

As I have no idea where to start is there any chance you could whip up a quick script that could be added to any script that uses WMI to ensure it's running.

EG

_initWMI() - Return value - true (all is well) False (WMI not working)

It could be added to any script needing WMI to ensure they work...

What do you think? Worthwhile or worthless?

??? If you don't have the time let me know (PM) and I'll have a go myself. Sounds like an interesting project.

Thanks

Share this post


Link to post
Share on other sites

The WMI approach has been done too. >_<

My website (in my sig) >> Code >> My UDF's >> Services. But like you said, it's good practice. If you plan on using a lot o WMI then you might want to take a look at the compiled version of AutoIt Scriptomatic. That's also in my signature.

My bad, I missed that completely.

Well, I guess I write the UDF for personal use then. :(

I'm gonna take a look at your website.


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

Share this post


Link to post
Share on other sites

G'day GEOSoft

You mentioned before about using the services (API version) to activate WMI if it's deactivated.

As I have no idea where to start is there any chance you could whip up a quick script that could be added to any script that uses WMI to ensure it's running.

EG

_initWMI() - Return value - true (all is well) False (WMI not working)

It could be added to any script needing WMI to ensure they work...

What do you think? Worthwhile or worthless?

??? If you don't have the time let me know (PM) and I'll have a go myself. Sounds like an interesting project.

Thanks

As you may see in services.au3 (API approach), there is a function named "_Service_Running"

You could use this one to check if WMI is running, if not then use the function "_Service_Start" from the same UDF, to start up WMI


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

Share this post


Link to post
Share on other sites

As you may see in services.au3 (API approach), there is a function named "_Service_Running"

You could use this one to check if WMI is running, if not then use the function "_Service_Start" from the same UDF, to start up WMI

I may have a "play" with that tonight. I'm thinking a single function with all the code in it (no includes) so that it can just be added to any WMI project.

I was also thinking about "other things" that may stop WMI from working. Any idea of what/how to check for these and what/how to fix them.

Thanks!

Share this post


Link to post
Share on other sites

I don't have the time at the moment but here's a hint for you. From the commandline

Net Start "Windows Management Instrumentation


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Hi everybody,

Here is my first UDF.

It's an ongoing project.

Hello -

in the afternoon I modified an existing software from Run cmd / send net stop ... with include your soft. I look a service is running or not, stop it, modify settinge by RegWrite and start it again - seems to be working fine, thanks!

A small problem was, I have to look for the real service-name, the language-depending names from Net start / Net stop in the cmd-window are not working.

First tests I made with XP-SP2-German, now in Win_2000-German because it has to work on it.

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