Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


January 2005

Make Remote Shutdowns a Snap

This administrator-friendly script boosts Windows' shutdown capabilities
RSS
Subscribe to Windows IT Pro | See More Task Automation Articles Here | Reprints
Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

Although Microsoft's tools become more administrator-friendly with each new version of Windows, most still have limitations. An example is the venerable Shutdown utility, which Microsoft finally integrated into the OS in Windows XP and updated for Windows Server 2003. Shutdown lets you reboot, shut down, or power off a PC. However, Shutdown has limitations that make it weak for enterprise use—for example:

  • Shutdown provides no testing capability. To determine whether you can shut down a system, you must actually shut it down before you run the Shutdown utility.
  • Shutdown lets you specify only one computer to be shut down each time you run it.
  • Shutdown doesn't let you authenticate to a remote system that requires public key infrastructure (PKI)—level security.
  • Shutdown doesn't let you specify a privileged account to use for performing a shutdown.

I designed a shutdown utility—the VBScript StopComputer script—to handle enterprise-type situations that are beyond the Windows Shutdown tool's abilities. StopComputer uses the Windows Management Instrumentation (WMI) Win32_OperatingSystem class's Win32Shutdown method, which lets you shut down, reboot, or log off a computer from a script. I walk you through the StopComputer utility to give you a basic understanding of how it works and how to use it. StopComputer works with Windows 2000 and higher in the OS's shipped configuration. You can also use it with Windows NT Server 4.0 systems if you install the WMI extensions on the systems that you want StopComputer to shut down. You must have Windows Script Host (WSH) 5.6 or later installed to run the utility.

StopComputer has a few limitations. It doesn't let you directly specify logged reasons for shutdowns, and it doesn't send network messages on its own. Also, for safety reasons, you can't run the utility against the local computer (i.e., the system on which StopComputer is running). As a batch-processing script, though, StopComputer is easy to integrate with other tools that perform those tasks. Alternatively, you could customize StopComputer by adding components that provide logging (to specify reasons for shutdown) and network messaging.

StopComputer Preliminaries
First, download all the code for StopComputer at the Windows IT Pro Web site. Go to http://www.windowsitpro.com, enter InstantDoc ID 44710 in the InstantDoc ID box, then click Download the Code. Save stopcomputer.wsf and stopcomputer.cmd in the same folder. The .cmd file handles input and output redirection for the .wsf file so that you can execute the script simply by entering

stopcomputer

at the command line. You don't need to specify the .cmd extension because cmd.exe finds .cmd files before .wsf files in path searches.

Before you start using StopComputer in your organization, you'll need to prepare your users for a possible planned or emergency shutdown of their systems. Discuss administrative shutdown events with users ahead of time to avoid panic should you have to perform a mass shutdown of their systems.

You should also convey your shutdown policies to users; for example, you might perform standard shutdowns at night for security, economy, or maintenance and emergency shutdowns of Internet-accessible systems at the first sign of virus activity or a network break-in. When users are aware of shutdown policies, they're more likely to interpret a sudden loss of network access or a remote system reboot as an attempt to protect them instead of a random failure that's the IT department's fault.

Another way to prepare users for a shutdown is to perform a shutdown drill on the network. You should also activate autosave and autorecovery features in software. For example, you can set Microsoft Excel to autosave recovery information by clicking Tools, Options, Save Autorecovery. Such features dramatically reduce the likelihood of data loss even during a forced reboot with live users.

Finally—and this is crucial—warn your users when you need to shut down a critical system quickly. Send a message over the network or, at the very least, make a companywide announcement about the shutdown to your network's users.

4 Essential Actions
You can use StopComputer to perform four actions on a system: Conduct a test shutdown, reboot the computer, shut down the computer but leave it powered up, and power off the computer. To perform a test shutdown, run the Stopcomputer command with the /w (what if) switch; this lets you test whether you can perform an actual shutdown on a system.

The ability to conduct a test shutdown is important because it lets you identify problems with system access ahead of time without actually shutting down computers. For example, to conduct a test shutdown on remote servers TS01, TS02, and TS03, you'd enter the command

stopcomputer /w ts01 ts02 ts03

If the test shutdown succeeds, the command outputs on screen the system name followed by "SUCCESS"; if the test shutdown fails, the command outputs the system name followed by "FAILED" and an error code. (You can change the amount of detail in the messages by specifying either the /v+ or /v- switch, which I discuss later.)

To reboot a computer, specify the /r (reboot) switch. For example, to reboot the DC01 domain controller (DC), run the command

stopcomputer /r dc01

To shut down a computer but leave it powered up, specify the /s (shutdown) switch:

stopcomputer /s dc01

Finally, to completely power off a system, run the StopComputer command with the /p (power-off) switch:

stopcomputer /p dc01

When you specify /w to perform a test shutdown, the script ignores the /r, /s, and /p options. If you specify more than one shutdown option (/r, /s, or /p), the script alerts you to your mistake and exits.

Sometimes shutdowns don't work as intended. A hung application can halt the shutdown process, as can an open and unattended user session that has unsaved data. If you need to force applications to close, you can run the StopComputer command with the /f (force) switch, which forces a system to shut down without notifying any applications that are running. For example, to force a shutdown of two terminal servers, TS1 and TS2, you'd run the command

stopcomputer ts1 ts2 /s /f

Options Galore
StopComputer's option switches let you perform shutdowns in different ways. Figure 1 lists these options. Like most batch-processing tools, StopComputer uses as input data either command-line parameters (e.g., computer names) that you specify to be shut down or, if you don't enter any parameters, the standard input stream (e.g., a text file containing a list of computer names). For example, if you want to forcibly shut down some computers and enter the names interactively, you'd issue the command

stopcomputer /s /f

After you enter the command, you'll see a blank line under it. Here, you can start typing in the names of the computers you want to shut down, pressing Enter after each one. When you're finished entering names, simply press Ctrl+C twice to exit the script.

Alternatively, you can run the command against a saved list of computers. For example, the kiosks.txt file contains the names of kiosks you need to power off late on Friday. To use StopComputer to power off the kiosks contained in the kiosks.txt list, run the command

stopcomputer /p /f < kiosks.txt

Even better, if you have a tool that generates a filtered computer list and sends it to standard output, you can feed this tool straight into StopComputer. Suppose you have a script—Getwebservers.cmd—that dynamically generates a list of your public Web servers, and you want to forcibly reboot these Web servers. To do so, run the command

getwebservers | stopcomputer/r /f

Handling Permissions
If you have the commendable habit of running in an account with limited privileges, the Shutdown utility won't work without wrapping it through runas.exe (i.e., using the Runas command to execute the Stopcomputer command). Even worse, if you have some isolated systems with distinct accounts and passwords, you can't handle their shutdowns remotely at all; you face yet another penalty for using good security practices.

The StopComputer script accommodates permissions restrictions to a degree. For example, if your current account lacks sufficient privileges to shut down a remote system, you can still perform the shutdown by specifying the username and password of the user whose system you want to shut down. To do so, run the StopComputer command and specify the /u (username) switch followed by the username and password of the user whose remote system you want to shut down, in the format username:password, like this:

stopcomputer /u alibaba:sesame

Security-conscious systems administrators might not approve of this method, which displays username and password information to anyone who happens to be passing by your computer screen. If you're concerned about revealing your password, you can simply enter only the username on the StopComputer command; StopComputer then prompts you to enter a password that isn't displayed.

Controlling Output and More
Console command output for StopComputer varies depending on how you intend to view or use the results. By default, if the script successfully accesses a system, StopComputer prints the computer name to standard output followed by the word "SUCCESS". If computer access or shutdown fails, StopComputer writes the computer name to standard error followed by the word "FAILED" and the error code that the OS returned. Listing 1 shows the error-handling portion of the StopComputer script. You can often look up the meanings of short error codes by running the command

net helpmsg

If you want to display the full message text displayed during system shutdown attempts, you can specify the /v+ (verbose) switch on the StopComputer command. StopComputer then appends available error descriptions to all messages that are displayed on screen.

If you want to stop the script from displaying the SUCCESS or FAILED message, specify the /v- (nonverbose) switch. Doing so tells StopComputer to print succeeding systems to standard output and failing systems to standard error. Be aware that if you specify the /v- switch, you won't be able to visually identify which systems StopComputer has successfully shut down and which shutdowns have failed; if neither standard error nor standard output is redirected to another command or a file, both will appear merely as bare names in the console window.

Two more options are worth mentioning because you might want to tweak them. The /a (authentication) switch lets you specify the WMI authentication level as a number from 0 to 6. StopComputer uses the Pkt (4) authentication level to ensure that it will always work against Windows XP and newer systems, which have more stringent security.

The /d (delay) switch specifies a delay time in seconds between each call to shut down a remote system; by default, StopComputer allows a 2-second delay. The delay gives you time to you halt the script (by pressing Ctrl+C) if you see a mistake (e.g., an incorrect system name) after you've executed the StopComputer command.

A Powerful Tool
StopComputer extends the usefulness of the Windows Shutdown utility by enabling you to shut down multiple systems remotely. Additionally, StopComputer lets you perform test shutdowns on systems, a capability you'll find useful for preparing for future shutdowns and anticipating potential problems before a forced emergency shutdown. You can enhance StopComputer to report the reasons for shutdowns and send network messages to users or let the user simply type "Quit" to exit interactive mode instead of having to press Ctrl+C twice. However you choose to use StopComputer, you'll surely find that it's a practical addition to your administrator's toolkit.

Project Snapshot: How to
PROBLEM: Microsoft's Resource Kit utility, Shutdown, has limitations that hamper its usefulness in an enterprise setting. The StopComputer script removes those limitations, so that you can easily shut down systems remotely.
WHAT YOU NEED: Windows 2000 or higher, or a Windows NT Server 4.0 system that has the Windows Management Instrumentation (WMI) extensions installed; Windows Script Host (WSH) 5.6 or later
DIFFICULTY: 2 out of 5
PROJECT STEPS:
  1. Define shutdown policies and convey them to users.
  2. Download the StopComputer script from the Windows IT Pro Web site.
  3. Conduct a test shutdown.
  4. Run StopComputer, specifying the computers you want to shut down and other shutdown options.



End of Article



Reader Comments
When I enforce password protected screen savers through GPO across the domain this script errors out. Is there any way to handle the locked screen saver with this tool or am I looking in the wrong place?

Wayne Geils January 20, 2005


Very usefull

Telhado February 02, 2005 (Article Rating: )


Very nice Script, thanks for sharing... I have a simple question, using WinXP SP2 with active firewall what port should be opened in order for this and other scripts to work. Thanks again.

gfilipe February 17, 2005 (Article Rating: )


You must log on before posting a comment.

If you don't have a username & password, please register now.




Learning Path Get acquainted with Windows Management Instrumentation (WMI) scripting
"Windows Management Instrumentation: The Systems Administrator’s Apprentice"


Learn how to send system-shutdown messages to users
"Real-World Scripting: Send Shutdown Warning Messages to Users"


To learn more about the WMI authentication levels and the meaning of the numeric values
"Setting the Default Process Security Level Using VBScript"


Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of July 21, 2008

An often irreverent look at some of the week's other news, including an iPhone 3G defeat, 180 million copies of Windows Vista in the wild, Microsoft earnings some more Yahoo silliness, Wii vs. Xbox 360, EU vs. Intel, AMD ousts its CEO, and so much more ...

The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...

The iPhone as a Mail Device

An Exchange administrator and self-proclaimed "Windows Mobile device wrangler" gives you the scoop on how well the iPhone 3G works for enterprise email, and points out some surprising omissions in Apple's latest release. ...


Task Automation Whitepapers Continuous Data Protection and Recovery for Microsoft Exchange

Protecting (You and) Your Data with Exchange Server 2007

A Preliminary Look at Deployment Plans for Microsoft Windows Vista

Related Events Check out our list of Free Email Newsletters!

Task Automation eBooks Spam Fighting and Email Security for the 21st Century

A Guide to Windows Certification and Public Keys

Keeping Your Business Safe from Attack: Patch Management

Related Task Automation Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

Shortcut Guide to SQL Server Infrastructure Optimization
With right tools and techniques, you can have a top-performing SQL Server infrastructure without having to cram your data centers so that they're overflowing. Download this eBook to learn how.

WinConnections Conference Fall 2008
Don’t miss the premier event for Microsoft IT Professionals in Las Vegas, November 10-13. Register and book your room by August 25 and receive a FREE room night (based on a three night minimum stay).

Become a fan of Windows IT Pro on Facebook!
Join us on Facebook and be a fan of Windows IT Pro!

Continuous Data Protection and Recovery for Exchange
Read this white paper to learn about Continuous Data Protection (CDP), Exchange 2007's local continuous replication and cluster continuous replication features.

Rev Up Your IT Know-How with Our Recharged Magazine!
The improved Windows IT Pro provides trusted IT content with an enhanced new look and functionality! Get comprehensive coverage of industry topics, expert advice, and real-world solutions—PLUS access to over 10,000 articles online. Order today!

Tips to Managing Messaging
Discover three fundamental mail and messaging management services - security, availability and control services - and how you can implement them in a Microsoft-centric mail and messaging environment.

Get It All with Windows IT Pro VIP
Stock your IT toolbox with every solution ever printed in Windows IT Pro and SQL Server Magazine plus bonus Web-exclusive content on hot topics. Subscribe to receive the VIP CD and a subscription to your choice of Windows IT Pro or SQL Server Magazine!



Drag & Drop Data Mapping Tool
Try this award-winning data mapping, & transformation tool that supports multiple databases, flat files, Web services, EDI, Excel 2007, & more! Free trial for 30 days!

Overcome bloated Windows file systems
Crossroads FMA delivers powerful yet inexpensive data migration

Bandwidth Monitoring Tool from SolarWinds
Identify largest bandwidth users in seconds. Get the free download now.

Speed Deployment of Vista and Microsoft Office
Read this white paper to learn how you can maximize your Vista and Office investments while lowering costs and increasing efficiency.

Integrated Virtualization Done Right
Download this white paper on server virtualization to begin improving resource utilization and lowering operating costs.

Order Your Fundamentals CD Today!
Gain an introduction to Exchange, learn server security requirements, and understand how unified communications can play a role in your messaging strategies with this free Exchange CD.

KVM over IP Solutions
Learn about a KVM over IP solution that is specifically designed to meet the needs of the distributed IT environment.
Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound
IT Library Technical Resources Directory Connected Home Windows Excavator SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing