Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

PowerShell Wake-on-lan script

*note* I forgot this post still was in my drafts folder, I posted the script in the PowerShell Newsgroup and asked if someone could test it and I got confirmed that it did work,thanks again,  so here it is :

I came along this post about WOL (wake-on-Lan) in PowerShell : Wake-On-LAN script , and that it did use an external .EXE

As I made scripts (o.a. VB.NET) for this before , I almost instanty did think

TMTOWTDI

I did know that it was something with a Magic Packet and a WakeUp bytes containing the MAC adress,

As I already did read this post, C# to PowerShell Translation Thought Process , and to make it easier I added C# to my search.

(before I did .NET or VB.NET ) , but the C# is easy to search on and examples are more easy to translate.

so I did my first (and last) Google search only with : WOL C#

 

The first page shown was :

CodeKeep Snippet: Wake On LAN (WOL) (C#)

I opened was this one and hit-in-one a perfect sample, so I just worked my way down an had my script ready in minutes

 

$mac = [byte[]](0x00, 0x0F, 0x1F, 0x20, 0x2D, 0x35)

$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)

 

(but then... oops no machine to test around... Off to friend for coffee ;-) ) :

we have not been able to test it but meanwhile made some parser lines for other formats of mac adresses :

PoSH> $mac = [byte[]]("00-0F-1F-20-2D-35".split('-') |% {[int]"0x$_"})                                                  
PoSH> $mac                                                                                                              
0                                                                                                                       
15                                                                                                                      
31                                                                                                                      
32                                                                                                                      
45                                                                                                                      
53                                                                                                                      
PoSH> "000F1F202D35" -match "(..)(..)(..)(..)(..)(..)" | out-null                                                       
PoSH> $mac = [byte[]]($matches[1..6] |% {[int]"0x$_"})                                                                  
PoSH> $mac                                                                                                              
0                                                                                                                       
15                                                                                                                      
31                                                                                                                      
32                                                                                                                      
45                                                                                                                      
53                                                                                                                      
PoSH> $mac = [byte[]](0x00, 0x0F, 0x1F, 0x20, 0x2D, 0x35)                                                               
PoSH> $mac                                                                                                              
0                                                                                                                       
15                                                                                                                      
31                                                                                                                      
32                                                                                                                      
45                                                                                                                      
53                                                                                                                      
PoSH>                                                  

 

enjoy,

Greetings /\/\o\/\/

Published Sunday, April 01, 2007 6:15 AM by admin
Filed under: , , , ,

Comments

# re: PowerShell Wake-on-lan script

Hi MOW,

I did the same, but some months ago:

http://viniciuscanto.blogspot.com/2007/01/wake-on-lan-powershell-acordando.html">http://viniciuscanto.blogspot.com/2007/01/wake-on-lan-powershell-acordando.html

[]s,

--

Vinicius Canto <scripterbr_at_gmail_dot_com>

MVP Visual Developer - Scripting

MCP Windows 2000 Server, Windows XP e SQL Server 2000

Blog sobre Scripting: http://viniciuscanto.blogspot.com

Monday, April 09, 2007 11:12 PM by Vinicius Canto [MVP]

# re: PowerShell Wake-on-lan script

script-file version

param ([String]$macString = $(throw 'mac address is required'))

$mac = $macString.split(':') | %{ [byte]('0x' + $_) }

if ($mac.Length -ne 6)

{

   throw 'mac address must be 6 hex numbers separated by :'

}

$UDPclient = new-Object System.Net.Sockets.UdpClient

$UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)

$packet = [byte[]](,0xFF * 6)

$packet += $mac * 16

write-debug ([bitconverter]::tostring($packet))

[void] $UDPclient.Send($packet, $packet.Length)

write-debug "Wake-On-Lan magic packet of length $($packet.Length) sent to $macString"

Friday, April 27, 2007 12:11 AM by James Manning

# re: PowerShell Wake-on-lan script

Hi James,

Thanks for the script version,

byte handling is also a bit cleaner ;-)

Greetings /\/\o\/\/

Friday, April 27, 2007 12:12 PM by MoW

# Powershell and SNMP | keyongtech

Sunday, January 18, 2009 12:16 PM by Powershell and SNMP | keyongtech

# 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.&#160;

Wednesday, April 01, 2009 6:12 PM by Off Campus
Anonymous comments are disabled