Q: How can I easily wake up a remote machine?
Wake up with PowerShell.
John Savill
November 22, 2011
2 Min Read
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:DBsavdalscs02|00:1F:D0:81:36:77savdalbfs01|00:15:17:C4:E1:05
Save the script below as wakeup.ps1:
#param ([String]$HostName = $(throw 'No host passed, please pass hostname')) #either formatif ($args.Length -ne 1){ throw "No host passed, please pass host parameter"}$HostName = $args[0].ToUpper()$FoundMatch = $FALSE$data = Get-Content "d:projectspowershellwakeup.dat"# write-host $data.count total lines read from fileforeach ($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:projectsPowerShell> .wakeup.ps1 savdalscs02Wake-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.
About the Author
Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.
You May Also Like