Scripting a Reliable Remote Reboot of a Windows Server
There may be a day when Microsoft realizes that rebooting a server after trivial security updates is too much hassle. Or, that undistinguished, yet business critical, third party application's memory leak just never gets fixed, forcing you to reboot weekly to prevent an outage. As a Windows sysadmin, unless you plan on migrating all your server applications to Unix, you're stuck rebooting your servers periodically. I came up with a way to issue reboots via ad-hoc or a scheduling tool to remotely reboot servers and ensure they are back online.
It is trivial to remote reboot a Windows server. Among the alternatives, this task can be accomplished with the Windows'
shutdown tool as well as SysInternals'
psshutdown tool. However, the only feedback you will get is that the reboot command was issued. You will not know whether the operating system had actually shutdown, rebooted, and come back online.
Attached is a VBScript file that will perform the following steps:
- Verify server is available
- Issue a remote reboot command via WMI
- Wait for server to become unavailable or timeout
- Wait for server to become available or timeout
Execute the command with the syntax:
cscript /nologo /E:VBScript RebootWait.vbs ServerName
If you use an enterprise scheduling tool, such as TIDAL, you can easily track which reboots failed because the scheduler will reflect an exit code of zero as success, non-zero as error.
Note: The /E:VBScript option was included to cover a rare case when Windows script host does not properly map the .vbs file extension to VBScript language.
Requirements to keep in mind:
- This tool was developed and tested on Windows XP and Windows Server 2003 with Windows Script Host 5.6.
- The user running the script must have permissions to remote reboot the target server, which is usually granted via local Administrators group.
- The target server must have "Windows Management Instrumentation" and "Remote Procedure Call (RPC)" services running and set to automatically start.
- If issuing reboots over a firewall, you will need to read up on Using Distributed COM with Firewalls, since remote WMI calls use the DCOM protocol.
Feedback is welcome. Enjoy.
About Shawn Poulson / Exploding Coder
Software developer and designer
I'm a software developer in the Philadelphia/Baltimore metro area that loves web technology and creating compelling and useful applications. This blog was created to showcase some of my ideas and share my passion and experiences with others in the same field. Read more...