Contents for Guy’s Scripting Ezine 135 – Shutdown
- This Week’s Secret
- This Week’s Mission
- Example: Script to shutdown a computer
- Windows 8 Shutdown Command
- Summary of Shutdown
A tip I picked up from playing both chess and bridge is, that once you have found a good move, look for an even better one. This week John Murray has taken my original shutdown script, and improved it.
I have found a strange paradox with scripting, in that if you have two scripts that perform the same task, the shorter script is invariably the more elegant.
To help you decide whether, or not, to accept This Week’s Mission, let us have a refresher on shutdown.exe. (Found in the Windows folder of XP, Vista and W2K3 Server).
As the name suggests, this command shuts down a machine; a lesser know fact is that you can use the /r switch instead of /s, and thus restart instead of shutdown. However, the first switch to master is the /a. Think of /a as meaning abort. If you make a mistake and shoot yourself in the foot, you have 60 seconds to type this command: shutdown /a /m \\ hostname. Sooner or later you WILL shutdown the wrong machine. After that mistake, every time you use shutdown, /a will be ingrained in your thinking.
Whenever I use the slash character to introduce a switch, I get a sack full of email saying, I should have used the minus instead of the slash. And if I use the minus sign I get a sack load of …. To tell the truth, I don’t care which convention is correct, shutdown understands -r or /r, and especially shutdown understands /a or -a.
What I do care is, that before you run this week’s script, you open cmd prompt and type plain: shutdown. My aim is make sure you appreciate all the switches, and to encourage you to try a manual walk-through before you execute John’s .vbs script.
Possible Scenarios for issuing a Shutdown command
- Suppose you get a phone call that a machine in a distant office has hung. You want to restart that machine remotely.
- Your server has installed an update and now needs a reboot. By the time you walk down the corridor and reach the server room, you would like the server to have rebooted, and be waiting at the logon screen.
- You want a script to automatically shut down computers every night.
Warning: If any of these apply, do not read any further.
You don’t understand what this means: Shutdown /a /m \\hostname
You are new to VBScript
You are of a nervous disposition
Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.
It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.
Script kindly written by Josh Murray
- Copy and paste the script below into notepad. Alternatively, use a script editor like OnScript.
- Save the file with .vbs extension e.g. Shutdown.vbs.
- Double click the .vbs file, and when you see the input box, enter the computername that you wish to shut down.
‘ Example VBScript to Shutdown computers
‘ Author Josh Murray
‘ Version 4.1 – February 2007
Dim objShell, strComputer, strInput
strComputer = (InputBox(" ComputerName to shutdown", "Computer Name"))
If strComputer <> "" Then
strInput = True
Loop until strInput = True
strShutdown = "shutdown -s -t 120 -f -m \\" & strComputer
set objShell = CreateObject("WScript.Shell")
Note 1: The script invites you type the machine name by scripting InputBox(). An alternative would be to hardcode the machine name in the script.
Note 2: Trace the logic which tests for a null string, if you forget to enter anything in the input box.
Note 3: Observe how the strShutdown variable builds the full command. Check how objShell.run executes the shutdown.
Note 4: The switches in the actual shutdown are worth further examination.
4a: -t 120 means wait 2 minutes. In the original script, John actually set -t 0 meaning don’t wait at all! The default is 60 seconds.
4b: -f means force programs to close, and don’t warn users. Yes, you can shutdown a remote machine, firewalls permitting.
If you want to save that long walk fix a hung machine, then consider the built-in shutdown command. Beware of rebooting the wrong machine! Take the time to investigate the switches available to the shutdown command. In particular become expert at the abort switch: shutdown /a /m \\ machinename.
See more about shutdown instructions.