Wake On LAN (WOL) – PowerShell style

Working on another project and I needed a way to wake up an offline server from within my PS script.  I found this genius blog post and converted it to a script.

Genius blog post - The PowerShell Guy : PowerShell Wake-on-lan script

I saved the below text as send-wol.ps1 for re-usability.  This code came from The PowerShell Guy blog, I cannot claim credit !!!!  I just made it a script.  Mainly, I combined two snippets from his post and added the args reference.

 $mac = [byte[]]($args[0].split('-') |% {[int]"0x$_"}) 
  
 $UDPclient = new-Object System.Net.Sockets.UdpClient
 $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
 $packet = [byte[]](,0xFF * 102)
 6..101 |% { $packet[$_] = $mac[($_%6)]}
 $UDPclient.Send($packet, $packet.Length)

So how would you run it?  I would proactively open a command prompt and get the mac addresses from your arp table and save them to a file.  You can get at the table by running “arp –a” and locating the IP address for the node you are concerned about.  Later if you want to wake the host up, just open PowerShell, and run the script as follows:

image

Other ways to get the arp address for an offline machine?  If your network devices have SNMP enabled you may be able to leverage vendor tools to retrieve arp data from there.  If you have an inventory too such as System Center Configuration Manager, it would also store that data.