-
Notifications
You must be signed in to change notification settings - Fork 5
/
WakeOnLan.psm1
142 lines (99 loc) · 5.82 KB
/
WakeOnLan.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<#
Invoke-WakeOnLan
Chris Warwick, @cjwarwickps, January 2012. This version, November 2015.
Cmdlet to send a Wake-on-Lan packet to a specified target MAC addresses.
Wake on Lan (WOL) uses a “Magic Packet” that consists of six bytes of 0xFF (the physical layer broadcast address), followed
by 16 copies of the 6-byte (48-bit) target MAC address (see http://en.wikipedia.org/wiki/Wake-on-LAN).
This packet is sent via UDP to the LAN Broadcast addresses (255.255.255.255) on arbitrary Port 4000.
Construction of this packet in PowerShell is very straight-forward: (“$Packet = [Byte[]](,0xFF*6)+($Mac*16)”).
This script has a (hard-coded) table of saved MAC addresses to allow machine aliases to be specified as parameters to the
function (the real addresses have been obfuscated here) and uses a regex to validate MAC address strings.
It would be possible to use DNS and the ARP Cache to resolve MAC addresses, however, the ARP cache will only be populated with
a valid entry for any given target adapter for a relative short period of time after the last use of the address (10 minutes
or less depending on usage); ARP cannot be used to dynamically resolve the address of a suspended adapter.
#>
#Requires -Version 2
<#
.Synopsis
This cmdlet sends Wake-on-Lan Magic Packets to the specified Mac addresses.
.Description
Wake on Lan (WOL) uses a “Magic Packet” that consists of six bytes of 0xFF (the physical layer broadcast address), followed
by 16 copies of the 6-byte (48-bit) target MAC address (see http://en.wikipedia.org/wiki/Wake-on-LAN).
This packet is sent via UDP to the LAN Broadcast addresses (255.255.255.255) on arbitrary Port 4000.
Construction of this packet in PowerShell is very straight-forward: (“$Packet = [Byte[]](,0xFF*6)+($Mac*16)”).
This script has a (hard-coded) table of saved MAC addresses to allow machine aliases to be specified as parameters to the
function (the real addresses have been obfuscated here) and uses a regex to validate MAC address strings. The address
aliases are contained in a hash table in the script - but they could very easily be obtained from an external source such as
a text file or a CSV file (this is left as an exercise for the reader).
It would be possible to use DNS and the ARP Cache to resolve MAC addresses, however, the ARP cache will only be populated with
a valid entry for any given target adapter for a relative short period of time after the last use of the address (10 minutes
or less depending on usage); ARP cannot be used to dynamically resolve the address of a suspended adapter.
.Example
Invoke-WakeOnLan 00-1F-D0-98-CD-44
Sends WOL packets to the specified address
.Example
Invoke-WakeOnLan 00-1F-D0-98-CD-44, 00-1D-92-3B-C2-C8
Sends WOL packets to the specified addresses
.Example
00-1F-D0-98-CD-44, 00-1D-92-3B-C2-C8 | Invoke-WakeOnLan
Sends WOL packets to the specified addresses
.Example
Invoke-WakeOnLan Server3
Sends WOL packets to the specified target using an alias. The alias must currently be hard-coded in the script.
.Inputs
An array of MAC addresses. Each address must be specified as a sequence of 6 hex-coded bytes seperated by ':' or '-'
The input can also contain aliases - these must currently be hard-coded in the script (see examples)
MAC addresses can be piped to the cmdlet.
.Outputs
Wake-on-Lan packets are sent to the specified addresses
.Parameter MacAddress
An array of MAC addresses. Each address must be specified as a sequence of 6 hex-coded bytes seperated by ':' or '-'
.Functionality
Sends Wake-on-Lan Magic Packets to the specified Mac addresses
#>
Function Invoke-WakeOnLan {
[OutputType()]
Param (
[Parameter(ValueFromPipeline)]
[String[]]$MacAddress
)
Begin {
# The following table contains aliases for commonly used MAC addresses; modify as required.
$StaticLookupTable=@{
Hyperion = '00-1F-D0-98-CD-44'
Nova = '00-1D-92-3B-C2-C8'
Desktop = '00-15-58-9B-6A-1B'
Laptop = '00-17-08-42-D5-18'
AD2 = '00-1F-D0-98-CD-5C'
Server3 = '00-0E-2E-49-25-32'
Media = '1C-6F-65-D7-20-D7'
}
$UdpClient = New-Object System.Net.Sockets.UdpClient
}
Process {
Foreach ($MacString in $MacAddress) {
# Check to see if a known MAC alias has been specified; if so, substitute the corresponding address
If ($StaticLookupTable.ContainsKey($MacString)) {
Write-Verbose -Message "Found '$MacString' in lookup table"
$MacString = $StaticLookupTable[$MacString]
}
# Validate the MAC address, 6 hex bytes separated by : or -
If ($MacString -NotMatch '^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$') {
Write-Warning "Mac address '$MacString' is invalid; must be 6 hex bytes separated by : or -"
Continue
}
# Split and convert the MAC address to an array of bytes
$Mac = $MacString.Split('-:') | Foreach {[Byte]"0x$_"}
# WOL Packet is a byte array with the first six bytes 0xFF, followed by 16 copies of the MAC address
$Packet = [Byte[]](,0xFF * 6) + ($Mac * 16)
# Write-Verbose "Broadcast packet: $([BitConverter]::ToString($Packet))" # Un-comment this line to display packet
$UdpClient.Connect(([System.Net.IPAddress]::Broadcast),4000) # Send packets to the Broadcast address
[Void]$UdpClient.Send($Packet, $Packet.Length)
Write-Verbose "Wake-on-Lan Packet sent to $MacString"
}
}
End {
$UdpClient.Close()
$UdpClient.Dispose()
}
}