A VBScript Wake On LAN' Project
A VBScript Wake On LAN' Project
by Jeff Harbert Originally posted at http://blog.jeffharbert.com/index.php/2009/03/avbscript-wake-on-lan-project/ I was working at home one weekend recently, performing some scheduled server maintenance. We have mostly HP servers where I work, and they have this nice feature built into them called Remote Insight. Its a card thats plugged into the motherboard of the server. This card has an ethernet port on it. You can connect to the RI card over this ethernet port and use it as a sort of virtual KVM. Its a fantastic feature, letting you control a server remotely, shutting down or restarting if you need to, and, since its essentially attached to the console, you can even get into the BIOS should the need arise. That is, when it works. The RI cards are pretty reliable, but every once in a while theyll disconnect you and make you wait a 30 minute timout period before it resets the connection. Not a big deal if youre in the building, but a much bigger problem if youre offsite, as I was this particular weekend. Anyway, I had to shut down a server briefly, and that servers RI card decided that right then would be a good time to drop its connection. I had to drive in to work on a Sunday just so I could press the power button on the server. I was good and annoyed, let me tell you. The next work day, I decided to give myself a backup method of turning on a server. My tool of choice? A wonderful piece of technology called Wake on LAN. I use a tiny application called WOL.EXE for this. Open a command prompt, type wol 112233445566? (that last part being a MAC address) and WOL.EXE sends out whats called the magic packet to wake up the computer (or server) with the correct MAC address. This would have suited my needs perfectly, if only Id had the MAC address of the server in question. Hmm, I feel some scripting coming on. Lets not limit this to servers. Lets cover all the computers in an entire domain, ok? Ok. The first thing to do is gather all the computer names from your Active
Directory domain. I found the perfect script over at Microsofts Script Center Repository. Their website doesnt support direct hyperlinking, so I cant link to the script. Ill just give it to you here:
On Error Resume Next Const ADS_SCOPE_SUBTREE = 2 Set objConnection = CreateObject(ADODB.Connection) Set objCommand = CreateObject(ADODB.Command) objConnection.Provider = ADsDSOObject objConnection.Open Active Directory Provider Set objCommand.ActiveConnection = objConnection objCommand.Properties(Page Size) = 1000 objCommand.Properties(Searchscope) = ADS_SCOPE_SUBTREE objCommand.CommandText = _ SELECT Name FROM LDAP://dc=fabrikam,dc=com WHERE objectCategory=computer Set objRecordSet = objCommand.Execute objRecordSet.MoveFirst Do Until objRecordSet.EOF Wscript.Echo objRecordSet.Fields(Name).Value objRecordSet.MoveNext Loop
Quick side note: The script references an example AD domain as LDAP://dc=fabrikam,dc=com. The AD domain where I work is actually a subdomain of our public domain name, so I have to add to the DC path above. Like so: LDAP://dc=subdomain,dc=fabrikam,dc=com. Note how I added a third DC section. Thats how you can reference a subdomain name in an LDAP path. The script as-is will output the information it gathers to the console, but I wanted to capture it in a text file. The easiest way to do this without modifying the script is to run the script from within Microsofts Scriptomatic tool, with Scriptomatic set to display the results in a text file. Boom, nice and easy. I use Scriptomatic a lot this way. Paste the script into Scriptomatic, change the LDAP path in the script to your own AD domain, and click Run. After a second or two youll see a text file pop open with a list of all the computers and servers in your domain. Save this text file somewhere safe. I saved mine with the name workstations.txt. Now we need to fetch all the MAC addresses of the computers listed in that text file we just saved. Well use another script to do that, using the text file we just saved as the input for the script.
We want to loop through the text file line by line grabbing MAC addresses as we go
Do Until objFile.AtEndOfStream strComputer = objFile.ReadLine
If we can ping the workstation, grab the MAC address and save it in a text
file If PINGFlag = True Then wScript.Echo strComputer & pings Dim objShell,objExec Set objShell=CreateObject(wscript.shell) strCommand=nbtstat -a & strComputer Set objExec=objShell.Exec(strCommand) Do While objExec.StdOut.AtEndOfStreamTrue strLine=objExec.StdOut.ReadLine If InStr(strLine,MAC Address) Then arrFields = Split(strLine, ) strMAC1 = arrFields(7) strMACAddress = Replace(strMAC1, -, ) strCompEntry = strComputer & , & strMACAddress Set objFile2 = objFSO.OpenTextFile(C:\scripts\Fetch MAC Addresses\MAC_Addresses.txt, ForAppending) objFile2.WriteLine strCompEntry objFile2.close End If Loop Else
The script reads the first workstation name, sees if the workstation responds to ping, runs the nbtstat command against the workstation, grabs the MAC address by monitoring the stdout of the nbtstat command, then saves the workstation name and MAC address (sans hyphens) to a second text file. Also, if the workstation does not respond to ping, it saves that workstation name to a third text file. We can use this third text file at a later date as the input file of this same script so that, over time, we can gather all the MAC addresses in our domain. There you have it, all the MAC addresses in your domain. For myself, I now have all the information I need to wake up any workstation or server on my domain, but lets not stop here. Lets make it easy to use the MAC addresses we just gathered. Heres a script that prompts you for a workstation name, searches for the MAC address in the second text file, then automatically sends the WOL packet to wake up the workstation. Lets dim our variables
dim fname, objFSO, objFile
This opens an input box into which well enter our workstation name
fname=InputBox(Which computer do you want to wake up?) fname = UCase(fname)
Whatever array field has the workstation name, the MAC address
will be in the next field strPC=arrFields(i) strMAC=arrFields(i+1)
If we do, lets grab the MAC address and send the WOL signal
Dim objShell,objExec Set objShell=CreateObject(wscript.shell) strCommand=C:\scripts\wol.exe & strMAC Set objExec=objShell.Exec(strCommand) Lets get a visual confirmation that we sent the WOL signal wscript.echo Wake on lan command sent to MAC address & strMAC
The loop will terminate here once Y=1, otherwise itll keep going
Loop
And there you have it. I hope you find this helpful.