Powershell 请解释此LAN唤醒脚本是如何工作的

Powershell 请解释此LAN唤醒脚本是如何工作的,powershell,wake-on-lan,Powershell,Wake On Lan,几个月前我在博客上发现了这个PowerShell代码。它将LAN唤醒信号发送到您选择的MAC地址,而无需使用外部程序。我在博客上发表了评论,并要求作者描述脚本背后的逻辑,因为我对它很好奇。稍后我又回到了这篇博文,看看作者是否回复了我的评论。我很惊讶地看到,我被重定向到一个页面,作者说他因为崩溃而丢失了他的博客。我记不起它的细节了,但我想我再也没有把那个博客放进书签了 现在我想请Stack Overflow的聪明人看看这段代码,并向我解释它的逻辑。每行加一条评论就太棒了。我很想知道这是怎么回事。它

几个月前我在博客上发现了这个PowerShell代码。它将LAN唤醒信号发送到您选择的MAC地址,而无需使用外部程序。我在博客上发表了评论,并要求作者描述脚本背后的逻辑,因为我对它很好奇。稍后我又回到了这篇博文,看看作者是否回复了我的评论。我很惊讶地看到,我被重定向到一个页面,作者说他因为崩溃而丢失了他的博客。我记不起它的细节了,但我想我再也没有把那个博客放进书签了

现在我想请Stack Overflow的聪明人看看这段代码,并向我解释它的逻辑。每行加一条评论就太棒了。我很想知道这是怎么回事。它似乎比我发现的其他脚本更健壮,因为它跨子网工作。不过,我对人际网络知之甚少

我最好奇的事情之一是结尾的for循环。为什么要多次发送信号?为什么在不同的港口?但我真的很想知道整个剧本背后的逻辑

守则:

param(
$targetMac,
$network=[net.ipaddress]::广播,
$subnet=[net.ipaddress]::广播
)
试一试{
if($network.gettype().equals([string])){
$network=[net.ipaddress]::Parse($network);
}
如果($subnet.gettype().equals([string])){
$subnet=[net.ipaddress]::解析($subnet);
}
$broadcast=新对象net.ipaddress(([system.net.ipaddress]::parse(“255.255.255.255”).address-bxor$subnet.address-bor$network.address))
$mac=[Net.NetworkInformation.PhysicalAddress]::Parse($targetMac.toupper().replace(“,”))
$u=新对象net.sockets.udpclient
$ep=新对象net.ipendpoint$broadcast,0
$ep2=新对象net.ipendpoint$broadcast,7
$ep3=新对象net.ipendpoint$broadcast,9
$payload=[byte[]@(255255,255255);
$payload+=($mac.GetAddressBytes()*16)
对于($i=0;$i-lt 10;$i++){
$u.Send($payload,$payload.Length,$ep)|输出空值
$u.Send($payload,$payload.Length,$ep2)|输出空值
$u.Send($payload,$payload.Length,$ep3)|输出空值
睡眠1;
}
}抓住{
$Error |写入错误;
}

维基百科对局域网唤醒有很好的解释:

那篇文章的摘录:

[The Magic Packet] is typically sent as a UDP datagram to port 7 or 9, but actually it can be sent on any port. [魔法包]通常作为UDP数据报发送到端口7或9,但实际上它可以是 发送到任何端口。
我的猜测之所以多次发送数据包,是因为偶尔会发生网络冲突。如果发生冲突,则必须重新发送数据包。

非常感谢您的帮助!详细阅读Wiki链接:“…然后是目标计算机48位MAC地址的16次重复…”。非常感谢!一、 我也认为没有必要发送十个魔法包。我想知道剧本的作者是否有这样的理由。哦,是这样吗?
#These are the parameters to the script. The only mandatory param here is the mac address
#[net.ipaddress]::Broadcast will resolve to something like 255.255.255.255
param (
    $targetMac,
    $network = [net.ipaddress]::Broadcast,
    $subnet = [net.ipaddress]::Broadcast
)

#We start the try, catch error handling here.
#if something in try block fails, the catch block will write the error
try {

#This will evaludate to False. Hence, $network will have whatever was passed through params or the default value
#in this case the default value is 255.255.255.255
    if($network.gettype().equals([string])) {
        $network = [net.ipaddress]::Parse($network);
    }

#This will evaludate to False. Hence, $network will have whatever was passed through params or the default value
#in this case the default value is 255.255.255.255    
    if($subnet.gettype().equals([string])) {
        $subnet = [net.ipaddress]::Parse($subnet);
    }

    #Not sure if this is really required here. But, assuming that the default value for both $network and $subet is 255.255.255.255,
    #this will result in $broadcast set to 255.255.255.255
    $broadcast = new-object net.ipaddress (([system.net.ipaddress]::parse("255.255.255.255").address -bxor $subnet.address -bor $network.address))

#This again assumes that you had given . as the delimeter in MAC address and removes that from MAC address    
    $mac = [Net.NetworkInformation.PhysicalAddress]::Parse($targetMac.toupper().replace(".",""))

#Create a new object of type net.sockets.udpclient
    $u = New-Object net.sockets.udpclient

#WOL magic packet can be sent on port 0, 7, or 9    
#Create a end point for the broadcast address at port 0    
    $ep = New-Object net.ipendpoint $broadcast, 0

#Create a end point for the broadcast address at port 7    
    $ep2 = New-Object net.ipendpoint $broadcast, 7

#Create a end point for the broadcast address at port 9    
    $ep3 = New-Object net.ipendpoint $broadcast, 9

#Create a payload packet
#First, create a byte array
    $payload = [byte[]]@(255,255,255, 255,255,255);

#add the mac address to the above byte array    
    $payload += ($mac.GetAddressBytes()*16)

#Send 10 magic packets for each port number or end point created above.
#one is more than enough. If everything is congfigured properly
    for($i = 0; $i -lt 10; $i++) {
        $u.Send($payload, $payload.Length, $ep) | Out-Null
        $u.Send($payload, $payload.Length, $ep2) | Out-Null
        $u.Send($payload, $payload.Length, $ep3) | Out-Null
        sleep 1;
    }
} catch {
#catch block catches any error from try block
    $Error | Write-Error;
}