Created
September 17, 2014 18:45
-
-
Save zalmyr/920dc2e383331a20bcd4 to your computer and use it in GitHub Desktop.
Wake On LAN Powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#These are the parameters to the script. The only mandatory param here is the mac address | |
#[net.ipaddress]::Broadcast will resolve to something like 255.255.255.255 | |
param ( | |
$targetMac, | |
$network = [net.ipaddress]::Broadcast, | |
$subnet = [net.ipaddress]::Broadcast | |
) | |
#We start the try, catch error handling here. | |
#if something in try block fails, the catch block will write the error | |
try { | |
#This will evaludate to False. Hence, $network will have whatever was passed through params or the default value | |
#in this case the default value is 255.255.255.255 | |
if($network.gettype().equals([string])) { | |
$network = [net.ipaddress]::Parse($network); | |
} | |
#This will evaludate to False. Hence, $network will have whatever was passed through params or the default value | |
#in this case the default value is 255.255.255.255 | |
if($subnet.gettype().equals([string])) { | |
$subnet = [net.ipaddress]::Parse($subnet); | |
} | |
#Not sure if this is really required here. But, assuming that the default value for both $network and $subet is 255.255.255.255, | |
#this will result in $broadcast set to 255.255.255.255 | |
$broadcast = new-object net.ipaddress (([system.net.ipaddress]::parse("255.255.255.255").address -bxor $subnet.address -bor $network.address)) | |
#This again assumes that you had given . as the delimeter in MAC address and removes that from MAC address | |
$mac = [Net.NetworkInformation.PhysicalAddress]::Parse($targetMac.toupper().replace(".","")) | |
#Create a new object of type net.sockets.udpclient | |
$u = New-Object net.sockets.udpclient | |
#WOL magic packet can be sent on port 0, 7, or 9 | |
#Create a end point for the broadcast address at port 0 | |
$ep = New-Object net.ipendpoint $broadcast, 0 | |
#Create a end point for the broadcast address at port 7 | |
$ep2 = New-Object net.ipendpoint $broadcast, 7 | |
#Create a end point for the broadcast address at port 9 | |
$ep3 = New-Object net.ipendpoint $broadcast, 9 | |
#Create a payload packet | |
#First, create a byte array | |
$payload = [byte[]]@(255,255,255, 255,255,255); | |
#add the mac address to the above byte array | |
$payload += ($mac.GetAddressBytes()*16) | |
#Send 10 magic packets for each port number or end point created above. | |
#one is more than enough. If everything is congfigured properly | |
for($i = 0; $i -lt 10; $i++) { | |
$u.Send($payload, $payload.Length, $ep) | Out-Null | |
$u.Send($payload, $payload.Length, $ep2) | Out-Null | |
$u.Send($payload, $payload.Length, $ep3) | Out-Null | |
sleep 1; | |
} | |
} catch { | |
#catch block catches any error from try block | |
$Error | Write-Error; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment