Skip to content

Instantly share code, notes, and snippets.

@reboot81
Forked from jpluimers/README.md
Last active August 8, 2023 19:40
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 reboot81/f816e95aaee42a8107449a6de4f99065 to your computer and use it in GitHub Desktop.
Save reboot81/f816e95aaee42a8107449a6de4f99065 to your computer and use it in GitHub Desktop.
datto comp: Wake-on-LAN.ps1 PowerShell script for sending Wake-on-LAN magic packets to given machine's hardware MAC address

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

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

Adopted to work with datto rmm.

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)

Create a component, select powershell as script type, paste the script, add a variable: Name: MacAddress, Type: String, Default Value: 01-23-45-67-89-AB, Description: MAC-address

#######################################################
##
## Wake-on-LAN.ps1, v1.2, 2023
## Adapted by Bo Saurage
## 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)
#>
# Removed this and replaced it with $MacAddress=$env:MacAddress so it would work with datto.
#param(
# [Parameter(Mandatory = $true, HelpMessage = "MAC address of target machine to wake up")]
# [string] $MacAddress
#)
# Datto env name:MacAddress, type:String
$MacAddress=$env: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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment