Skip to content

Instantly share code, notes, and snippets.

@jpluimers
Forked from alimbada/Wake.ps1
Last active December 19, 2023 21:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jpluimers/344ed287b784048349769a334f116b46 to your computer and use it in GitHub Desktop.
Save jpluimers/344ed287b784048349769a334f116b46 to your computer and use it in GitHub Desktop.
Wake-on-LAN.ps1 PowerShell script for sending Wake-on-LAN magic packets to given machine's hardware MAC address

Wake-on-LAN.ps1: a Wake-on-LAN PowerShell script

Wake-on-LAN.ps1 PowerShell script for sending Wake-on-LAN magic packets to given machine's hardware MAC address

This script originated is a modification [Wayback] WakeUp-Machines.ps1 by Matthijs ten Seldam, Microsoft that has the drawback of requiring a text file with the machines to wake up, and (before his change) the original reliance on the above mentioned [Wayback] WolCmd.exe in the current directory.

[Wayback] Ammaar Limbada modified it to take a hardware MAC address on the command-line, but unlike the [Wayback] Perl wakeonlan script that is available for most Linux and BSD systems, it was limited to Windows and pure hex (Intel Landesk) formatted hardware MAC Addresses.

wakeonlan supports these formats or "notational conventions":

  • xx:xx:xx:xx:xx:xx (canonical)
  • xx-xx-xx-xx-xx-xx (Windows)
  • xxxxxx-xxxxxx (Hewlett-Packard switches)
  • xxxxxxxxxxxx (Intel Landesk)

Goal is to have this script support all of them too (:

#######################################################
##
## Wake-on-LAN.ps1, v1.1, 2021
##
## Adapted by Ammaar Limbada and Jeroen Wiert Pluimers
## Original Author: Matthijs ten Seldam, Microsoft (see: http://blogs.technet.com/matthts)
##
#######################################################
<#
.SYNOPSIS
Starts a list of physical machines by using Wake On LAN.
.DESCRIPTION
Wake sends a Wake On LAN magic packet to a given machine's MAC address.
.PARAMETER MacAddress
MacAddress of target machine to wake.
.EXAMPLE
Wake A0DEF169BE02
.INPUTS
None
.OUTPUTS
None
.NOTES
Supports all these MAC addresses formats:
- xx:xx:xx:xx:xx:xx (canonical)
- xx-xx-xx-xx-xx-xx (Windows)
- xxxxxx-xxxxxx (Hewlett-Packard switches)
- xxxxxxxxxxxx (Intel Landesk)
- xxxx.xxxx.xxxx (Cisco)
#>
param(
[Parameter(Mandatory = $true, HelpMessage = "MAC address of target machine to wake up")]
[string] $MacAddress
)
Set-StrictMode -Version Latest
function Send-Packet([string]$MacAddressString) {
<#
.SYNOPSIS
Sends a number of magic packets using UDP broadcast.
.DESCRIPTION
Send-Packet sends a specified number of magic packets to a MAC address in order to wake up the machine.
.PARAMETER MacAddressString
String with the MAC address of the machine to wake up.
#>
try {
$Broadcast = ([System.Net.IPAddress]::Broadcast)
## Create UDP client instance
$UdpClient = New-Object Net.Sockets.UdpClient
## Create IP endpoints for each port
$IPEndPoint = New-Object Net.IPEndPoint $Broadcast, 9
$BareMacAddressString = $MacAddress.ToUpper() -split "[:\.-]" -join "" # \ because it is a regular expression
## Construct physical address instance for the hardware MAC address of the machine (string to byte array)
$MAC = [Net.NetworkInformation.PhysicalAddress]::Parse($BareMacAddressString)
## Construct the Magic Packet frame
$Packet = [Byte[]](, 0xFF * 6) + ($MAC.GetAddressBytes() * 16)
## Broadcast UDP packets to the IP endpoint of the machine
$UdpClient.Send($Packet, $Packet.Length, $IPEndPoint) | Out-Null
$UdpClient.Close()
}
catch {
$UdpClient.Dispose()
$Error | Write-Error;
}
}
## Send magic packet to wake machine
Write-Output "Sending magic packet to $MacAddress"
Send-Packet $MacAddress
@drlsdee
Copy link

drlsdee commented Feb 17, 2023

At line 67, instead of splitting the raw string by certain characters and then joining it back, it would be better just to remove all non-hex characters:

$BareMacAddressString = $MacAddress.ToUpper() -replace '[^A-F0-9]'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment