I would like to remotely restart a Windows service on a target machine in the network of my PRTG core server. How can I do this automatically?
Can I automatically restart a Windows service with PRTG?
Votes:
0
7 Replies
Votes:
0
This article applies to PRTG Network Monitor 12 or later
Automatically Restart Windows Services
This functionality is not build into PRTG by default, but you can achieve an automatic Windows service restart by using a custom script in combination with PRTG's Execute Program notification.
- Write a script that is capable of restarting the desired Windows service on the target computer.
- Adapt the script to suit your needs. You can also use placeholders. For details, please see What placeholders can I use with PRTG?
- Note: Notification scripts can only be executed on the machine running the PRTG core server, never on a Remote Probe!
- Adapt the script to suit your needs. You can also use placeholders. For details, please see What placeholders can I use with PRTG?
- Save the script to the Notifications\EXE sub folder of your PRTG program directory. Make sure the Windows user account running your PRTG core server has sufficient rights to execute the script. By default, this is the "System" local Windows user account.
- In PRTG, create a new notification (see PRTG Manual: Account Settings—Notifications). Enable the Execute Program section and select your newly saved script. Save your settings.
- (Optional) Test the notification by clicking on the Test link in the list of notifications.
- Now you need to set a notification trigger to execute the script on a certain condition. For example, navigate to Notifications tab of a sensor, then add a trigger for the status condition Down which executes your "Execute Program" notification. The script will then be executed if the sensor changes to a red "down" status. For details about notifications triggers, please see PRTG Manual: Sensor Notifications Settings
See Also
Created on Sep 3, 2012 12:03:55 PM by
Daniel Zobel [Product Manager]
Last change on Apr 6, 2017 6:56:07 AM by
Luciano Lingnau [Paessler]
Votes:
0
Under step 1, stopping and starting a service can also be done with a simple batch file using the SC command.
@echo off rem stop service SC \\ServerName stop ServiceName > nul rem wait 3 seconds ping 1.1.1.1 -n 1 -w 3000 > nul rem start service SC \\ServerName start ServiceName > nul
Votes:
0
I believe this post has now been superseded, as within a 'wmiservicesensor' you can now choose two options under "If Service is Not Running"? - namely are:
"Start/Restart Service" "Do Nothing"
I assume this means it will do what it says on the tin? (i.e. restart the service), but I wondered what the frequency of the attempted restarts are? / how you can get notified of restarts? etc.
Sorry if this is the wrong place to put this question, but I can't find any information on this new feature within the product.
Any help appreciated!
Votes:
0
That is correct, it will try to restart the service. The sensor will try to restart the service once. A notification would be triggered by defining a trigger for the "When condition clears..." entry (seeing as the sensor would switch from an error to an up state).
Votes:
0
Hi,
We would like to restart a Windows Service based on a specific criteria; ex. high memory consumption. We've created the powershell script to be used as a custom notification with parameters %host and %ServiceDisplayName%, but we can't figure out the placeholder that refers to the Service Display Name as detected by the WMI Service Monitor.
As a reference:
# Restart Windows Service # # The Parameter section consists of 2 parameters: ComputerName, DisplayName # if ($Args.Count -lt 2) { Write-Host "Not enough Arguments. ComputerName and DisplayName must be specified." exit 1; } elseif ($Args.Count -eq 2) { $service = Get-Service -ComputerName $Args[0] -DisplayName $Args[1] -ErrorAction SilentlyContinue if ($service.Length -gt 0) { # Service exists Try { (get-service -ComputerName $Args[0] -DisplayName $Args[1]).Stop() (get-service -ComputerName $Args[0] -DisplayName $Args[1]) | Set-Service -Status Running exit 0; } Catch { exit 2; } } else { Write-Host "Service not found" exit 2; } } else { Write-Host "Too many arguments." exit 2; }
Created on Jun 8, 2015 3:20:23 PM
Last change on Apr 5, 2017 1:49:24 PM by
Luciano Lingnau [Paessler]
Votes:
0
Simply put the friendly name in the sensor comments and use %comments as the placeholder :)
Votes:
0
Here's a different take on the Powershell variant, this does a remote connection via Invoke-Command and runs the other commands locally. The advantage of this is approach is that you can provide credentials when starting the Invoke-Command.
# Restart Service on Remote Computer # $server - The server on which the server should be restarted, use FQDN for reliability: 'server.domain.tld' # $account - The account to be used to connect via powershell remoting, use 'domain.tld\account' format # $password - The password of the account # $serviceName - The displayname of the Service, For instance 'SNMP Service' # Parameters in PRTG can be: -server 'server.contoso.com' -account 'contoso.com\john.doe' -password 'mysafepassword' -serviceName 'PRTG Probe Service' param( [string]$server = 'server.contoso.com', [string]$account = 'contoso.com\john.doe', [string]$password = 'mysafepassword', [string]$serviceName = 'PRTG Probe Service' ) $pass = ConvertTo-SecureString -AsPlainText $password -Force $credential = New-Object System.Management.Automation.PSCredential -ArgumentList $account,$pass Invoke-Command -ComputerName $server -Credential $credential -ArgumentList $serviceName -ScriptBlock { param( [string]$serviceName ) try { Stop-Service -DisplayName $serviceName -WarningAction SilentlyContinue Write-Output "Stop service $($serviceName): OK" }catch{ Write-Output "Stop service $($serviceName): Failed - $($_.Exception.Message)" } try { Start-Service -DisplayName $serviceName -WarningAction SilentlyContinue Write-Output "Start service $($serviceName): OK" }catch{ Write-Output "Start service $($serviceName): Failed - $($_.Exception.Message)" } Exit 0 }
Created on Apr 6, 2017 6:45:20 AM by
Luciano Lingnau [Paessler]
Last change on Apr 6, 2017 6:55:26 AM by
Luciano Lingnau [Paessler]
Add comment