05 Sep 2013 @ 6:52 AM 

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!

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"
        }
    }
}
Posted By: Jeff Wouters
Last Edit: 05 Sep 2013 @ 12:34 PM

EmailPermalink
Tags
Categories: DuPSUG, PowerShell, Script



 Last 50 Posts
 Back
Change Theme...
  • Users » 1
  • Posts/Pages » 339
  • Comments » 486
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

About



    No Child Pages.

Contact



    No Child Pages.

Speaking



    No Child Pages.

Health Check



    No Child Pages.