![]()
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″


This is exactly what I was looking for… Thanks a lot for sharing.
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″
Thanks for sharing! The Mac parameter is positional, you don’t need to specify it as its assumed.
I’ll update the code to include your fix for the dashes.
Hey buddy,
great post, just a quick question from a powershell starter:
6..101 |% { $packet[$_] = $target[($_%6)]}
What does this part do?
Creates the magic packet
<- 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!
You will need to specify the MAC address of the device, its unavoidable.
It should’nt be if it predefined in the script.
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.
that’s not what you asked, but I appreciate the feedback.
Also, consider using the switch command in powershell to reduce your code:
http://technet.microsoft.com/en-us/library/ff730937.aspx
I’m as you know a noob at that. Would you please give me an example of the code for this script?
That switch statement didnt help me much. I dont know how that will help me.
Hi Gustav,
here is my recommendation:
http://pastebin.com/SYthJH4f
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.
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….
Thanks for contributing!