How can I reboot a computer with a WMI call?

Votes:

0

Your Vote:

Up

Down

I would like to reboot a remote computer using a Windows Management Instrumentation (WMI) call.

  • Is there a script that I can use to invoke a reboot / restart call?
  • Can you give me an example on how to set up a custom EXE notification?
  • How do I set up a notification trigger?

custom-notification howto notifications prtg reboot script wmi

Created on Feb 3, 2010 1:05:52 PM by  Daniel Zobel [Paessler Support] (24,941) 3 3

Last change on Aug 12, 2010 11:12:30 AM by  Daniel Zobel [Paessler Support] (24,941) 3 3



6 Replies

Accepted Answer

Votes:

0

Your Vote:

Up

Down

This article applies to PRTG Network Monitor Version 12 or later, as well as to previous (deprecated) versions

How To Reboot a Computer via Windows Management Instrumentation (WMI) Call

It's the main task of PRTG Network Monitor to optimize your network's availability and performance by avoiding system downtimes. All systems should always be up and running properly. That's the theory... Still some services fail, even without a complete crash of the server itself. One of the most common methods of recovery is a reboot of the server to get the failing service back online.

In certain cases, it is useful to have this done automatically. PRTG initiates a reboot when it discovers a service that fails for more than 15 minutes for example. Remote reboots can be performed by command lines tools (e.g. from the Sysinternals toolset) or by using a Windows Management Instrumentation (WMI) call.

Usually, PRTG notifications are used to notify you as a system administrator whenever something unusual happens in your network. PRTG can send you messages via email, pager and many other ways. But instead of just sending warnings to your mobile phone, you can also program PRTG to automatically launch any program or script with the "Execute Program" notification. This is still called a "notification", but it does far more. We will show you how to use this capability to reboot a remote system when a certain sensor enters an "Error" state. For the reboot, a VBS script initiates a Windows Management Instrumentation (WMI) call. No additional software is needed on the server or the target system. All you need are two WMI capable Windows system for the PRTG server and the to-be-rebooted client.

Setting Up a WMI Reboot

Generally, there are three steps to set up your WMI reboot:

  1. Create a VBS script that can invoke the reboot call (see the sample below) and enter the necessary credentials and device names.
  2. Setup this script as a PRTG notification.
  3. Connect this notification with a sensor's notification trigger.

Step 1: Create a VBS Script that Can Invoke the Reboot Call

To create a reboot script for use with PRTG, follow these simple steps:

  • Ensure that the folder C:\temp exists. The log file will be created there. Without this folder the script will not work and an error message might appear, for example, RPC Server unavailable.
  • Ensure that WMI is running. You can check this with the Windows System Uptime sensor on the server you would like to apply a reboot call.
  • Create a text file and paste the following VBS script (see other posts below for a modified script which allows for rebooting multiple servers and adding delays):
' ***************************************************************************
' PRTG CUSTOM EXE Notification, reboots a remote computer via WMI
' ***************************************************************************
' created Nov 2009 for PRTG Network Monitor V7, tested and updated Sep 2013 for PRTG V13 by Paessler Support Team, www.paessler.com
' this script is open Source and comes without support and warranty

' When a sensor monitoring a remote computer goes into "Down" state, you sometimes may find
' it useful to reboot this computer.
' You can do so by assigning a EXE notification to the critical sensor.
' This Sensor triggers on "Down" State a VisualBasic Script that does the actual rebooting.
'

' *********************** Set your Parameters here ***************************
' Adjust the parameters to your needs. Of course you can pass the parameters from within PRTG,
' using the according parameters.
' Note: If your computer is not in a domain, enter an empty string "" for strDomain.

On Error Resume Next

strComputer = "MyComputer"
strDomain = "MyDomain"
strUser = "MyName"
strPassword = "MyPassword"

' *************************** Codesection ***********************************

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\temp\ScriptLog.txt",true)

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer,_
                            "root\cimv2",_
                            strUser,_
                            strPassword,_
                            "",_
                            "ntlmdomain:" + strDomain,_
                            "128")

If Err.Number <> 0 Then
  objFile.writeline(Err.Description)
  wscript.quit(Err.Number)
else
 
  On Error goto 0
  Set colSwbemObjectSet = objSWbemServices.ExecQuery("Select * From Win32_OperatingSystem")
  For Each objSystem in colSWbemObjectSet
    objSystem.Reboot
  Next
 
  objFile.writeline("Success")
  wscript.quit("0")

End if 
  • In the script, adjust the parameters as described, according to the computer you want to reboot. Enter Computer name, Domain name and the credentials of the Windows user account as specified on the remote system. It's the same data you would enter in PRTG as WMI credentials. If your computer is not in a domain, simply enter an empty string ("") for the according variable.
  • Save the script in the Notifications\EXE subfolder of your PRTG installation with a meaningful name and extension .vbs (e.g. "reboot_devicename.vbs"). Please note: When saving the file, enclose the filename in quotes to make sure that the extension .VBS is saved correctly (many text editors may want to save it as .TXT)!

Step 2: Setup This Script as a PRTG Notification

Now, you can setup this script as a notification in your PRTG installation:

  • Login to the PRTG web interface and select Setup | Account Settings | Notifications from the main menu bar.
  • Click on Add new notification to enter the "Add Notification" screen.
  • Enter a new Notification Name (e.g. "Reboot Devicename") and additionally a Schedule (if you want to avoid reboots for certain time spans).
  • Select Execute Program and select the .VBS file you just created. The script will be called when the notification is triggered later.
  • You can leave the Parameter field empty. Click on Save.

Tip: You can test the functionality of the batch/script combination by clicking on the Test button next to the notification name. IMPORTANT: After clicking on "Test", the reboot should be performed.

Step 3: Connect This Notification With a Sensor's Notification Trigger

To trigger this new reboot "notification" (it's still called notification, although it performs a reboot), connect it with a sensor:

  • In PRTG web interface's main menu, click on either "Devices" or "Sensors" and in the appearing list, select a sensor by just clicking on it. You then enter the sensor's detail view.
  • Click on the "Notifications" tab and then click on Add State Trigger.
  • Choose Condition Down. In the "latency" field, you can enter a time in seconds for which the reboot is being delayed in case the sensor enters the "Down" state. This will give the sensor a chance to recover before a reboot is performed. We recommend a value of a few minutes, such as "900" for 15 minutes.
  • In the drop down menu "perform notification", select the "Reboot Devicename" notification you created before.
  • Click on "Save".

Done. You now have successfully created your reboot notification!

Testing your new notification trigger is easy: Simply right-click on the sensor in the device tree you have created the notification trigger for and select Simulate Error Status from the context menu. After latency time has elapsed, the reboot will be performed.

Remarks

  • Any notification will always be executed on the computer running the PRTG core server (never on the computer running a remote probe!)
  • We recommend using the reboot "notification" as Escalation Notification with e.g. 15 minutes delay. This way, you can send out, e.g., emails or text messages using the normal notifications minutes or hours before the computer is rebooted. When you do not deal with the failure in a certain time, the reboot will then be performed as an escalating action.
  • Of course, you can also define other trigger types for use with the reboot "notification" (such as State, Speed, Volume, Threshold or Change Triggers). It depends on your specific usage scenarios if this makes sense for you.
  • Adding reboot "notification" triggers on group or probe level does not make too much sense (each VBS script can only reboot one specific device).

See Also

Created on Feb 3, 2010 1:11:20 PM by  Daniel Zobel [Paessler Support] (24,941) 3 3

Last change on Sep 12, 2013 3:44:56 PM by  Gerald Schoch [Paessler Support] (6,173) 2 1



Votes:

0

Your Vote:

Up

Down

I have tried a similar approach but with restarting a service on a remote computer, but it does not work from withing Paessler monitoring service. I have multiple versions (WMI, Powershell, Sysinternals) but all have the same problem. It is on a windows 2008 R2 server and I think it has something to do with the fact that the notifications folder is a subfolder of the program files.

Does someone have a solution for me?

I will upgrade the software from 7.2.4.5051 to the latest version.

Mario

Created on Jul 27, 2010 2:29:54 PM by  mariowitdoek (0) 1



Votes:

0

Your Vote:

Up

Down

PTF Launcher

If the computer that has to be rebooted has a user loggedin, you can use PTF Launcher to start "shutdown /r" on the specified computer in order to reboot that computer.

Use the -c= parameter to specify the computer that has PTF Launcher running.

Enter the folowing paramters in the parameter section of the Launcher Custom Notification

-c=192.168.1.1 -s=shutdown.exe -a=/r

You can also install PTF Launcher an any other computer that has a user loggedin to reboot the secified computer with "shutdown /r /m \computer"

-c=192.168.1.1 -s=shutdown.exe -a=/r /m \\computer

Please see the .pdf that is included in the download for more information.

Created on Jul 30, 2010 9:02:19 AM by  PRTGToolsFamily (5,544) 3 2

Last change on Apr 27, 2012 12:54:47 PM by  Manischa Mittal [Paessler Support] (0) 1



Votes:

0

Your Vote:

Up

Down

Reboot Multiple Servers

The following modification of the original script allows for rebooting multiple servers and adding delays.

Our special thanks go to Dale Van Aken for providing his modified script!

' ***************************************************************************
' PRTG Notification, performs reboot/shutdown on a list of remote computers via WMI
' ***************************************************************************
' 
' This relies on a Custom EXE notification to execute.
'
' Note: If computer is not in domain, use empty string "" for strDomain.
'
' DVA 9/12/12 - Code modified from Paessler provided sample for multiple 
' servers and allows for delays to be configured; for example to permit 
' dependent servers to shutdown before a Domain Controller
'
' configure for either shutdown or reboot
'
' ****************************************************************************

On Error Resume Next

' list of domain computers to shut down

arrComputerList = Array("fileserver","exchangeserver","phones")

' Delay this many seconds before shutdown of corresponding server

DelayList = Array(15,30,30)

strDomain = "mydomain"
strUser = "prtg" ' must be member of Domain Administrators
strPassword = "password"

' *************************** Code *******************************************

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\Program Files\PRTG Network Monitor\Notifications\EXE\ShutdownLog.txt",true)

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")

idx = 0

For each strComputer in arrComputerList
	On Error Resume Next

	Wscript.Sleep DelayList(idx)*1000
	
	Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer,_
                            "root\cimv2",_
                            strUser,_
                            strPassword,_
                            "",_
                            "ntlmdomain:" + strDomain,_
                            "128")

	If Err.Number <> 0 Then
  		objFile.writeline(strComputer & ": Failed")
  		objFile.writeline(strComputer & ": " & Err.Description)
	else

' 
' we have access - do the shutdown/reboot
'

	On Error goto 0
  	Set colSwbemObjectSet = objSWbemServices.ExecQuery("Select * From Win32_OperatingSystem")
  	
		For Each objSystem in colSWbemObjectSet

'
'  ***** for testing only writes log entry - uncomment the correct line for your needs
'
'   		objSystem.Shutdown
'   		objSystem.Reboot

 		objFile.writeline("Acting on: " & strComputer)

  		Next

  		objFile.writeline(strComputer & ": Success")

	End if 

	idx = idx + 1

Next  ' strComputer
 
objFile.writeline( idx & " Completed [ok]")
wscript.quit("0")

Note:

  • Ensure to use the correct path to your PRTG installation in line 2 of the code above, i.e.: Set objFile = objFSO.CreateTextFile("C:\Program Files (x86)\PRTG Network Monitor\Notifications\EXE\ShutdownLog.txt",true)
  • The actions reboot resp. shutdown are commented out. Remove the quotation mark at the beginning of the corresponding line to uncomment it.

Created on Sep 17, 2012 8:59:09 AM by  Daniel Zobel [Paessler Support] (24,941) 3 3

Last change on Sep 12, 2013 4:05:10 PM by  Gerald Schoch [Paessler Support] (6,173) 2 1



Votes:

0

Your Vote:

Up

Down

Is there a way to do this without the need to save a password in clear text? Our guidelines don't allow for something like this ...

Created on Sep 19, 2012 7:29:27 AM by  Christian Möller (0) 1



Votes:

0

Your Vote:

Up

Down

Dear Christian,

according to the API documentation ("Setup | PRTG API" in your PRTG installation), you can use the credentials of the parent device where you created the sensor on.

When calling the script in PRTG, simply hand them over to the script as parameters, using the following PRTG placeholders:

PlaceholderContents
%windowsdomainThe domain for Windows access (may be inherited from parent)
%windowsuserThe user name for Windows access (may be inherited from parent)
%windowspasswordThe password for Windows access (may be inherited from parent)

On runtime, PRTG will automatically replace the placeholders by the credentials set for the parent device.

Created on Sep 19, 2012 8:50:00 AM by  Daniel Zobel [Paessler Support] (24,941) 3 3

Last change on Sep 19, 2012 9:00:14 AM by  Daniel Zobel [Paessler Support] (24,941) 3 3



Please log in or register to enter your reply.


Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.

PRTG
Network Monitor
Intuitive to Use.
Easy to manage.

150.000 administrators have chosen PRTG to monitor their network. Find out how you can reduce cost, increase QoS and ease planning, as well.

Visit
www.paessler.com

What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general. You are invited to get involved by asking and answering questions!

Learn more

Top Tags


View all Tags