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.
The Problem
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.
My Solution
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
- Success!
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.
Attachment | Size |
---|---|
RebootWait.zip | 1.23 KB |
- Login to post comments
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...
Comments
Nice
Great Script, just what I was looking for. Now to add to check if any users logged into server prior to reboot, send messages with a countdown, shut off remote logins prior to reboot - run your reboot script - check for any post reboot activities like cleanup, updates etc - perform is any, re-enable logons. Will post final product!
I'm glad you found it useful!
I'm glad you found it useful! Any feedback is welcome.
Is this also compatible with
Is this also compatible with Windows 7? I think I would need some remote computer assistance :(
Thanks - just what I needed!
Been a while since you posted this script, but thanks! It's exactly what I was looked for. Long live google.
Been a while since you posted
Been a while since you posted this, but you're welcome!