Downloads
20508.zip

Windows Shell Scripting

Wake on LAN (WOL) lets you remotely start desktop PCs or servers if they have a network card and motherboard that support this technology. The WOL-enabled network card turns on the machine when it receives a special WOL packet addressed to its media access control (MAC) address. This card remains active even when users power down the machine.

Systems administrators often use WOL to activate PCs before software rollouts. However, you can use WOL for other tasks. For example, you can use WOL in scripts to turn on desktop PCs or servers after power outages or other shutdown events. You can also use WOL in scripts to turn on all desktop PCs just before the workday starts. This practice helps improve productivity because users don't have to wait for their machines to boot up each morning.

I wrote a script, WakeUpCall.bat, that uses WOL to wake up machines remotely. You can find this script in the Code Library on the Windows Scripting Solutions Web site (http://www.winscriptingsolutions.com). WakeUpCall.bat uses Gammadyne Software's wol.exe, a freeware utility that sends WOL packets. To download wol.exe, go to http://www.gammadyne.com/cmdline.htm. Let's look at how WakeUpCall.bat uses the WOL packets to wake up machines and what you need to do to use this script.

Planning the Setup
I designed WakeUpCall.bat to execute on a centrally located machine that's always running. That machine then wakes up all the other machines. However, WOL packets don't readily pass through routers. If you're on a large network that has router-separated segments, you might need to have WakeUpCall.bat run in several locations.

You can schedule WakeUpCall.bat to run on a local machine or from a network share. Task Scheduler lets you schedule remote scripts, so if you plan to schedule the script to run in multiple locations, pointing to a copy on a network share is best. That way, if you need to make modifications, you need to edit only one script and one set of input files.

Obtaining the Input
To use WakeUpCall.bat, you need to obtain the MAC address for each machine you plan to wake up remotely. You can use the Getmac utility in the Microsoft Windows NT Server 4.0 Resource Kit or the script PingandArp.bat that I discussed in "Real-World Scripting: Document Network Data About Your Class C Subnet," February 2001.

After you've obtained the MAC addresses, you need to provide the script with those addresses. Using a file to provide input to a script is good practice. If you use an input file, you don't have to hard-code that information. Hard-coding data makes scripts less portable and more prone to errors. Each time you edit the script, the potential for accidentally changing crucial code exists. Thus, you should write your code so that all the main input data comes from outside the script.

For this reason, you need to create a Comma Separated Values (CSV) input file that contains the computer name and MAC address of each machine you plan to wake up remotely. If you plan to run the script from multiple locations, you can include a third value that identifies the network segment in which the machine is located. Figure 1 contains a sample input file that lists all three values—computer name, MAC address, and segment, respectively—for each machine.

Although using an input file is good practice, it isn't foolproof because you still need to hard-code the path to the input file in the script. Scripts that point to nonexistent input files often continue to run and produce unexpected results. Thus, another good practice is to test for the input file's existence, which WakeUpCall.bat does.

Manipulating the Input
Data from an input file often requires processing to get it in the right format for use later in the script. Such is the case with the MAC addresses in the CSV file. As Figure 1 shows, the MAC addresses have hyphens. However, wol.exe requires MAC addresses without hyphens. Thus, WakeUpCall.bat uses the code

Set MAC=%MAC:-=%
Set MAC=%MAC: =%

to remove the hyphens as well as any leading or trailing spaces. The code assigns the resulting MAC address to the %MAC% variable.

In addition to formatting the MAC addresses, WakeUpCall.bat makes sure the addresses have the correct number of characters (i.e., 12) and that those characters are valid hexadecimals (i.e., a through f and 0 through 9). As the code excerpt in Listing 1 shows, WakeUpCall.bat uses the FindStr /v /i command to check each MAC address for these two characteristics. The FindStr command searches text for specified strings. The /v switch tells FindStr to output any text that doesn't match (in this case, any incorrect MAC address). The /i switch tells FindStr to perform a case-insensitive search.

The code that follows the /i switch contains the strings for which FindStr is searching. If you want FindStr to search for multiple strings, you enclose the strings in quotation marks. By default, FindStr conducts searches with a regular expression, which includes special syntax characters such as the caret (^) and the dollar sign ($). The caret calls for a match at the beginning of the string, and the dollar sign calls for a match at the end of the string. The code \[a-f0-9\] represents a character that must reside in the a though f range or the 0 through 9 range. Because the MAC addresses have 12 characters, the code \[a-f0-9\] appears 12 times.

After you've specified the strings FindStr is to look for, you need to provide the text that FindStr is to search. In WakeUpCall.bat, the %MAC% variable contains this text. However, FindStr doesn't accept input from variables; it accepts only input from files or output from commands brought in with the pipe (|) symbol. The pipe symbol captures command output that would ordinarily be displayed on screen and uses that output as input for the next command. Thus, to feed the MAC address in the %MAC% variable to FindStr, the script uses the Echo command to display the MAC address. The pipe symbol then sends the MAC address to FindStr.

Pinging the Machine
Before WakeUpCall.bat sends a WOL packet to a machine, the script pings that machine to see whether it's already running. If the machine fails to reply, the script sends the packet.

The script's pinging technique requires your system to resolve the computer name into the IP address. Thus, you'll need to register all desktop PCs with WINS or DNS, or you'll need to set up the PCs so that they support broadcasting. Most organizations use one or both of these resolution strategies, so you probably won't have to worry about this step.

If these resolution strategies don't apply to your organization, you can remove the ping test from the script. The WOL packet is small, so sending it to a machine that's already turned on produces no negative effects. Even if your organization uses these resolution strategies, you might want to remove the ping test if you need to wake up numerous machines quickly.

Using the Script
WakeUpCall.bat works on machines running Windows 2000 Professional, Win2K Server, NT Server 4.0 Service Pack (SP6), or NT Workstation SP6 or SP5. Here are the steps to get the script working in your environment:

  1. Create an input file called mac_addresses.csv. In this CSV file, enter the computer name and MAC address for each machine you want to wake up remotely. If you want to specify the machine's segment, include a third value. The descriptors you use for the segments are up to you. The only requirement is that the descriptors can't contain any spaces. For example, if your three segments are in three separate buildings, you might use Bldg1, Bldg2, and Bldg3 as the descriptors.


  2. Copy mac_addresses.csv, wol.exe, and WakeUpCall.bat to a common folder location. Share this folder if you plan to run the centrally located script in multiple locations.


  3. In WakeUpCall.bat, configure the location of the source files in a local path or a Uniform Naming Convention (UNC) path, if shared.


  4. Schedule WakeUpCall.bat to run. If you plan to run WakeUpCall.bat in multiple locations, use the command
  5. WakeUpCall.bat SegDescriptor
    where SegDescriptor is the descriptor for the particular segment in which you want to run the script. For example, you would use WakeUpCall.bat Bldg1

    to wake up all the Bldg1 machines listed in the CSV file. If you don't have router-separated segments and you want to wake up all the machines with one script, use the command

    WakeUpCall.bat All This command prompts the script to ignore the third value, if it exists. As a result, the script wakes up every machine listed in the CSV file.

Troubleshooting
Machines that don't respond to WOL packets typically have an incorrect MAC address, have an incorrect BIOS or hardware configuration, or reside on a network segment that isn't receiving the WOL packet. Check for these possibilities if you encounter a machine that won't wake up.