-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSend-MagicPacket.ps1
51 lines (48 loc) · 1.4 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
param(
[Parameter(Mandatory=$true)]
[string]
$MacAddress,
[Parameter(Mandatory=$false)]
[string]
$BroadcastAddress,
[Parameter(Mandatory=$false)]
[int]
$Port,
[Parameter(Mandatory=$false)]
[string]
$ConfirmAwake,
[switch]
$Beep
)
# set default broadcast address and port if not specified
if (!$BroadcastAddress) {$BroadcastAddress = "255.255.255.255"}
if (!$Port) {$Port = 7}
# send the magic packet
$MacByteArray = $MacAddress -split "[:-]" | ForEach-Object {[Byte]"0x$_"}
[Byte[]]$MagicPacket = (,0xFF * 6) + ($MacByteArray * 16)
$UdpClient = New-Object System.Net.Sockets.UdpClient
$UdpClient.Connect(($BroadcastAddress),$Port)
[Void]$UdpClient.Send($MagicPacket,$MagicPacket.Length)
$UdpClient.Close()
Write-Host "Magic packet sent to $MacAddress"
if ($ConfirmAwake) {
while ($true) {
Write-Host "Checking if $ConfirmAwake is online..."
try {
$Connection = Test-Connection $ConfirmAwake -Count 1 -Quiet
if ($Connection) {
Write-Host "$ConfirmAwake is online."
if ($Beep) {
[System.Console]::Beep(200,1000)
}
exit 0
} else {
Write-Host "Checking if $ConfirmAwake is online..."
}
Start-Sleep -Seconds 1
} catch {
Write-Host "Syntax error"
exit 1
}
}
}