Shutting Down Remote Computers
This is always a useful trick to have up your sleeve. After you've figured out how to do it, you can perform a number of other useful tricks with remote computers.
Shutting Down Remote Computers
Listing 31.1 shows the basic script. You are prompted for a computer name, and then that computer is shut down. This script does use WMI, so both your computer and the one you're shutting down must support WMI, and your user credentials must be accepted on the remote machine as an administrator.
Listing 31.1. Shutdown.vbs. Shuts down a remote computer by using WMI.
'get machine to shut down
Dim sMachine
sMachine = InputBox("Shut down what computer?")
'create WMI query
Dim sWMI
sWMI = "SELECT * FROM Win32_OperatingSystem WHERE" & _
"Primary = True"
'Contact specified machine
Dim oOS
Set oOS = GetObject("winmgmts://" & sMachine & _
"/root/cimv2".ExecQuery(sWMI)
'run through all returned entries
Dim oItem
For Each oItem in oOS
oItem.Shutdown
Next
You don't need to make any changes to this script to get it to run.
Shutting Down Remote Computers-Explained
This script is typical of most WMI scripts you've seen, except that it uses a method of the queried WMI instance instead of simply querying information. The script starts by getting the name of the computer you want to work with.
'get machine to shut down
Dim sMachine
sMachine = InputBox("Shut down what computer?")
Next, the script creates a basic WMI query to get all instances of Win32_OperatingSystem that are the primary operating system on the remote machine. I'm not aware of any circumstances under which this query could return more than one operating system, but it's theoretically possible.
'create WMI query
Dim sWMI
sWMI = "SELECT * FROM Win32_OperatingSystem WHERE" & _
"Primary = True"
Next, the script executes the WMI query to obtain a list of results.
'Contact specified machine
Dim oOS
Set oOS = GetObject("winmgmts://" & sMachine & _
"/root/cimv2".ExecQuery(sWMI)
Finally, because it's theoretically possible for a machine to have more than one primary operating system installed, I run through each one and use its Shutdown method to tell it to shut down. This performs a clean shutdown, meaning applications are asked to exit.
'run through all returned entries
Dim oItem
For Each oItem in oOS
oItem.Shutdown
Next
That's all there is to it. The WMI documentation says that the Win32_OperatingSystem class exposes three methods associated with rebooting and shutting down. They are
Shutdown, which I've used here. This is a basic clean shutdown. Win32Shutdown, which provides more control over the shutdown process. You can pass this method a flag indicating what type of shutdown you'd like. For example, 0 is a logoff, 1 is a shutdown, 2 is a reboot, and 8 is a power off. Add 4 to any value to force the action, making 6, for example, a forced reboot. Reboot, which is a simple, clean reboot cousin of Shutdown.
You can modify the example script here to use any one of these. For example, to implement a forced shutdown:
'run through all returned entries
Dim oItem
For Each oItem in oOS
oItem.Win32Shutdown(5,0)
Next
I'm often asked if there's a way to automatically log users off their computers at a specific time; many organizations want to use this capability to better manage software deployments as well as keep workstations more secure. There is a way! Just use Win32Shutdown with the appropriate parameter and run the script from Task Scheduler. Provided the script runs under the credentials of a domain administrator, it should be able to force all machines in the domain to remotely log off, if desired. Just provide it with a list of computers, either from a file or from the results of a domain query.
|