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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With