This morning I was asked to write a little script to do a WOL through PowerShell. Instead of writing it myself, I found this script.
As a scriptaholic I can’t just copy-paste and be done with it… I have to make it more suitable for my customer. 🙂
So, I’ve changed/added a few things.
- A proper parameter
- A little (crude) error handling
- Allowing for multiple MAC addresses as input
- Allowing for pipeline input
- Just a few other things to make it more understandable for myself
Have fun!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
function Send-WOL { param ( [parameter( mandatory=$true, position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [ValidateLength(17,17)] [ValidatePattern("^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$")] [array]$MACAddress ) foreach ($MAC in $MACAddress) { try { $MAC = $mac.split(':') | %{ [byte]('0x' + $_) } $UDPclient = new-Object System.Net.Sockets.UdpClient $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000) $Packet = [byte[]](,0xFF * 6) $Packet += $MAC * 16 Write-Verbose ([bitconverter]::tostring($Packet)) [void] $UDPclient.Send($Packet, $Packet.Length) Write-Output "WOL command sent to $MAC" } catch [system.exception] { Write-Output "ERROR: Unable to send WOL command to $MAC" } } } |
[…] found a very good working PowerShell snippet on Jeff Wouters’s blog here , so all the credit belongs to […]