Saturday, September 11, 2010

Powershell Wake-OnLan Function

Me and my colleague were discussing just before the left for the weekend and at one point he said he said; "I always switch off my PC before the weekend and it's always switched on when I come back on Monday. It didn't used to do that so they (the desktop operations team) must have deployed Wake-on-Lan". This triggered a thought in my head, how does Wake-on-Lan work. So after my colleague left I googled around a bit and found this piece of java code. After seeing how easy it is to send a Wake-on-Lan magic packet I couldn't stop myself from spending another half an hour at work to build a powershell Wake-on-Lan function.

To wake a PC up just run the following:

PS C:\PS> Wake-OnLan -BroadCastAddress 192.168.0.255 -MacAddress 32:2D:F4:32:7D:E5

Where -BroadCastAddress is the IP Address subnet address and where -MacAddress is the mac address of the computer to wake up. The -MacAddress parameter takes the address in either the colon (32:24:F4:32:7D:E5) or the dash (32-24-F4-32-7D-E5) format. Here is the function...

Function Wake-OnLan {
param(
  [string]$BroadCastAddress=$(throw "Mandatory -Parameter -BroadCastAddress missing, for example 192.168.0.255"),
  [string]$MacAddress=$(throw "Mandatory -Parameter -MacAddress missing, for example 32:2D:F4:32:7D:E5 or 32-2D-F4-32-7D-E5")
  )
  [void][System.Reflection.Assembly]::LoadWithPartialName("System.Net")
  [void][System.Reflection.Assembly]::LoadWithPartialName("System.Net.Sockets")
  $udpClient = new-object System.Net.Sockets.UdpClient
  $endPoint = new-object System.Net.IPEndPoint $([System.Net.IPAddress]::Parse($BroadCastAddress)),10000

  $MacAddress = $MacAddress.Replace(":","-")
  [byte[]]$macBytes = $macAddress.split("-") | %{[byte]"0x$_"}
  [byte[]]$bytes = new-object "byte[]" $(6 + 16 * $($macBytes.length))
  for ($i = 0; $i -lt 6; $i++) {
    $bytes[$i] = [byte] 0xff
  }
  for ($i = 6; $i -lt $bytes.length; $i += $macBytes.length) {
    for($j = 0; $j -lt $macBytes.length; $j++){
      $bytes[$i + $j] = $macBytes[$j]
    }
  }
  $udpClient.connect($endPoint)
  [void]$udpClient.Send($bytes,$bytes.length)
  Write-Host "Magic packet has been sent"
}


Please enjoy!

No comments:

Post a Comment