-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSend-MagicPacket.ps1
60 lines (44 loc) · 1.51 KB
/
Send-MagicPacket.ps1
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
<#
This script displays a list of MAC addresses from a json file,
prompts the user to decide whether to pick one of the
existing addresses or provide their own input and then
broadcasts a WOL packet for the chosen computer in port 0.
#>
$knownAdaptors = Get-Content -Path $env:HOMEPATH\.config\WakeOnLAN\targetsList.json -ErrorAction SilentlyContinue | ConvertFrom-Json
Write-Host @"
List of known hosts:
"@
$count = 0
foreach ($adaptor in $knownAdaptors)
{
Write-Host "[$count]>" $adaptor.MAC "`t Friendly Name: " $adaptor.friendlyName
$count += 1
}
Write-Host @"
[$count]> User-defined
"@
Write-Host -NoNewline "Choose target of Magic Packet: "
[Int16] $target = Read-Host
if ($targetMac -eq $count)
{
Write-Host -NoNewline "MAC of target: "
$targetAdaptor = Read-Host
}
elseif (($target -gt $count) -or ($target -lt 0))
{
throw "Invalid input value"
}
else
{
[PSCustomObject] $targetAdaptor = $knownAdaptors[$target]
}
# Create WOL packet contents:
[Byte[]] $startBytes = ,0xFF * 6
$targetAdaptor.MAC = $targetAdaptor.MAC.Replace(':','-')
[Byte[]] $macBytes = ($targetAdaptor.MAC.Split('-') | ForEach-Object { [Byte] "0x$_"}) * 16
[Byte[]] $magicPacketContents = $startBytes + $macBytes
# Send WOL packet:
$UdpClient = New-Object System.Net.Sockets.UdpClient
$UdpClient.Connect([System.Net.IPAddress]::Broadcast,0)
$UdpClient.Send($magicPacketContents, $magicPacketContents.Length) | Out-Null #Suppress Output
$UdpClient.Close()