Skip to content

Instantly share code, notes, and snippets.

@alanwoolley
Last active May 23, 2023 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanwoolley/92da49258dccc08e3f5c914745ec1698 to your computer and use it in GitHub Desktop.
Save alanwoolley/92da49258dccc08e3f5c914745ec1698 to your computer and use it in GitHub Desktop.
Powershell script to send WOL magic packet and wait for the woken host to respond to ICMP Ping
<#
.SYNOPSIS
Sends a Wake-on-LAN (WOL) packet to a specified MAC address and pings an optional IP address until it is online.
.DESCRIPTION
This script sends a WOL packet to the specified MAC address to wake up a target device. It can also ping an optional IP address until it is online.
.PARAMETER mac
The MAC address of the target device. It should be provided in one of the following formats:
- 001122334455
- 00-11-22-33-44-55
- 00:11:22:33:44:55
.PARAMETER ipAddress (optional)
The IP address to ping until it is online.
.PARAMETER broadcast
The broadcast IP address used to send the WOL packet. Default is [System.Net.IPAddress]::Broadcast.
.LINK
https://gist.github.com/alanwoolley/92da49258dccc08e3f5c914745ec1698
.EXAMPLE
.\wol.ps1 -mac "001122334455" -ip "192.168.1.100"
Sends a WOL packet to the MAC address 001122334455 using the specified IP address.
.EXAMPLE
.\wol.ps1 -mac "001122334455" -ip "192.168.1.100" -broadcast "192.168.1.255"
Sends a WOL packet to the MAC address 001122334455 using the specified IP and broadcast addresses.
.EXAMPLE
.\wol.ps1 -mac "00-11-22-33-44-55" -ip "192.168.1.100"
Sends a WOL packet to the MAC address 00-11-22-33-44-55 using the specified IP address.
.EXAMPLE
.\wol.ps1 -mac "00:11:22:33:44:55" -ip "192.168.1.100" -broadcast "192.168.1.255"
Sends a WOL packet to the MAC address 00:11:22:33:44:55 using the specified IP and broadcast addresses.
#>
param (
[Parameter(Mandatory = $true, HelpMessage = "The MAC address of the target device.")]
[ValidatePattern('^([0-9A-Fa-f]{2}[:-]?){5}([0-9A-Fa-f]{2})$')]
[string]$mac,
[Parameter(Mandatory = $false, HelpMessage = "The IP address of the target device.")]
[ValidateNotNullOrEmpty()]
[string]$ip,
[Parameter(HelpMessage = "The broadcast IP address used to send the WOL packet.")]
[System.Net.IPAddress]$broadcast = [System.Net.IPAddress]::Broadcast
)
# Remove any separators and convert MAC address to upper case
$mac = $mac -replace '[:-]', ''
$mac = $mac.ToUpper()
# Convert MAC address to byte array
$macBytes = for ($i = 0; $i -lt 6; $i++) {
[byte]::Parse($mac.Substring($i * 2, 2), 'HexNumber')
}
# Create magic packet
$magicPacket = [byte[]]::new(102)
# Fill the first 6 bytes of the magic packet with 0xFF
0..5 | ForEach-Object { $magicPacket[$_] = 0xFF }
# Repeat the target MAC address 16 times in the magic packet
$index = 6
for ($i = 0; $i -lt 16; $i++) {
$macBytes | ForEach-Object {
$magicPacket[$index++] = $_
}
}
# Display the magic packet byte array
Write-Host "Magic Packet:"
for ($i = 0; $i -lt $magicPacket.Length; $i += 6) {
$row = $magicPacket[$i..($i + 5)] | ForEach-Object { "0x{0:X2}" -f $_ }
Write-Host ($row -join ' ')
}
# Create UDP client and send the magic packet
$udpClient = New-Object System.Net.Sockets.UdpClient
$udpClient.Connect($broadcast, 9)
$bytesSent = $udpClient.Send($magicPacket, $magicPacket.Length)
$udpClient.Close()
Write-Host "WOL magic packet ($bytesSent bytes) has been broadcast to wake up the device with MAC address $mac." -ForegroundColor Green
# Ping the IP address until it is online
if ($ip) {
Write-Host "Pinging IP address: $ipAddress"
$ping = New-Object System.Net.NetworkInformation.Ping
$timeout = 20000 # 20 seconds
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
while ($stopwatch.ElapsedMilliseconds -lt $timeout) {
$reply = $ping.Send($ip)
if ($reply.Status -eq 'Success') {
Write-Host "Ping succeeded! IP address $ip is online." -ForegroundColor Green
break
}
Start-Sleep -Milliseconds 1000
}
$stopwatch.Stop()
if ($stopwatch.ElapsedMilliseconds -ge $timeout) {
Write-Host "Timeout! Could not ping IP address $ipAddress within the specified timeout." -ForegroundColor Red
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment