Simple Wake On Lan powershell Function.

I recently wrote the below powershell module for waking up my home lab. There’s a great article on The Powershell Guy to do with Wake On Lan, but it didn’t include a function or any specific error checking.

Below is the module I put together quickly for the purpose.
[sourcecode language=”powershell”]
function send-wakeonlan{
param(
[string]$mac)
if (!($mac -like "*:*:*:*:*") -or ($mac -like "*-*-*-*-*")){
write-error "mac address not in correct format"
break
}

$string=@($mac.split(":""-") | foreach {$_.insert(0,"0x")})
$target = [byte[]]($string[0], $string[1], $string[2], $string[3], $string[4], $string[5])

$UDPclient = new-Object System.Net.Sockets.UdpClient
$UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
$packet = [byte[]](,0xFF * 102)
6..101 |% { $packet[$_] = $target[($_%6)]}
$UDPclient.Send($packet, $packet.Length) | out-null
}
[/sourcecode]

The usage of this function is below:

send-wakeonlan -mac “3C:4A:92:77:AA:21″

send-wakeonlan -mac “3C-4A-92-77-AA-21″

Related Posts

New Module: Creating an RDP file password with Pow... Here's something that is surprisingly tricky to automate in this day and age. Creating a password and storing it in an RDP file. I'm not here to debat...
Dealing with multi numbered versions in powershell... So here's a quick little blog about something i discovered in powershell while googling today. Lots of vendors like to use version numbers includin...
Accurately checking the Citrix PVS “cache in... Citrix Provisioning services "Cache in RAM, overflow to disk", even with it's challenges is something I've always felt was a great idea, hell, I fores...

17 Comments About “Simple Wake On Lan powershell Function.

  1. Hugh Poling

    Your if statement wasn’t working. I specified dashes in the mac the if statement would error out. The corrected version of the if statement:

    if (-not (($mac -like “*:*:*:*:*”) -or ($mac -like “*-*-*-*-*”))){
    write-error “mac address not in correct format”
    break
    }

    Also, I didn’t need to use the “-mac” switch in the syntax.

    Both of these worked for me after changing the if statement as above:

    send-wakeonlan “3C:4A:92:77:AA:21″
    send-wakeonlan “3C-4A-92-77-AA-21″

    Reply
  2. George

    Hey buddy,
    great post, just a quick question from a powershell starter:
    6..101 |% { $packet[$_] = $target[($_%6)]}
    What does this part do?

    Reply
  3. Gustav Krantz

    <- Also as powershell starter. If i would want to
    run it as: send-wakeonlan i.e. send-wakeonlan tv Instead of having
    to put in the mac for every computer i want to start. Can you
    rewrite the script to use like… if ((tv = string) send to ?
    Thanks!

    Reply
  4. Gustav Krantz

    Sure it’s possible. I just did it…

    $tv = “6C:62:6D:87:DB:A5″
    $seeker = “0C:0C:0C:0C:01″
    $server = “00:22:15:88:AE:29″
    $virwin = “F4:6D:04:34:06:91″
    function wol{
    param(
    [string]$mac)
    if (($mac -eq “tv”)){
    $tv=@($tv.split(“:””-“) | foreach {$_.insert(0,”0x”)})
    $target = [byte[]]($tv[0], $tv[1], $tv[2], $tv[3], $tv[4], $tv[5])

    $UDPclient = new-Object System.Net.Sockets.UdpClient
    $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
    $packet = [byte[]](,0xFF * 102)
    6..101 |% { $packet[$_] = $target[($_%6)]}
    $UDPclient.Send($packet, $packet.Length) | out-null
    }
    if (($mac -eq “seeker”)){
    $seeker=@($seeker.split(“:””-“) | foreach {$_.insert(0,”0x”)})
    $target = [byte[]]($seeker[0], $seeker[1], $seeker[2], $seeker[3], $seeker[4], $seeker[5])

    $UDPclient = new-Object System.Net.Sockets.UdpClient
    $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
    $packet = [byte[]](,0xFF * 102)
    6..101 |% { $packet[$_] = $target[($_%6)]}
    $UDPclient.Send($packet, $packet.Length) | out-null
    }
    if (($mac -eq “server”)){
    $server=@($server.split(“:””-“) | foreach {$_.insert(0,”0x”)})
    $target = [byte[]]($server[0], $server[1], $server[2], $server[3], $server[4], $server[5])

    $UDPclient = new-Object System.Net.Sockets.UdpClient
    $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
    $packet = [byte[]](,0xFF * 102)
    6..101 |% { $packet[$_] = $target[($_%6)]}
    $UDPclient.Send($packet, $packet.Length) | out-null
    }
    if (($mac -eq “virwin”)){
    $virwin=@($virwin.split(“:””-“) | foreach {$_.insert(0,”0x”)})
    $target = [byte[]]($virwin[0], $virwin[1], $virwin[2], $virwin[3], $virwin[4], $virwin[5])

    $UDPclient = new-Object System.Net.Sockets.UdpClient
    $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
    $packet = [byte[]](,0xFF * 102)
    6..101 |% { $packet[$_] = $target[($_%6)]}
    $UDPclient.Send($packet, $packet.Length) | out-null
    }
    }

    All I need to do now is run:
    wol tv/seeker/server/virwin and the script will wake the computer i tell it to.

    No problem.

    Reply
  5. Markus Ressel

    Hey there,

    your code still is not updated. The test for wrong input is simply useless since you can enter anything after and before the last/first dash/colon.

    Anyway thanks a lot for sharing this, made my life a lot easier!
    Keep up the good work.

    Reply
  6. Markus Lohr

    Hey guys,
    here some improvements (on my side of the world the mac-address has 6 segments… ) ;-)
    ——————————————————

    function send-wakeonlan{
    param(
    [string]$mac)
    # error checking
    if (-not(($mac -like “*:*:*:*:*:*”) -or ($mac -like “*-*-*-*-*-*”))){
    write-error “mac address not in correct format”
    break
    }
    #rewrite the mac-format
    $string = @($mac.split(“:””-“) | foreach {$_.insert(0,”0x”)})
    $target = [byte[]]($string[0], $string[1], $string[2], $string[3], $string[4], $string[5])

    #building the magic package
    $UDPclient = new-Object System.Net.Sockets.UdpClient
    $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
    $packet = [byte[]](,0xFF * 102)
    6..101 |% { $packet[$_] = $target[($_%6)]}
    #sending the magic package
    $UDPclient.Send($packet, $packet.Length) | out-null
    }

    #PS stuff
    Export-ModuleMember -Function send-wakeonlan
    ——————————————————
    have fun….

    Reply

Leave a Reply