Mr. Roboto

Shut Down, Already

Use this script to shut down or reboot a group of computers.

I never tire of scripts and utilities that can shut down or restart Windows systems en masse. They're useful during scheduled maintenance windows, for shutting down client computers for weekends or holidays (think of the power you'll save) or for running on your very last day of work just for fun (just kidding -- I don't condone that in any way).

The Windows Shutdown.exe utility is truly awesome for shutting down and restarting even remote computers. Run Shutdown -i and you'll be working with a nice graphical user interface (see Figure 1) where you can specify multiple computers. Copy and paste names from a text file, for example, and you can reboot an entire Web farm with just a few clicks. If you use it entirely from the command-line, however, you can't give Shutdown.exe a list of computer names or have it grab names from an Active Directory organizational unit (OU). This limits its usefulness in some scenarios.

Enter my custom tool of the month: ShutdownRestart. You can give it a list of computers, specify an action (which must be shutdown, restart, logoff or poweroff) and even have the tool force that action (thus forcing applications to close, if necessary). Run shutdownrestart /list:computers.txt /ping /verbose /action:restart /force, for example, and you'll get the output shown in Figure 2 as the specified computers dutifully reboot themselves.

DownLoad

Download this month's tool from www.ScriptingAnswers.com/roboto/col6.zip

Windows Shutdown GUI
[Click on image for larger view.]
Figure 1. The GUI for Windows Shutdown.exe is solid, but somewhat limited.

That's not all. Instead of /list, you can specify /container, as in /container:Sales, which will target every computer account in your domain's Sales OU (they always forget to shut down for the weekend, don't they?). You can also specify the /log argument, as in /log:C:\failed.txt. This will create a log file named C:\failed.txt, which includes the names of all the computers that the tool was asked to target, but couldn't reach or for which it didn't have sufficient permissions. That creates a list of machines you'll need to deal with manually -- or you can make some adjustments and feed the list back to the tool using the /list argument.

Note that the /action:poweroff option can only work on computers whose motherboards specifically implement a Windows-commanded power-down. Most new systems do this. If your systems don't, Windows will just quit to the "It's now safe to power down your system" screen without actually shutting down.

Output of shutdown script
[Click on image for larger view.]
Figure 2. The resulting output of this month's shutdown script.

Hopefully this tool will give you some additional control over your environment, and make the next mass-restart or mass-shutdown of a group of computers just a bit easier for you.

About the Author

With more than fifteen years of IT experience, Don Jones is one of the world’s leading experts on the Microsoft business technology platform. He’s the author of more than 35 books, including Windows PowerShell: TFM, Windows Administrator’s Scripting Toolkit, VBScript WMI and ADSI Unleashed, PHP-Nuke Garage, Special Edition Using Commerce Server 2002, Definitive Guide to SQL Server Performance Optimization, and many more. Don is a top-rated and in-demand speaker and serves on the advisory board for TechMentor. He is an accomplished IT journalist with features and monthly columns in Microsoft TechNet Magazine, Redmond Magazine, and on Web sites such as TechTarget and MCPMag.com. Don is also a multiple-year recipient of Microsoft’s prestigious Most Valuable Professional (MVP) Award, and is the Editor-in-Chief for Realtime Publishers.

comments powered by Disqus

Reader Comments:

Tue, Mar 20, 2007 CM OR

Fixed the problem by moving the On Error Resume Next on line 202 up one line.

Tue, Mar 20, 2007 CM OR

Nice script works great until it encounters an error with the RPC server being unavailable. The script terminates immediately when it hits this. Ran it against a single machine having this issue and it gives:

Pinging pcname
Success
Connecting to WMI on pcname
*** Error connecting to WMI on pcname
The RPC server is unavailable.
C:\sr.wsf(202, 10) Microsoft VBScript runtime error: Object not a collection

So it looks like it needs to catch this error if thats possible.

Wed, Jul 12, 2006 Jola Montreal

The following Command does not work as expected.
WMI service is running on the remote and local machines. Also, I am admin on local and remote machines. Have an idea of the problem ?

cscript ShutdownRestart.wsf /list:List.txt /verbose /log:c:\Failed.txt
/action:shutdown /force


Console output:

Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. Tous droits réservés.

Reading names from file List.txt
Connecting to urba
Connecting to WMI on urba
*** Error connecting to WMI on urba
Accès refusé.

Connecting to admin
Connecting to WMI on admin
*** Error connecting to WMI on admin
Accès refusé.

Connecting to jtheoret
Connecting to WMI on jtheoret
*** Error connecting to WMI on jtheoret
Accès refusé.

Command completed.

Thu, Jul 6, 2006 Robert Annapolis, MD

Also if you have spaces in your OU names you will need to add quotes before Branch and after Company.

Thu, Jul 6, 2006 Robert Annapolis, MD

I wanted to add how to specify OU's that were not located in the root of AD and also how to stamp the date on the log file.

This is a simple batch file that I use to run as a scheduled task for a OU that is located under Domain/Company/Computers/Branch and it places a log file with the date stamped in a folder for review the next day.

cscript shutdownrestart.wsf /container:Branch,OU=Computers,OU=Company /recurse /ping /verbose /action:logoff /force /log:"c:\shutdownrestart\Logs\Branch Logoff Failed - %date:~4,2%-%date:~7,2%-%date:~12,2%.txt"

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above

Redmond Tech Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.