Simplest way to restart service on a remote computer

后端 未结 11 1926
我寻月下人不归
我寻月下人不归 2020-12-23 13:07

What\'s the easiest programmatic way to restart a service on a remote Windows system? Language or method doesn\'t matter as long as it doesn\'t require human interaction.

相关标签:
11条回答
  • 2020-12-23 13:48

    If it doesn't require human interaction which means there will be no UI that invokes this operation and I assume it would restart at some set interval? If you have access to machine, you could just set a scheduled task to execute a batch file using good old NET STOP and NET START

    net stop "DNS Client"
    net start "DNS client"
    

    or if you want to get a little more sophisticated, you could try Powershell

    0 讨论(0)
  • 2020-12-23 13:48

    As of Powershell v3, PSsessions allow for any native cmdlet to be run on a remote machine

    $session = New-PSsession -Computername "YourServerName"
    Invoke-Command -Session $Session -ScriptBlock {Restart-Service "YourServiceName"}
    Remove-PSSession $Session
    

    See here for more information

    0 讨论(0)
  • 2020-12-23 13:49
    1. open service control manager database using openscmanager
    2. get dependent service using EnumDependService()
    3. Stop all dependent services using ChangeConfig() sending STOP signal to this function if they are started
    4. stop actual service
    5. Get all Services dependencies of a service
    6. Start all services dependencies using StartService() if they are stopped
    7. Start actual service

    Thus your service is restarted taking care all dependencies.

    0 讨论(0)
  • 2020-12-23 13:51

    If you attempting to do this on a server on different domain you will need to a little bit more than what has been suggested by most answers so far, this is what I use to accomplish this:

    #Configuration
    $servername = "ABC",
    $serviceAccountUsername = "XYZ",
    $serviceAccountPassword = "XXX"
    
    #Establish connection
    try {
        if (-not ([System.IO.Directory]::Exists('\\' + $servername))) {
            net use \\$servername /user:$serviceAccountUsername $serviceAccountPassword
        }
    }
    catch {
        #May already exists, if so just continue
        Write-Output $_.Exception.Message
    }
    
    #Restart Service
    sc.exe \\$servername stop "ServiceNameHere"
    sc.exe \\$servername start "ServiceNameHere"
    
    0 讨论(0)
  • 2020-12-23 13:52

    As of Windows XP, you can use sc.exe to interact with local and remote services. Schedule a task to run a batch file similar to this:

    sc \\server stop service
    sc \\server start service
    

    Make sure the task runs under a user account privileged on the target server.

    psservice.exe from the Sysinternals PSTools would also be doing the job:

    psservice \\server restart service
    
    0 讨论(0)
  • 2020-12-23 13:53

    DESCRIPTION: SC is a command line program used for communicating with the NT Service Controller and services. USAGE: sc [command] [service name] ...

        The option <server> has the form "\\ServerName"
        Further help on commands can be obtained by typing: "sc [command]"
        Commands:
          query-----------Queries the status for a service, or
                          enumerates the status for types of services.
          queryex---------Queries the extended status for a service, or
                          enumerates the status for types of services.
          start-----------Starts a service.
          pause-----------Sends a PAUSE control request to a service.
          interrogate-----Sends an INTERROGATE control request to a service.
          continue--------Sends a CONTINUE control request to a service.
          stop------------Sends a STOP request to a service.
          config----------Changes the configuration of a service (persistant).
          description-----Changes the description of a service.
          failure---------Changes the actions taken by a service upon failure.
          qc--------------Queries the configuration information for a service.
          qdescription----Queries the description for a service.
          qfailure--------Queries the actions taken by a service upon failure.
          delete----------Deletes a service (from the registry).
          create----------Creates a service. (adds it to the registry).
          control---------Sends a control to a service.
          sdshow----------Displays a service's security descriptor.
          sdset-----------Sets a service's security descriptor.
          GetDisplayName--Gets the DisplayName for a service.
          GetKeyName------Gets the ServiceKeyName for a service.
          EnumDepend------Enumerates Service Dependencies.
    
        The following commands don't require a service name:
        sc <server> <command> <option>
          boot------------(ok | bad) Indicates whether the last boot should
                          be saved as the last-known-good boot configuration
          Lock------------Locks the Service Database
          QueryLock-------Queries the LockStatus for the SCManager Database
    

    EXAMPLE: sc start MyService

    0 讨论(0)
提交回复
热议问题