Set Remote Service's Recovery Options using Powershell?

Max Alexander picture Max Alexander · Feb 13, 2012 · Viewed 19k times · Source

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?

Answer

Mohammad Nadeem picture Mohammad Nadeem · May 12, 2014

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.