Dienste starten und beenden

  • Oki, danker erstmal. Allerdings: habe ich was übersehen? Meine geänderte Variante (habe den Dienst nur ausgetauscht) funktioniert nämlich nicht. Können deaktivierte Dienste auch gestartet werden, wenn nicht, kann man sie per AutoIT aktivieren?

    Spoiler anzeigen
    [autoit]

    Global $STANDARD_RIGHTS_REQUIRED = 0x000F0000
    Global $SC_MANAGER_CONNECT = 0x0001
    Global $SC_MANAGER_CREATE_SERVICE = 0x0002
    Global $SC_MANAGER_ENUMERATE_SERVICE = 0x0004
    Global $SC_MANAGER_LOCK = 0x0008
    Global $SC_MANAGER_QUERY_LOCK_STATUS = 0x0010
    Global $SC_MANAGER_MODIFY_BOOT_CONFIG = 0x0020

    [/autoit] [autoit][/autoit] [autoit]

    Global $SC_MANAGER_ALL_ACCESS = BitOR($STANDARD_RIGHTS_REQUIRED, _
    $SC_MANAGER_CONNECT, _
    $SC_MANAGER_CREATE_SERVICE, _
    $SC_MANAGER_ENUMERATE_SERVICE, _
    $SC_MANAGER_LOCK, _
    $SC_MANAGER_QUERY_LOCK_STATUS, _
    $SC_MANAGER_MODIFY_BOOT_CONFIG)

    Global $SERVICE_QUERY_CONFIG = 0x0001
    Global $SERVICE_CHANGE_CONFIG = 0x0002
    Global $SERVICE_QUERY_STATUS = 0x0004
    Global $SERVICE_ENUMERATE_DEPENDENTS = 0x0008
    Global $SERVICE_START = 0x0010
    Global $SERVICE_STOP = 0x0020
    Global $SERVICE_PAUSE_CONTINUE = 0x0040
    Global $SERVICE_INTERROGATE = 0x0080
    Global $SERVICE_USER_DEFINED_CONTROL = 0x0100

    [/autoit] [autoit][/autoit] [autoit]

    Global $SERVICE_ALL_ACCESS = BitOR($STANDARD_RIGHTS_REQUIRED, _
    $SERVICE_QUERY_CONFIG, _
    $SERVICE_CHANGE_CONFIG, _
    $SERVICE_QUERY_STATUS, _
    $SERVICE_ENUMERATE_DEPENDENTS, _
    $SERVICE_START, _
    $SERVICE_STOP, _
    $SERVICE_PAUSE_CONTINUE, _
    $SERVICE_INTERROGATE, _
    $SERVICE_USER_DEFINED_CONTROL)

    [/autoit] [autoit][/autoit] [autoit]

    Global $SERVICE_CONTROL_STOP = 0x00000001
    Global $SERVICE_CONTROL_INTERROGATE = 0x00000004

    [/autoit] [autoit][/autoit] [autoit]

    ;#comments-start***
    ;Example - stop print spooler service
    If _ServiceRunning("Remote-Registrierung") Then
    _StopService("Remote-Registrierung")
    If @error Then
    MsgBox(64, "", "Failed to stop Spooler service")
    Else
    MsgBox(64, "", "Spooler service stopped")
    EndIf
    Else
    _StartService("Remote-Registrierung")
    If @error Then
    MsgBox(64, "", "Failed to start Spooler service")
    Else
    MsgBox(64, "", "Spooler service started")
    EndIf
    EndIf
    #comments-end***

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ;
    ; Description: Starts a service
    ; Syntax: _StartService($sServiceName)
    ; Parameter(s): $sServiceName - Name of service to start
    ; Requirement(s): None
    ; Return Value(s): On Success - Sets @error = 0
    ; On Failure - Sets:
    ; @error = 1056: Already running
    ; @error = 1060: Service does not exist
    ; Author(s): SumTingWong
    ; Documented by: noone
    ;
    ;===============================================================================

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _StartService($sServiceName)
    Local $arRet
    Local $hSC
    Local $hService
    Local $lError = -1

    [/autoit] [autoit][/autoit] [autoit]

    $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _
    "str", "", _
    "str", "ServicesActive", _
    "long", $SC_MANAGER_CONNECT)
    If $arRet[0] = 0 Then
    $arRet = DllCall("kernel32.dll", "long", "GetLastError")
    $lError = $arRet[0]
    Else
    $hSC = $arRet[0]
    $arRet = DllCall("advapi32.dll", "long", "OpenService", _
    "long", $hSC, _
    "str", $sServiceName, _
    "long", $SERVICE_START)
    If $arRet[0] = 0 Then
    $arRet = DllCall("kernel32.dll", "long", "GetLastError")
    $lError = $arRet[0]
    Else
    $hService = $arRet[0]
    $arRet = DllCall("advapi32.dll", "int", "StartService", _
    "long", $hService, _
    "long", 0, _
    "str", "")
    If $arRet[0] = 0 Then
    $arRet = DllCall("kernel32.dll", "long", "GetLastError")
    $lError = $arRet[0]
    EndIf
    DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)
    EndIf
    DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
    EndIf
    If $lError <> -1 Then SetError($lError)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ;
    ; Description: Stops a service
    ; Syntax: _StopService($sServiceName)
    ; Parameter(s): $sServiceName - Name of service to stop
    ; Requirement(s): None
    ; Return Value(s): On Success - Sets:
    ; @error = 0
    ; On Failure - Sets:
    ; @error = 1062: Already stopped
    ; @error = 1060: Service does not exist
    ; Author(s): SumTingWong
    ; Documented by: noone
    ;
    ;===============================================================================
    Func _StopService($sServiceName)
    Local $arRet
    Local $hSC
    Local $hService
    Local $lError = -1

    [/autoit] [autoit][/autoit] [autoit]

    $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _
    "str", "", _
    "str", "ServicesActive", _
    "long", $SC_MANAGER_CONNECT)
    If $arRet[0] = 0 Then
    $arRet = DllCall("kernel32.dll", "long", "GetLastError")
    $lError = $arRet[0]
    Else
    $hSC = $arRet[0]
    $arRet = DllCall("advapi32.dll", "long", "OpenService", _
    "long", $hSC, _
    "str", $sServiceName, _
    "long", $SERVICE_STOP)
    If $arRet[0] = 0 Then
    $arRet = DllCall("kernel32.dll", "long", "GetLastError")
    $lError = $arRet[0]
    Else
    $hService = $arRet[0]
    $arRet = DllCall("advapi32.dll", "int", "ControlService", _
    "long", $hService, _
    "long", $SERVICE_CONTROL_STOP, _
    "str", "")
    If $arRet[0] = 0 Then
    $arRet = DllCall("kernel32.dll", "long", "GetLastError")
    $lError = $arRet[0]
    EndIf
    DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)
    EndIf
    DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
    EndIf
    If $lError <> -1 Then SetError($lError)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ;
    ; Description: Checks to see if a service is installed
    ; Syntax: _ServiceExists($sServiceName)
    ; Parameter(s): $sServiceName - Name of service to check
    ; Requirement(s): None
    ; Return Value(s): On Success - Returns 1
    ; On Failure - Returns 0
    ; Author(s): SumTingWong
    ; Documented by: noone
    ;
    ;===============================================================================
    Func _ServiceExists($sServiceName)
    Local $arRet
    Local $hSC
    Local $bExist = 0

    $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _
    "str", "", _
    "str", "ServicesActive", _
    "long", $SC_MANAGER_CONNECT)
    If $arRet[0] <> 0 Then
    $hSC = $arRet[0]
    $arRet = DllCall("advapi32.dll", "long", "OpenService", _
    "long", $hSC, _
    "str", $sServiceName, _
    "long", $SERVICE_INTERROGATE)
    If $arRet[0] <> 0 Then
    $bExist = 1
    DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $arRet[0])
    EndIf
    DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
    EndIf
    Return $bExist
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ;
    ; Description: Checks to see if a service is running
    ; Syntax: _ServiceRunning($sServiceName)
    ; Parameter(s): $sServiceName - Name of service to check
    ; Requirement(s): None
    ; Return Value(s): On Success - Returns 1
    ; On Failure - Returns 0
    ; Author(s): SumTingWong
    ; Documented by: noone
    ;
    ;===============================================================================
    Func _ServiceRunning($sServiceName)
    Local $arRet
    Local $hSC
    Local $hService
    Local $bRunning = 0

    [/autoit] [autoit][/autoit] [autoit]

    $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _
    "str", "", _
    "str", "ServicesActive", _
    "long", $SC_MANAGER_CONNECT)
    If $arRet[0] <> 0 Then
    $hSC = $arRet[0]
    $arRet = DllCall("advapi32.dll", "long", "OpenService", _
    "long", $hSC, _
    "str", $sServiceName, _
    "long", $SERVICE_INTERROGATE)
    If $arRet[0] <> 0 Then
    $hService = $arRet[0]
    $arRet = DllCall("advapi32.dll", "int", "ControlService", _
    "long", $hService, _
    "long", $SERVICE_CONTROL_INTERROGATE, _
    "str", "")
    $bRunning = $arRet[0]
    DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)
    EndIf
    DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
    EndIf
    Return $bRunning
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ; Description: Delete a Windows Service
    ; Syntax: _ServDelete($iName[, $Computer])
    ; Parameter(s): $iName - The name of the service to delete
    ; $Computer - The network name of the computer (optional) The local computer is default
    ; Requirement(s): None
    ; Return Value(s): Success - Deletes the service
    ; Failure Sets @Error = -1 if service is not found
    ; Author(s) GEOSoft
    ; Modification(s):
    ; Note(s):
    ; Example(s):
    ;===============================================================================

    [/autoit] [autoit][/autoit] [autoit]

    Func _ServDelete($iName, $Computer = ".")
    $Service = ObjGet("winmgmts:\\" & $Computer & "\root\cimv2")
    $sItems = $Service.ExecQuery("Select * from Win32_Service")
    For $objService in $sItems
    If $objService.Name == $iName Then
    $objService.StopService($objService.Name)
    $objService.Delete($objService.Name)
    Return
    EndIf
    Next
    Return SetError(-1)
    EndFunc ;<==> _ServDelete()

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ; Description: Return the details of a Windows Service
    ; Syntax: _ServGetDetails($iName[, $Computer])
    ; Parameter(s): $iName - The name of the service to check
    ; $Computer - The network name of the computer (optional) The local computer is default
    ; Requirement(s): None
    ; Return Value(s): Success - Returns an array of the service details where element (-1 = Yes, 0 = No)
    ; [1] = Computer Network Name
    ; [2] = Service Name
    ; [3] = Service Type (Own Process, Share Process)
    ; [4] = Service State (Stopped, Running, Paused)
    ; [5] = Exit Code (0, 1077)
    ; [6] = Process ID
    ; [7] = Can Be Paused (-1, 0)
    ; [8] = Can Be Stopped (-1, 0)
    ; [9] = Caption
    ; [10] = Description
    ; [11] = Can Interact With Desktop (-1, 0)
    ; [12] = Display Name
    ; [13] = Error Control (Normal, Ignore)
    ; [14] = Executable Path Name
    ; [15] = Service Started (-1, 0)
    ; [16] = Start Mode (Auto, Manual, Disabled)
    ; [17] = Account Name (LocalSystem, NT AUTHORITY\LocalService, NT AUTHORITY\NetworkService)
    ; Failure Sets @Error = -1 if service not found
    ; Author(s) GEOSoft
    ; Modification(s):
    ; Note(s):
    ; Example(s): $Var = _ServGetDetails("ATI Smart")
    ; $Dtl = "System Name|Name|Type|State|ExitCode|Process ID|Can Pause|Can Stop|Caption|Description|"
    ; $Dtl = StringSplit($Dtl & "Interact With DskTop|Display Name|Error Control|Exec File Path|Started|Start Mode|Account", '|')
    ; For $I = 1 To $Var[0]
    ; MsgBox(4096,$Dtl[$I], $Var[$I])
    ; Next
    ;===============================================================================

    [/autoit] [autoit][/autoit] [autoit]

    Func _ServGetDetails($iName, $Computer = ".")
    Local $Rtn = ''
    $Service = ObjGet("winmgmts:\\" & $Computer & "\root\cimv2")
    $sItems = $Service.ExecQuery("Select * from Win32_Service")
    For $objService in $sItems
    If $objService.Name == $iName Then
    $Rtn &= $objService.SystemName & '|' & $objService.Name & '|' & $objService.ServiceType & '|' & $objService.State & '|'
    $Rtn &= $objService.ExitCode & '|' & $objService.ProcessID & '|' & $objService.AcceptPause & '|' & $objService.AcceptStop & '|'
    $Rtn &= $objService.Caption & '|' & $objService.Description & '|' & $objService.DesktopInteract & '|' & $objService.DisplayName & '|'
    $Rtn &= $objService.ErrorControl & '|' & $objService.PathName & '|' &$objService.Started & '|' & $objService.StartMode & '|'
    $Rtn &= $objService.StartName
    Return StringSplit($Rtn, '|')
    EndIf
    Next
    Return SetError(-1)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ; Description: Return the current state of a Windows Service
    ; Syntax: _ServGetState($iName[, $Computer])
    ; Parameter(s): $iName - The name of the service to check
    ; $Computer - The network name of the computer (optional) The local computer is default
    ; Requirement(s): None
    ; Return Value(s): Success - Returns the state of the service
    ; Failure Sets @Error = -1 if service not found
    ; Author(s) GEOSoft
    ; Modification(s):
    ; Note(s):
    ; Example(s):
    ;===============================================================================

    [/autoit] [autoit][/autoit] [autoit]

    Func _ServGetState($iName, $Computer = ".")
    $Service = ObjGet("winmgmts:\\" & $Computer & "\root\cimv2")
    $sItems = $Service.ExecQuery("Select * from Win32_Service")
    For $objItem in $sItems
    If $objItem.Name == $iName Then Return $objItem.State
    Next
    Return SetError(-1)
    EndFunc ;<==> _ServGetState()

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ; Description: List the currently installed services
    ; Syntax: _ServListInstalled([,$Computer])
    ; Parameter(s): $Computer - The network name of the computer (optional) The local computer is default
    ; Requirement(s): None
    ; Return Value(s): Success - Returns the state of the service
    ; Failure Sets @Error = -1 if service not found
    ; Author(s) GEOSoft
    ; Modification(s):
    ; Note(s):
    ; Example(s):
    ;===============================================================================

    [/autoit] [autoit][/autoit] [autoit]

    Func _ServListInstalled($Computer = ".")
    Local $Rtn = ''
    $Service = ObjGet("winmgmts:\\" & $Computer & "\root\cimv2")
    $sItems = $Service.ExecQuery("Select * from Win32_Service")
    For $objService in $sItems
    $Rtn &= $objService.Name & '|'
    Next
    Return StringSplit(StringTrimRight($Rtn, 1), '|')
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ; Description: Pause a Windows Service
    ; Syntax: _ServPause($iName[, $Computer])
    ; Parameter(s): $iName - The name of the service to start
    ; $Computer - The network name of the computer (optional). The local computer is default
    ; Requirement(s): None
    ; Return Value(s): Success - Pauses the service
    ; Failure Sets @Error = -1 if service not found or service is already paused
    ; Author(s) GEOSoft
    ; Modification(s):
    ; Note(s):
    ; Example(s):
    ;===============================================================================

    [/autoit] [autoit][/autoit] [autoit]

    Func _ServPause($iName, $Computer = ".")
    $Service = ObjGet("winmgmts:\\" & $Computer & "\root\cimv2")
    $sItems = $Service.ExecQuery("Select * from Win32_Service Where State = 'Running' ")
    For $objService in $sItems
    If $objService.Name == $iName Then
    $objService.PauseService($objService.Name)
    Return
    EndIf
    Next
    Return SetError(-1)
    EndFunc ;<==> _ServPause()

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ; Description: Resumes a previously paused Windows auto-start service
    ; Syntax: _ServResume($iName[, $Computer])
    ; Parameter(s): $iName - The name of the service to start
    ; $Computer - The network name of the computer (optional). The local computer is default
    ; Requirement(s): None
    ; Return Value(s): Success - Resumes the service
    ; Failure Sets @Error = -1 if service not found
    ; Author(s) GEOSoft
    ; Modification(s):
    ; Note(s):
    ; Example(s):
    ;===============================================================================

    [/autoit] [autoit][/autoit] [autoit]

    Func _ServResume($iName, $Computer = ".")
    $Service = ObjGet("winmgmts:\\" & $Computer & "\root\cimv2")
    $sItems = $Service.ExecQuery("Select * from Win32_Service Where State = 'Paused' and StartMode = 'Auto'")
    For $objService in $sItems
    If $objService.Name == $iName Then
    $objService.ResumeService($objService.Name)
    Return
    EndIf
    Next
    Return SetError(-1)
    EndFunc ;<==> _ServResume()

    [/autoit]
  • Man könnte auch WMI benutzen, um die Dienste zu kontrollieren. Hat noch den Vorteil, dass man auch an Remote Systemen die Dienste stoppen / starten kann.

    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

  • Man könnte auch WMI benutzen, um die Dienste zu kontrollieren. Hat noch den Vorteil, dass man auch an Remote Systemen die Dienste stoppen / starten kann.

    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

  • Der Finger was nicht nervös, sondern die DB des Forums mochte mich zu dieser Zeit nicht :P

    Hier ein WMI Beispiel:

    Spoiler anzeigen
    [autoit]


    ;Coded by UEZ 2009
    #AutoIt3Wrapper_Change2CUI=y
    #AutoIt3Wrapper_UseUpx=n

    [/autoit] [autoit][/autoit] [autoit]

    $oMyError = ObjEvent("AutoIt.Error", "oMyError") ; Install a custom error handler

    [/autoit] [autoit][/autoit] [autoit]

    Global $ip = "localhost"
    If $CmdLine[0] > 0 Then $ip = $CmdLine[1]

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

    $stop_srv = "RemoteRegistry"
    If Stop_Service($ip, $stop_srv) Then
    MsgBox(0, "Stop Service", "Service " & $stop_srv & " stopped properly on " & $ip)
    Else
    MsgBox(16, "Error", "Error")
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    Func Stop_Service($srv, $service)
    Local $colItems, $colItem, $ping
    $ping = Ping($srv)
    If $ping Then
    $colItems = $objWMIService.ExecQuery("Select * From Win32_Service Where Name='" & $service & "'")
    If IsObj($colItems) Then
    For $objItem In $colItems
    $objItem.StopService()
    Next
    Return SetError(0, 0, 1)
    Else
    Return SetError(1, 0, 0)
    EndIf
    Else
    Return SetError(2, 0, 0)
    EndIf
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func oMyError()
    Msgbox(0,"AutoItCOM Test","We intercepted a COM Error !" & @CRLF & @CRLF & _
    "err.description is: " & @TAB & $oMyError.Description & @CRLF & _
    "err.windescription:" & @TAB & $oMyError.WinDescription & @CRLF & _
    "err.number is: " & @TAB & Hex($oMyError.Number, 8) & @CRLF & _
    "err.lastdllerror is: " & @TAB & $oMyError.LastDllError & @CRLF & _
    "err.scriptline is: " & @TAB & $oMyError.Scriptline & @CRLF & _
    "err.source is: " & @TAB & $oMyError.Source & @CRLF & _
    "err.helpfile is: " & @TAB & $oMyError.Helpfile & @CRLF & _
    "err.helpcontext is: " & @TAB & $oMyError.HelpContext _
    )
    EndFunc

    [/autoit]

    Hier wir der Dienst "RemoteRegistry" gestoppt. Die Feinheiten überlasse ich dir.

    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

    • Offizieller Beitrag

    Mit ein paar Änderungen funktionieren auch die DLL-Aufrufe auf Remotesystemen!

    Spoiler anzeigen
    [autoit]

    Global $STANDARD_RIGHTS_REQUIRED = 0x000F0000
    Global $SC_MANAGER_CONNECT = 0x0001
    Global $SC_MANAGER_CREATE_SERVICE = 0x0002
    Global $SC_MANAGER_ENUMERATE_SERVICE = 0x0004
    Global $SC_MANAGER_LOCK = 0x0008
    Global $SC_MANAGER_QUERY_LOCK_STATUS = 0x0010
    Global $SC_MANAGER_MODIFY_BOOT_CONFIG = 0x0020

    [/autoit] [autoit][/autoit] [autoit]

    Global $SC_MANAGER_ALL_ACCESS = BitOR($STANDARD_RIGHTS_REQUIRED, _
    $SC_MANAGER_CONNECT, _
    $SC_MANAGER_CREATE_SERVICE, _
    $SC_MANAGER_ENUMERATE_SERVICE, _
    $SC_MANAGER_LOCK, _
    $SC_MANAGER_QUERY_LOCK_STATUS, _
    $SC_MANAGER_MODIFY_BOOT_CONFIG)

    [/autoit] [autoit][/autoit] [autoit]

    Global $SERVICE_QUERY_CONFIG = 0x0001
    Global $SERVICE_CHANGE_CONFIG = 0x0002
    Global $SERVICE_QUERY_STATUS = 0x0004
    Global $SERVICE_ENUMERATE_DEPENDENTS = 0x0008
    Global $SERVICE_START = 0x0010
    Global $SERVICE_STOP = 0x0020
    Global $SERVICE_PAUSE_CONTINUE = 0x0040
    Global $SERVICE_INTERROGATE = 0x0080
    Global $SERVICE_USER_DEFINED_CONTROL = 0x0100

    [/autoit] [autoit][/autoit] [autoit]

    Global $SERVICE_ALL_ACCESS = BitOR($STANDARD_RIGHTS_REQUIRED, _
    $SERVICE_QUERY_CONFIG, _
    $SERVICE_CHANGE_CONFIG, _
    $SERVICE_QUERY_STATUS, _
    $SERVICE_ENUMERATE_DEPENDENTS, _
    $SERVICE_START, _
    $SERVICE_STOP, _
    $SERVICE_PAUSE_CONTINUE, _
    $SERVICE_INTERROGATE, _
    $SERVICE_USER_DEFINED_CONTROL)

    [/autoit] [autoit][/autoit] [autoit]

    Global $SERVICE_CONTROL_STOP = 0x00000001
    Global $SERVICE_CONTROL_INTERROGATE = 0x00000004

    [/autoit] [autoit][/autoit] [autoit]

    ;#comments-start
    $RemotePC = "PCNAME"
    ;Example - stop print spooler service
    If _ServiceRunning("Remote-Registrierung", $RemotePC) Then
    _StopService("Remote-Registrierung", $RemotePC)
    If @error Then
    MsgBox(64, "", "Failed to stop Spooler service")
    Else
    MsgBox(64, "", "Spooler service stopped")
    EndIf
    Else
    _StartService("Remote-Registrierung", $RemotePC)
    If @error Then
    MsgBox(64, "", "Failed to start Spooler service")
    Else
    MsgBox(64, "", "Spooler service started")
    EndIf
    EndIf
    ;#comments-end

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ;
    ; Description: Starts a service
    ; Syntax: _StartService($sServiceName)
    ; Parameter(s): $sServiceName - Name of service to start
    ; Requirement(s): None
    ; Return Value(s): On Success - Sets @error = 0
    ; On Failure - Sets:
    ; @error = 1056: Already running
    ; @error = 1060: Service does not exist
    ; Author(s): SumTingWong
    ; Documented by: noone
    ;
    ;===============================================================================
    Func _StartService($sServiceName, $Computer = ".")
    Local $arRet
    Local $hSC
    Local $hService
    Local $lError = -1

    [/autoit] [autoit][/autoit] [autoit]

    $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _
    "str", $Computer, _
    "str", "ServicesActive", _
    "long", $SC_MANAGER_CONNECT)
    If $arRet[0] = 0 Then
    $arRet = DllCall("kernel32.dll", "long", "GetLastError")
    $lError = $arRet[0]
    Else
    $hSC = $arRet[0]
    $arRet = DllCall("advapi32.dll", "long", "OpenService", _
    "long", $hSC, _
    "str", $sServiceName, _
    "long", $SERVICE_START)
    If $arRet[0] = 0 Then
    $arRet = DllCall("kernel32.dll", "long", "GetLastError")
    $lError = $arRet[0]
    Else
    $hService = $arRet[0]
    $arRet = DllCall("advapi32.dll", "int", "StartService", _
    "long", $hService, _
    "long", 0, _
    "str", "")
    If $arRet[0] = 0 Then
    $arRet = DllCall("kernel32.dll", "long", "GetLastError")
    $lError = $arRet[0]
    EndIf
    DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)
    EndIf
    DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
    EndIf
    If $lError <> -1 Then SetError($lError)
    EndFunc ;==>_StartService

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ;
    ; Description: Stops a service
    ; Syntax: _StopService($sServiceName)
    ; Parameter(s): $sServiceName - Name of service to stop
    ; Requirement(s): None
    ; Return Value(s): On Success - Sets:
    ; @error = 0
    ; On Failure - Sets:
    ; @error = 1062: Already stopped
    ; @error = 1060: Service does not exist
    ; Author(s): SumTingWong
    ; Documented by: noone
    ;
    ;===============================================================================
    Func _StopService($sServiceName, $Computer = ".")
    Local $arRet
    Local $hSC
    Local $hService
    Local $lError = -1

    [/autoit] [autoit][/autoit] [autoit]

    $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _
    "str", $Computer, _
    "str", "ServicesActive", _
    "long", $SC_MANAGER_CONNECT)
    If $arRet[0] = 0 Then
    $arRet = DllCall("kernel32.dll", "long", "GetLastError")
    $lError = $arRet[0]
    Else
    $hSC = $arRet[0]
    $arRet = DllCall("advapi32.dll", "long", "OpenService", _
    "long", $hSC, _
    "str", $sServiceName, _
    "long", $SERVICE_STOP)
    If $arRet[0] = 0 Then
    $arRet = DllCall("kernel32.dll", "long", "GetLastError")
    $lError = $arRet[0]
    Else
    $hService = $arRet[0]
    $arRet = DllCall("advapi32.dll", "int", "ControlService", _
    "long", $hService, _
    "long", $SERVICE_CONTROL_STOP, _
    "str", "")
    If $arRet[0] = 0 Then
    $arRet = DllCall("kernel32.dll", "long", "GetLastError")
    $lError = $arRet[0]
    EndIf
    DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)
    EndIf
    DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
    EndIf
    If $lError <> -1 Then SetError($lError)
    EndFunc ;==>_StopService

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ;
    ; Description: Checks to see if a service is installed
    ; Syntax: _ServiceExists($sServiceName)
    ; Parameter(s): $sServiceName - Name of service to check
    ; Requirement(s): None
    ; Return Value(s): On Success - Returns 1
    ; On Failure - Returns 0
    ; Author(s): SumTingWong
    ; Documented by: noone
    ;
    ;===============================================================================
    Func _ServiceExists($sServiceName, $Computer = ".")
    Local $arRet
    Local $hSC
    Local $bExist = 0

    [/autoit] [autoit][/autoit] [autoit]

    $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _
    "str", $Computer, _
    "str", "ServicesActive", _
    "long", $SC_MANAGER_CONNECT)
    If $arRet[0] <> 0 Then
    $hSC = $arRet[0]
    $arRet = DllCall("advapi32.dll", "long", "OpenService", _
    "long", $hSC, _
    "str", $sServiceName, _
    "long", $SERVICE_INTERROGATE)
    If $arRet[0] <> 0 Then
    $bExist = 1
    DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $arRet[0])
    EndIf
    DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
    EndIf
    Return $bExist
    EndFunc ;==>_ServiceExists

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ;
    ; Description: Checks to see if a service is running
    ; Syntax: _ServiceRunning($sServiceName)
    ; Parameter(s): $sServiceName - Name of service to check
    ; Requirement(s): None
    ; Return Value(s): On Success - Returns 1
    ; On Failure - Returns 0
    ; Author(s): SumTingWong
    ; Documented by: noone
    ;
    ;===============================================================================
    Func _ServiceRunning($sServiceName, $Computer = ".")
    Local $arRet
    Local $hSC
    Local $hService
    Local $bRunning = 0

    [/autoit] [autoit][/autoit] [autoit]

    $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", _
    "str", $Computer, _
    "str", "ServicesActive", _
    "long", $SC_MANAGER_CONNECT)
    If $arRet[0] <> 0 Then
    $hSC = $arRet[0]
    $arRet = DllCall("advapi32.dll", "long", "OpenService", _
    "long", $hSC, _
    "str", $sServiceName, _
    "long", $SERVICE_INTERROGATE)
    If $arRet[0] <> 0 Then
    $hService = $arRet[0]
    $arRet = DllCall("advapi32.dll", "int", "ControlService", _
    "long", $hService, _
    "long", $SERVICE_CONTROL_INTERROGATE, _
    "str", "")
    $bRunning = $arRet[0]
    DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)
    EndIf
    DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
    EndIf
    Return $bRunning
    EndFunc ;==>_ServiceRunning

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ; Description: Delete a Windows Service
    ; Syntax: _ServDelete($iName[, $Computer])
    ; Parameter(s): $iName - The name of the service to delete
    ; $Computer - The network name of the computer (optional) The local computer is default
    ; Requirement(s): None
    ; Return Value(s): Success - Deletes the service
    ; Failure Sets @Error = -1 if service is not found
    ; Author(s) GEOSoft
    ; Modification(s):
    ; Note(s):
    ; Example(s):
    ;===============================================================================
    Func _ServDelete($iName, $Computer = ".")
    $Service = ObjGet("winmgmts:\\" & $Computer & "\root\cimv2")
    $sItems = $Service.ExecQuery("Select * from Win32_Service")
    For $objService In $sItems
    If $objService.Name == $iName Then
    $objService.StopService($objService.Name)
    $objService.Delete($objService.Name)
    Return
    EndIf
    Next
    Return SetError(-1)
    EndFunc ;==>_ServDelete

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ; Description: Return the details of a Windows Service
    ; Syntax: _ServGetDetails($iName[, $Computer])
    ; Parameter(s): $iName - The name of the service to check
    ; $Computer - The network name of the computer (optional) The local computer is default
    ; Requirement(s): None
    ; Return Value(s): Success - Returns an array of the service details where element (-1 = Yes, 0 = No)
    ; [1] = Computer Network Name
    ; [2] = Service Name
    ; [3] = Service Type (Own Process, Share Process)
    ; [4] = Service State (Stopped, Running, Paused)
    ; [5] = Exit Code (0, 1077)
    ; [6] = Process ID
    ; [7] = Can Be Paused (-1, 0)
    ; [8] = Can Be Stopped (-1, 0)
    ; [9] = Caption
    ; [10] = Description
    ; [11] = Can Interact With Desktop (-1, 0)
    ; [12] = Display Name
    ; [13] = Error Control (Normal, Ignore)
    ; [14] = Executable Path Name
    ; [15] = Service Started (-1, 0)
    ; [16] = Start Mode (Auto, Manual, Disabled)
    ; [17] = Account Name (LocalSystem, NT AUTHORITY\LocalService, NT AUTHORITY\NetworkService)
    ; Failure Sets @Error = -1 if service not found
    ; Author(s) GEOSoft
    ; Modification(s):
    ; Note(s):
    ; Example(s): $Var = _ServGetDetails("ATI Smart")
    ; $Dtl = "System Name|Name|Type|State|ExitCode|Process ID|Can Pause|Can Stop|Caption|Description|"
    ; $Dtl = StringSplit($Dtl & "Interact With DskTop|Display Name|Error Control|Exec File Path|Started|Start Mode|Account", '|')
    ; For $I = 1 To $Var[0]
    ; MsgBox(4096,$Dtl[$I], $Var[$I])
    ; Next
    ;===============================================================================
    Func _ServGetDetails($iName, $Computer = ".")
    Local $Rtn = ''
    $Service = ObjGet("winmgmts:\\" & $Computer & "\root\cimv2")
    $sItems = $Service.ExecQuery("Select * from Win32_Service")
    For $objService In $sItems
    If $objService.Name == $iName Then
    $Rtn &= $objService.SystemName & '|' & $objService.Name & '|' & $objService.ServiceType & '|' & $objService.State & '|'
    $Rtn &= $objService.ExitCode & '|' & $objService.ProcessID & '|' & $objService.AcceptPause & '|' & $objService.AcceptStop & '|'
    $Rtn &= $objService.Caption & '|' & $objService.Description & '|' & $objService.DesktopInteract & '|' & $objService.DisplayName & '|'
    $Rtn &= $objService.ErrorControl & '|' & $objService.PathName & '|' & $objService.Started & '|' & $objService.StartMode & '|'
    $Rtn &= $objService.StartName
    Return StringSplit($Rtn, '|')
    EndIf
    Next
    Return SetError(-1)
    EndFunc ;==>_ServGetDetails

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ; Description: Return the current state of a Windows Service
    ; Syntax: _ServGetState($iName[, $Computer])
    ; Parameter(s): $iName - The name of the service to check
    ; $Computer - The network name of the computer (optional) The local computer is default
    ; Requirement(s): None
    ; Return Value(s): Success - Returns the state of the service
    ; Failure Sets @Error = -1 if service not found
    ; Author(s) GEOSoft
    ; Modification(s):
    ; Note(s):
    ; Example(s):
    ;===============================================================================
    Func _ServGetState($iName, $Computer = ".")
    $Service = ObjGet("winmgmts:\\" & $Computer & "\root\cimv2")
    $sItems = $Service.ExecQuery("Select * from Win32_Service")
    For $objItem In $sItems
    If $objItem.Name == $iName Then Return $objItem.State
    Next
    Return SetError(-1)
    EndFunc ;==>_ServGetState

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ; Description: List the currently installed services
    ; Syntax: _ServListInstalled([,$Computer])
    ; Parameter(s): $Computer - The network name of the computer (optional) The local computer is default
    ; Requirement(s): None
    ; Return Value(s): Success - Returns the state of the service
    ; Failure Sets @Error = -1 if service not found
    ; Author(s) GEOSoft
    ; Modification(s):
    ; Note(s):
    ; Example(s):
    ;===============================================================================
    Func _ServListInstalled($Computer = ".")
    Local $Rtn = ''
    $Service = ObjGet("winmgmts:\\" & $Computer & "\root\cimv2")
    $sItems = $Service.ExecQuery("Select * from Win32_Service")
    For $objService In $sItems
    $Rtn &= $objService.Name & '|'
    Next
    Return StringSplit(StringTrimRight($Rtn, 1), '|')
    EndFunc ;==>_ServListInstalled

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ; Description: Pause a Windows Service
    ; Syntax: _ServPause($iName[, $Computer])
    ; Parameter(s): $iName - The name of the service to start
    ; $Computer - The network name of the computer (optional). The local computer is default
    ; Requirement(s): None
    ; Return Value(s): Success - Pauses the service
    ; Failure Sets @Error = -1 if service not found or service is already paused
    ; Author(s) GEOSoft
    ; Modification(s):
    ; Note(s):
    ; Example(s):
    ;===============================================================================
    Func _ServPause($iName, $Computer = ".")
    $Service = ObjGet("winmgmts:\\" & $Computer & "\root\cimv2")
    $sItems = $Service.ExecQuery("Select * from Win32_Service Where State = 'Running' ")
    For $objService In $sItems
    If $objService.Name == $iName Then
    $objService.PauseService($objService.Name)
    Return
    EndIf
    Next
    Return SetError(-1)
    EndFunc ;==>_ServPause

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ; Description: Resumes a previously paused Windows auto-start service
    ; Syntax: _ServResume($iName[, $Computer])
    ; Parameter(s): $iName - The name of the service to start
    ; $Computer - The network name of the computer (optional). The local computer is default
    ; Requirement(s): None
    ; Return Value(s): Success - Resumes the service
    ; Failure Sets @Error = -1 if service not found
    ; Author(s) GEOSoft
    ; Modification(s):
    ; Note(s):
    ; Example(s):
    ;===============================================================================
    Func _ServResume($iName, $Computer = ".")
    $Service = ObjGet("winmgmts:\\" & $Computer & "\root\cimv2")
    $sItems = $Service.ExecQuery("Select * from Win32_Service Where State = 'Paused' and StartMode = 'Auto'")
    For $objService In $sItems
    If $objService.Name == $iName Then
    $objService.ResumeService($objService.Name)
    Return
    EndIf
    Next
    Return SetError(-1)
    EndFunc ;==>_ServResume

    [/autoit]
  • UEZ:
    Um ehrlich zu sein, verstehe ich dein Script nicht so recht :pinch:

    Habe meine Problematik mal etwas anders gelöst:

    [autoit]

    #include <array.au3>

    [/autoit][autoit][/autoit][autoit]

    Local $pcname[1] = ["PCname"]

    [/autoit][autoit][/autoit][autoit]

    For $a = 0 to ubound($PCname) - 1
    Local $commandName = "sc \\" & $PCname[$a] & " " & "start RemoteRegistry"
    Run(@ComSpec & " /c " & $commandName, "", @SW_HIDE)
    next

    [/autoit]

    Wie ich das jetzt mit dem aktivieren/deaktivieren hinbekommen, weiss ich nicht.
    Das kann man an der Kommandozeile scheinbar nicht für Remoterechner machen...

    Einmal editiert, zuletzt von Scritch (30. Dezember 2010 um 16:25)

  • Wenn Du die Dienste Remote konfigurieren willst dann besorg Dir die PsTools

    Dort gibts es eine psservice.exe

    Damit ginge es so z. B.

    psservice \\RemotePC setconfig Remote-Registrierung auto

    auto=Automatisch - demand=Manuell - disabled=Deaktiviert

    Danach Start über
    psservice \\RemotePC start Remote-Registrierung


    LG

    Schnuecks

  • Danke, genau das was ich gesucht habe. Funktioniert auf der Kommandozeile wunderbar, nur im Script leider nicht :pinch:

    [autoit]

    Local $pcname[1] = ["PCname"]
    For $a = 0 to ubound($pcname) - 1
    Local $commandName = "psservice" & "\\" & $pcname[$a] & " " & "setconfig Remote-Registrierung auto"
    Run(@ComSpec & " /c " & $commandName, "", @SW_HIDE)
    next

    [/autoit]
  • veruch es so:

    [autoit]

    Local $pcname[1] = ["PCname"]
    For $a = 0 to ubound($pcname) - 1
    Local $commandName = "psservice \\" & $pcname[$a] & " setconfig Remote-Registrierung auto"
    ConsoleWrite($commandName & @crlf) ;diese Zeilen sind nur zur Kontrolle
    Run($commandName, "", @SW_HIDE)
    next

    [/autoit]

    mfg autoBert

  • Du hattest ein Leerzeichen vergessen. Ist meit das Problem bei run/shellexecute & co, dehalb habe ich immer eine Consolenausgabe dabei da fäll es auf. Der Comspec wird afaik nur gebraucht wenn Befehle des Kommandpinterpreters benutzt werden, nicht aber bei EXE-Datei aufrufen,

    PS.: auf gelöst etzen nicht vergessen,

    mfg autoBert