A: Would you like to be able to wake up a remote machine by only having to remember its name, rather than its MAC address?

In a previous FAQ, I had a Windows PowerShell script that sent the magic packet needed to wake up a machine over the network (see my FAQ "How can I easily send a magic packet...?" here at Windows IT PRO).

However, it required you to enter the MAC address of the machine.

The script below grabs a data file called wakeup.dat (update to where you have the dat file), and reads in name and MAC address pairs in the format below:

savdalscs01|00:1B:21:BD:E2:DB
savdalscs02|00:1F:D0:81:36:77
savdalbfs01|00:15:17:C4:E1:05

Save the script below as wakeup.ps1:

#param ([String]$HostName = $(throw 'No host passed, please pass hostname')) #either format
if ($args.Length -ne 1)
{
throw "No host passed, please pass host parameter"
}
$HostName = $args[0].ToUpper()

$FoundMatch = $FALSE

$data = Get-Content "d:\projects\powershell\wakeup.dat"
# write-host $data.count total lines read from file

foreach ($line in $data)
{
$linesplit = $line.split("|")
if ($linesplit.Length -ne 2)
{
throw 'Each line should have | format, e.g. host|00:23:23:00:23:23'
}
if ($HostName.CompareTo($linesplit[0].ToUpper()) -eq 0) #if they are equal
{
$FoundMatch = $TRUE

$MACStr = $linesplit[1]
$MACAddr = $MACStr.split(':') | %{ [byte]('0x' + $_)}
$MACAddrParts = $MACStr.split(':')
if ($MACAddrParts.Length -ne 6)
{
throw 'MAC address must be format xx:xx:xx:xx:xx:xx'
}
$UDPclient = new-Object System.Net.Sockets.UdpClient
$UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
$packet = [byte[]](,0xFF * 6)
$packet += $MACAddr * 16
[void] $UDPclient.Send($packet, $packet.Length)
write "Wake-On-Lan magic packet sent to $MACStr, length $($packet.Length)"
}
}

if ($FoundMatch -eq $FALSE)
{
write-host "Never found match for " $HostName
}

You can then run it as follows:

PS D:\projects\PowerShell> .\wakeup.ps1 savdalscs02
Wake-On-Lan magic packet sent to 00:1F:D0:81:36:77, length 102


To see more FAQs, please go to John Savill's FAQs page on Windows IT Pro.