Shutting Down a Computer

Microsoft® Windows® 2000 Scripting Guide

Computers occasionally need to be removed from the network, perhaps for scheduled maintenance, because the computer is not functioning correctly, or to complete a configuration process. For example, if a DHCP server is handing out erroneous IP addresses, you might want to shut the computer down until a service technician can be dispatched to fix the problem. If you suspect that a security breach has occurred, you might need to shut down certain servers to ensure that they cannot be accessed until the security issue has been resolved. Some configuration operations (such as changing a computer name) require you to restart the computer before the change takes effect.

A computer can be shut down using the Win32Shutdown method, specifying 1 as the parameter value. Passing 1 as a parameter indicates that the computer should be shut down. Other parameters specify different actions, such as restarting the computer or logging off the user. For a complete list of parameter values, see Table 8.21.

For computers that support the ability to be programmatically powered off, you can specify 8 as the parameter value. Passing 8 as the parameter shuts down the computer and also turns off the power.

Scripting Steps

Listing 8.24 contains a script that shuts down a computer. To carry out this task, the script must perform the following steps:

  1. Create a constant named SHUTDOWN and set the value to 1.

    This constant specifies that the computer should be shut down.

  2. Create a variable to specify the computer name.

    In Listing 8.24, the computer name is set to "." which will result in the local computer being shut down. To shut down a remote computer, simply set the variable to the appropriate computer name.

  3. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."

  4. Use to the ExecQuery method to return a collection consisting of all instances of the Win32_OperatingSystem class.

    The Shutdown privilege must be specified as part of the GetObject moniker. If it is not, the script will fail and the computer will not be shut down. The Shutdown privilege is required to shut down a computer by using WMI. However, including the privilege within the moniker does not ensure that the script will succeed. In addition to inclusion of the privilege within the moniker, the user under whose credentials the script is running must also have permission to shut down the computer.

  5. For each operating system in the collection, use the Win32Shutdown method, specifying the constant SHUTDOWN (with the value 1) for the method parameter.

To shut down, but not power off, the computer, use the value 1 as the method parameter. Other values will result in other actions. For example, the value 0 will log the user off but will not shut down the computer.

Listing 8.24 Shutting Down a Computer

  
1
2
3
4
5
6
7
8
9
Const SHUTDOWN = 1
strComputer = "."
Set objWMIService = GetObject("winmgmts: {(Shutdown)}" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
 ObjOperatingSystem.Win32Shutdown(SHUTDOWN)
Next

When the script in Listing 8.24 runs, the computer is shut down, the user is logged off, all processes are stopped, and the screen displays the message:

" It is now safe to shut down your computer."

However, the computer remains powered on unless manually turned off.