Wake-On-Lan with PowerShell

Here is a code snippet from PowerShell script that I found on the net (thanks Brandon!) that actually works, as opposed to many examples I tried out that did not send magic packet correctly…

function Send-WOL{
    param ($macaddy)
    $mymac = $macaddy.split(':') | %{ [byte]('0x' + $_) }
    if ($mymac.Length -ne 6)
    {
        throw 'Mac Address Must be 6 hex Numbers Separated by : or -'
    }
    Write-Verbose "Creating UDP Packet"
    $UDPclient = new-Object System.Net.Sockets.UdpClient
    $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
    $packet = [byte[]](,0xFF * 6)
    $packet += $mymac * 16
    Write-Verbose ([bitconverter]::tostring($packet))
    [void] $UDPclient.Send($packet, $packet.Length)
    Write-Host  "   - Wake-On-Lan Packet of length $($packet.Length) sent to $mymac"
}

…and here is an example:

PS E:\> Send-Wol(’70:f3:95:15:00:b5′)
– Wake-On-Lan Packet of length 102 sent to 112 243 149 21 0 181
PS E:\>

Advertisement

Posted on 25.07.2011, in Scripting and tagged , , . Bookmark the permalink. Comments Off on Wake-On-Lan with PowerShell.

Comments are closed.