Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Remote Service's Recovery Options using Powershell?

I am have a really hard time getting this to work. Hopefully someone can help me out!

I am currently working on a Powershell deployment script for a service. After installing the service, I'd like to set the Service Recovery options to "Restart the Service" every time the service crashes after 0 minutes.

Does anyone know how to do this using Powershell to set these options for remote machine?

like image 542
Max Alexander Avatar asked Feb 13 '12 20:02

Max Alexander


People also ask

How do I start and stop Windows services on a remote machine using PowerShell?

Method 3: Using PowerShell Get-Service -ComputerName computername -Name servicename | Restart-Service -Force. Get-Service -ComputerName computername -Name servicename | Stop-Service -Force. Get-Service -ComputerName computername -Name servicename | Start-Service.

How do I get remote computer services in PowerShell?

Getting Remote Services With Windows PowerShell, you can use the ComputerName parameter of the Get-Service cmdlet to get the services on remote computers. The ComputerName parameter accepts multiple values and wildcard characters, so you can get the services on multiple computers with a single command.

How do I disable a service in PowerShell?

Open Start. Search for PowerShell, right-click the top result, and select the Run as administrator option. Type the following command to disable a service and press Enter: Set-Service -Name "SERVICE-NAME" -Status stopped -StartupType disabled In the command, update "SERVICE-NAME" for the name of the service.

How do I stop and start services in PowerShell?

To start or stop a service through PowerShell, you can use the Start-Service or the Stop Service cmdlet, followed by the name of the service that you want to start or stop. For instance, you might enter Stop-Service DHCP or Start-Service DHCP.


1 Answers

You can write a powershell function using sc.exe as explained here. The function will look something like:

function Set-Recovery{
    param
    (
        [string] 
        [Parameter(Mandatory=$true)]
        $ServiceName,

        [string]
        [Parameter(Mandatory=$true)]
        $Server
    )

    sc.exe "\\$Server" failure $ServiceName reset= 0 actions= restart/0 #Restart after 0 ms
}

And you can call the function like:

Set-Recovery -ServiceName "ServiceName" -Server "ServerName"

Note: The account you are running the script must have admin rights on the remote server.

like image 166
Mohammad Nadeem Avatar answered Sep 19 '22 12:09

Mohammad Nadeem