mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-03-29 16:16:47 +01:00
Improved wakeup.ps1
This commit is contained in:
parent
72e7956034
commit
fbfd13d636
@ -1,65 +1,35 @@
|
|||||||
#!/snap/bin/powershell
|
#!/snap/bin/powershell
|
||||||
<#
|
<#
|
||||||
.SYNTAX ./wakeup.ps1 [<hostname>]
|
.SYNTAX ./wakeup.ps1 [<hostname>] [<MAC-address>]
|
||||||
.DESCRIPTION sends a magic packet to the given computer, waking him up
|
.DESCRIPTION sends a magic packet to the given computer, waking him up
|
||||||
.LINK https://github.com/fleschutz/PowerShell
|
.LINK https://github.com/fleschutz/PowerShell
|
||||||
.NOTES Author: Markus Fleschutz / License: CC0
|
.NOTES Author: Markus Fleschutz / License: CC0
|
||||||
#>
|
#>
|
||||||
|
|
||||||
param([string]$Hostname = "")
|
param([string]$Hostname = "", [string]MACaddress = "")
|
||||||
if ($Hostname -eq "" ) {
|
|
||||||
$Hostname = read-host "Enter hostname"
|
|
||||||
}
|
|
||||||
|
|
||||||
function Send-WOL {
|
function Send-WOL { param([string]$mac, [string]$ip="255.255.255.255", [int]$port=9)
|
||||||
<#
|
$broadcast = [Net.IPAddress]::Parse($ip)
|
||||||
.SYNOPSIS
|
|
||||||
Send a WOL packet to a broadcast address
|
|
||||||
.PARAMETER mac
|
|
||||||
The MAC address of the device that need to wake up
|
|
||||||
.PARAMETER ip
|
|
||||||
The IP address where the WOL packet will be sent to
|
|
||||||
.EXAMPLE
|
|
||||||
Send-WOL -mac 00:11:32:21:2D:11 -ip 192.168.8.255
|
|
||||||
.EXAMPLE
|
|
||||||
Send-WOL -mac 00:11:32:21:2D:11
|
|
||||||
#>
|
|
||||||
|
|
||||||
[CmdletBinding()]
|
|
||||||
param(
|
|
||||||
[Parameter(Mandatory=$True,Position=1)]
|
|
||||||
[string]$mac,
|
|
||||||
[string]$ip="255.255.255.255",
|
|
||||||
[int]$port=9
|
|
||||||
)
|
|
||||||
$broadcast = [Net.IPAddress]::Parse($ip)
|
|
||||||
|
|
||||||
$mac=(($mac.replace(":","")).replace("-","")).replace(".","")
|
$mac=(($mac.replace(":","")).replace("-","")).replace(".","")
|
||||||
$target=0,2,4,6,8,10 | % {[convert]::ToByte($mac.substring($_,2),16)}
|
$target=0,2,4,6,8,10 | % {[convert]::ToByte($mac.substring($_,2),16)}
|
||||||
$packet = (,[byte]255 * 6) + ($target * 16)
|
$packet = (,[byte]255 * 6) + ($target * 16)
|
||||||
|
|
||||||
$UDPclient = new-Object System.Net.Sockets.UdpClient
|
$UDPclient = new-Object System.Net.Sockets.UdpClient
|
||||||
$UDPclient.Connect($broadcast,$port)
|
$UDPclient.Connect($broadcast,$port)
|
||||||
[void]$UDPclient.Send($packet, 102)
|
[void]$UDPclient.Send($packet, 102)
|
||||||
}
|
}
|
||||||
|
|
||||||
$MyMACAddresses = "io=11:22:33:44:55:66","pi=11:22:33:44:55:66"
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
foreach($item in $MyMACAddresses) {
|
if ($Hostname -eq "" ) {
|
||||||
$array = $item.Split("=")
|
$Hostname = read-host "Enter hostname or IP address"
|
||||||
$thisHost=$array[0]
|
|
||||||
$thisMAC=$array[1]
|
|
||||||
if ($thisHost -like $Hostname) {
|
|
||||||
Send-WOL $thisMAC
|
|
||||||
write-output "OK - host $thisHost waked up (MAC $thisMAC)"
|
|
||||||
exit 0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if ($MACaddress -eq "" ) {
|
||||||
write-output "Sorry, hostname $Hostname is unknown."
|
$MACaddress = read-host "Enter the MAC address"
|
||||||
pause
|
}
|
||||||
exit 1
|
Send-WOL $MACaddress $Hostname
|
||||||
|
write-output "OK - sent magic packet to $Hostname (MAC $MACaddress) to wake him up"
|
||||||
|
exit 0
|
||||||
} catch {
|
} catch {
|
||||||
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||||
exit 1
|
exit 1
|
||||||
|
Loading…
Reference in New Issue
Block a user