Skip to content

Instantly share code, notes, and snippets.

@KhoraLee
Last active December 7, 2019 04:08
Show Gist options
  • Save KhoraLee/8d978cf815d452f7bf33e839d0c1a9f3 to your computer and use it in GitHub Desktop.
Save KhoraLee/8d978cf815d452f7bf33e839d0c1a9f3 to your computer and use it in GitHub Desktop.
Turning on other computer on Windows by using WOL(Wake-On-Lan)
@echo off
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& './wol.ps1' 'Mac Address' "
[cmdletBinding()]
param(
[Parameter()]
[ValidatePattern('(^([0-9a-fA-F]{2}[\.:-]{0,1}){5}[0-9a-fA-F]{2}$)|(^([0-9a-fA-F]{4}\.[0-9a-fA-F]{4}\.[0-9a-fA-F]{4})$)')]
[string]$mac,
[string]$ip="255.255.255.255",
[int]$port=9
)
$broadcast = [Net.IPAddress]::Parse($ip)
$mac=$mac.ToUpper()
$mac=(($mac.replace(":","")).replace("-","")).replace(".","")
$target=0,2,4,6,8,10 | % {[convert]::ToByte($mac.substring($_,2),16)}
$packet = (,[byte]255 * 6) + ($target * 16)
$UDPclient = new-Object System.Net.Sockets.UdpClient
$UDPclient.Connect($broadcast,$port)
[void]$UDPclient.Send($packet, 102)
#User Output
0,2,4,6,8 | % {$OutMac+="$($mac.substring($_,2))`:"}
$OutMac+=$Mac.substring(10,2)
Write-HOST "A Magic-Packet was send to $ip`:$port to wake up '$OutMac'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment