Guy's Scripting Ezine 45 - VBS Shutdown
Contents for Guy's Scripting Ezine 45 - VBS Shutdown
As you may know, I am not a great one for disclaimers. Moreover, the whole nature of scripting is such that occasionally, mistakes just happen. In fact, the road to a successful script is littered with cast-offs. Therefore, it may come as a surprise when I say: 'This week take great care, this script could annoy people, or worse, you could shoot yourself in the foot and shutdown your own machine unexpectedly.' I will let you into a secret, I often forget the /s switch, which is vital for a shutdown and as a result my first effort does not work.
Suppose that you need to restart a machine in a distant office. By the time you walk down the corridor and reach the server room, you would like the domain controller to have rebooted.
What we are going to do is employ a VBScript to open up a DOS box, then instruct sendkeys to type in a command which will restart a computer. But first, in order to get the correct machine name, the script generates an input box where you enter the name of the machine you wish to shut down.
Before you rush off and try the script, take the time to familiarise yourself with the rich variety of 'shutdown' switches. Go to the CMD prompt on a Windows Server 2003 or XP computer and then type shutdown /? For instance, be aware that this command will either shutdown or restart. Furthermore shutdown works on either a local or a remote machine. Naturally, you need to be an administrator to execute shutdown successfully.
Here is your 'Get out of jail card'. WHEN TESTING, if the script does NOT abort the shutdown,
you have 60 seconds manually cancel by typing:
Example 1a - The Superior Version by Josh Murray
strShutdown = "shutdown -s -t 0 -f -m \\" & strComputer
Note 1: This (1a) is the advanced, smooth efficient version. The next example 1b shows the wart's and all learning progression that I initially went through.
Guy Recommends: The Free IP Address Tracker (IPAT)
Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets. IPAT cracks this problem of allocating IP addresses in networks in two ways:
For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges.
For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker
Example 1b - The Original Version by Guy Thomas
The first point that I want to make is this, in example
1b, I have deliberately left in a VBScript command that
will abort the restart. When you are ready to test YOUR script, and for
real, remove the lines underneath :
My strongest advice is master the above command FIRST, before you proceed to test shutdown in anger.
Note 1: InputBox is the VBScript method which prompts you for the computer name. This is the name of the machine to shutdown. In effect InputBox saves having to hardcode strComputer = "BigServer". My basic error checking invokes the 'if' command which in turn, checks for blank names. Did you like the 'DO ... Loop Until' command?
Note 2: Sub (routine) is this week's new command. I have experimented with recycling code. In particular we need two DOS boxes, therefore we need two commands to quit the DOS box. Look out for a Sub Function called CloseBox(). You could have used any name, for example, DosExit() would work just as well. How could you take advantage of SUB CloseBox()? The answer is that you 'Call' the sub routine, for example: Call CloseBox(). Note: 'Call Sub CloseBox()', would be wrong.
Note 3: & Chr(34) is used to generate a speech mark sometimes called a double quotes. I needed this trick to make quotes within quotes work successfully.
Note 4: You did remember to remove or 'Rem out the abort lines on the real script?
Note 5: Shutdown /t 60 adds a one minute delay before shutdown. The /c switch is purely an indulgence which adds a comment.
VBS Shutdown Challenges and Ideas
1) Add a default value to the inputbox. For example, "LocalHost" or ,"BigServer".
strComputer = (InputBox("Enter ComputerName to shutdown", "Computer Name", "LocalHost"))
2) My script employs the usual range of string and integer variables. Perhaps you would like to reduce the value of intLong from 10 seconds to 3 seconds? If so, reduce intLong from 10000 to = 3000.
3) Experiment with different values for the /c "Comment" and /t 60 switches.
4) If you are feeling bolder, why not investigate other shutdown switches, for example /r to restart instead of /s which merely shuts down the machine.
SolarWinds' Orion performance monitor will help you discover what's happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
Example 2: Script to Shutdown all Computers in a named OU
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.
Fortunately, SolarWinds have created the Free WMI Monitor so that you can actually see and understand these gems of operating system information. Take the guess work out of which WMI counters to use for scripts.
Author: Guy Thomas Copyright © 1999-2017 Computer Performance LTD All rights reserved.
Please report a broken link, or an error to: