Jump to content

restart services on remote computer GUI


FMS
 Share

Recommended Posts

Dear reader,

I'm a little stuck in writting the script I want to build.

The anoying thing is : I know how to do it myself but not how to build it.

When u see the code u will understand what i mean

The mean problem is that i don't know how to use command line in[puts in autoit.

When is searching the help files of autoit i dint find anything usefull :(

I hope there is somebody that will push me in the right direction for building this "simple" code :)

(read: not for me that simple since I'm exploring Autoit :))

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### Form
$Form1 = GUICreate("Services restarter", 381, 205, 192, 114)
$ButtonServ1 = GUICtrlCreateButton("Restart services 1", 224, 32, 121, 33)
$ButtonServ2 = GUICtrlCreateButton("Restart services 2", 224, 80, 121, 33)
$ButtonServ3 = GUICtrlCreateButton("Restart services 3", 224, 128, 121, 33)
$Group1 = GUICtrlCreateGroup("Services", 208, 16, 153, 169)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$ButtonCancel = GUICtrlCreateButton("Cancel", 40, 144, 121, 33)
$LUsername = GUICtrlCreateLabel("Username", 16, 40, 52, 17)
$IUsername = GUICtrlCreateInput("", 96, 40, 89, 21)
$IPassword = GUICtrlCreateInput("", 96, 80, 89, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_PASSWORD))
$LPassword = GUICtrlCreateLabel("Wachtwoord", 16, 80, 65, 17)
$Group2 = GUICtrlCreateGroup("Credentials", 8, 16, 185, 113)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### end Form
While 1
 $nMsg = GUIGetMsg()
 Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  case $ButtonCancel
   Exit
  case $ButtonServ1
   Local $var = Ping("Computer1", 250)
    If $var Then
    #comments-start command line inputs
    sc \\Computer1 stop Service1
    sc \\Computer1 query Service1
    if service1 stopped then
    sc \\Computer1 stop Service2
    sc \\Computer1 query Service2
    if service2 stopped then
    sc \\Computer1 stop Service3
    sc \\Computer1 query Service3
    if service3 stopped then
    sc \\Computer1 start Service1
    sc \\Computer1 query Service1
    if service1 started then
    sc \\Computer1 start Service2
    sc \\Computer1 query Service2
    if service2 started then
    sc \\Computer1 start Service3
    sc \\Computer1 query Service3
    if service3 started then
    #comments-end
    Else
     MsgBox(0, "Status", "An error occured with number: " & @error & @CRLF & "1 = Host is offline"& @CRLF & "2 = Host is unreachable"& @CRLF & "3 = Bad destination"& @CRLF & "4 = Other errors or also unreachable")
    EndIf
 EndSwitch
WEnd
Edited by FMS

as finishing touch god created the dutch

Link to comment
Share on other sites

Hello,

thanks for the tip Jfish and looked in to it.

Afther a weekend of "trail and error" i come up whit something like this:  

case $ButtonServ1
   Local $var = Ping("Computer1", 250)
    If $var Then
     Run ("cmd.exe")
     WinWaitActive("C:\Windows\System32\cmd.exe")
     send ("sc Computer1query Service1")

     ;(((don't know how to read the query)))) : iff service is running or stopped do .. until stopped

     send ("sc Computer1 stop Service1")

firing up the command line and put in the commands whit the "send" command.

I realy think that there must be a easyer way than making send commands.

Could somebody help me whit this??

thanks in advanced..

Greez FMS

as finishing touch god created the dutch

Link to comment
Share on other sites

how about an easier solution - if this GUI is to be used by you or by power user, and if you do not need to automate the restarting of services, then use this:

services.msc /Computer=<computername>

than manually filter by Status, select the service you want, and click Start.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

euhmm good tip :)
but i wanted to give this tool to application managers who have some restriction on the network.

So i need to GUI this so no other services where stopped accidently.

thnx in advanced

as finishing touch god created the dutch

Link to comment
Share on other sites

quick and dirty: get all stopped services for a computer. i don't think it needs any comments, if something is not clear then just ask.

#include <String.au3>
#include <Array.au3>

$sSC_hostname=@ComputerName
$sSC_output=@TempDir&'\SCoutput.txt'
FileDelete($sSC_output)

RunWait(@ComSpec&' /c sc.exe \\'&$sSC_hostname&' query state= inactive > "'&$sSC_Output&'"','',@SW_HIDE)

$sSC_content=FileRead($sSC_Output)
$aStoppedServicesList=_StringBetween($sSC_content,'DISPLAY_NAME: ',@CRLF)
_ArrayDisplay($aStoppedServicesList)

this script assumes you have sufficient permissions. if the user needs to specify credentials, then use RunAsWait() instead of RunWait()

instead of _ArrayDisplay() you probably want to fill a List or a ListView with the array data, then when user clicks a button, read the selected entry from the list, send sc.exe to start the service, wait few seconds and check the new status of all services - because the service you start may start a dependency service, don't overlook that.

P.S. and there is also this:

'?do=embed' frameborder='0' data-embedContent>>

Edited by orbs

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

WMI seems to be good for this job.

Here are some functions to do what you want, I think.

Just look at the beginning of the script for the example.

; Retrives informations for RemoteRegistry service
$aServiceInfos = _WMIService_Get("RemoteRegistry", @ComputerName) ; Replace this by your values
If IsArray($aServiceInfos) Then
    If $aServiceInfos[0][3] = True Then ; The service is started
        ; Then Stops the service
        If _WMIService_Stop("RemoteRegistry") Then MsgBox(0, "", "The RemoteRegistry service has stopped")
    Else ; The service is stopped
        ; Then starts the service
        If _WMIService_Start("RemoteRegistry") Then MsgBox(0, "", "The RemoteRegistry service has started")
    EndIf
Else
    MsgBox(16, "Error", "Unable to retrieve informations for this service on this computer")
EndIf










; #FUNCTION# ======================================================================================
; Name...........: _WMIService_Get
; Description ...: Retrieves informations for one or all services on a remote/local computer
; Syntax.........: _WMIService_Get($sServiceName, $sComputerName)
; Parameters ....: $sServiceName - Name of the service (shortname) to retrieve informations for.
;                  Defaut "" returns informations for all services
;                  $sComputerName - Name or IPAddress of the remote computer
;                  Default "" is the current omputername
; Return values .: Success - Returns an array containing informations
;                   $aReturn[n][0] - Service Name
;                   $aReturn[n][1] - Service DisplayName
;                   $aReturn[n][2] - PathName : Fully qualified path to the service binary file that implements the service.
;                   $aReturn[n][3] - True if service is started, or false if not
;                   $aReturn[n][4] - Start mode of the Windows base service.
;                                    Possible return values are "Boot", "System", "Auto", "Manual", "Disabled"
;                   $aReturn[n][5] - Current state of the base service.
;                                    Possible return values are "Stopped", "Start Pending", "Stop Pending", "Running",
;                                    "Continue Pending", "Pause Pending", "Paused", "Unknown"
;                  Error - Returns 0
; =================================================================================================
Func _WMIService_Get($sServiceName = "", $sComputerName = ".")
    Local $aReturn[1][6]
    Local $wbemFlagReturnImmediately = 0x10
    Local $wbemFlagForwardOnly = 0x20
    Local $objService
    Local $n = 0
    
    Local $sFilter = ""
    If $sServiceName <> "" Then $sFilter = " Where Name = '" & $sServiceName & "'"

    Local $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $sComputerName & "\root\cimv2")
    Local $colService = $objWMIService.ExecQuery ("Select * from Win32_Service" & $sFilter, "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If NOT IsObj($colService) Then Return 0

    For $objService in $colService
        $n += 1
        Redim $aReturn[$n][6]
        
        $aReturn[$n - 1][0] = $objService.Name
        $aReturn[$n - 1][1] = $objService.DisplayName
        $aReturn[$n - 1][2] = $objService.PathName
        $aReturn[$n - 1][3] = $objService.Started
        $aReturn[$n - 1][4] = $objService.StartMode
        $aReturn[$n - 1][5] = $objService.State
    Next
    
    If $n = 0 Then Return 0
    Return $aReturn
EndFunc




; #FUNCTION# ======================================================================================
; Name...........: _WMIService_Start
; Description ...: Starts a service
; Syntax.........: _WMIService_Start($sServiceName, $sComputerName)
; Parameters ....: $sServiceName - Name of the service (shortname) to start.
;                  $sComputerName - Name or IPAddress of the remote computer
;                  Default "" is the current omputername
; Return values .: Success - Returns 1
;                  Error - Returns -1 if service name is invalid, or returns 0 and sets @error to
;                      0 : Success
;                      1 : Not Supported
;                      2 : Access Denied
;                      3 : Dependent Services Running
;                      4 : Invalid Service Control
;                      5 : Service Cannot Accept Control
;                      6 : Service Not Active
;                      7 : Service Request Timeout
;                      8 : Unknown Failure
;                      9 : Path Not Found
;                     10 : Service Already 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
; =================================================================================================
Func _WMIService_Start($sServiceName, $sComputerName = ".")
    Local $wbemFlagReturnImmediately = 0x10
    Local $wbemFlagForwardOnly = 0x20
    Local $iReturn
    
    Local $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $sComputerName & "\root\cimv2")
    Local $colService = $objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & $sServiceName & "'", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If NOT IsObj($colService) Then Return -1
    
    For $objService in $colService
        $iReturn = $objService.StartService()
        If $iReturn = 0 Then
            Return 1
        Else
            Return SetError($iReturn, 0, 0)
        EndIf
    Next

    Return -1
EndFunc




; #FUNCTION# ======================================================================================
; Name...........: _WMIService_Stop
; Description ...: Stops a service
; Syntax.........: _WMIService_Stop($sServiceName, $sComputerName)
; Parameters ....: $sServiceName - Name of the service (shortname) to stop.
;                  $sComputerName - Name or IPAddress of the remote computer
;                  Default "" is the current omputername
; Return values .: Success - Returns 1
;                  Error - Returns -1 if service name is invalid, or returns 0 and sets @error to
;                      0 : Success
;                      1 : Not Supported
;                      2 : Access Denied
;                      3 : Dependent Services Running
;                      4 : Invalid Service Control
;                      5 : Service Cannot Accept Control
;                      6 : Service Not Active
;                      7 : Service Request Timeout
;                      8 : Unknown Failure
;                      9 : Path Not Found
;                     10 : Service Already 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
; =================================================================================================
Func _WMIService_Stop($sServiceName, $sComputerName = ".")
    Local $wbemFlagReturnImmediately = 0x10
    Local $wbemFlagForwardOnly = 0x20
    Local $iReturn
    
    Local $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $sComputerName & "\root\cimv2")
    Local $colService = $objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & $sServiceName & "'", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If NOT IsObj($colService) Then Return -1
    
    For $objService in $colService
        $iReturn = $objService.StopService()
        If $iReturn = 0 Then
            Return 1
        Else
            Return SetError($iReturn, 0, 0)
        EndIf
    Next

    
    Return -1
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...