Hello, I’m using a Powershell script to Wake on Lan my NAS. It’s working well on 2 of my PC’s. But strangely, it’s not working on my third PC. here is my current code. Any help would be appreciated :
$NASMac = "00:11:32:74:CF:98"
$NASStartupDurationInMn = 2.3
$nbrEAGlobal=1
if($ConnectedToRouter -eq 1) {
{
if($NASRunning -eq 0)
{
Write-Host "Waking up $NASName"
# Source of this code : https://www.pdq.com/blog/wake-on-lan-wol-magic-packet-powershell/
$MacByteArray = $NASMac -split "[:-]" | ForEach-Object { [Byte] "0x$_"}
[Byte[]] $MagicPacket = (,0xFF * 6) + ($MacByteArray * 16)
$UdpClient = New-Object System.Net.Sockets.UdpClient
$UdpClient.Connect(([System.Net.IPAddress]::Broadcast),7)
$UdpClient.Send($MagicPacket,$MagicPacket.Length)
$UdpClient.Close()
Write-Host "Waiting $NASStartupDurationInMn minutes for $NASName to startup..."
Start-Sleep -s ($NASStartupDurationInMn * 60)
Write-Host "Checking if $NASName is now running"
if(Test-Connection -Cn $NASName -BufferSize 16 -Count 1 -ea $nbrEAGlobal -quiet)
{
Write-Host "Connected to $NASName"
$NASRunning = 1
}
else
{
Write-Host "$NASName is not online"
$NASRunning = 0
}
}
}
}
In simple terms, are the first three octets of the IP address the same:
i.e. if the IP address of the NAS is 192.168.1.x, is the IP address of the PC 192.168.1.y
WoL packets aren’t routed so checking the subnet is a quick way of checking that the PC and NAS are on the same network segment (for home users, anyway).
have you confirmed that WoL is a feature of your NAS and if so, have you been able to wake it via any other method to prove out that the issue is within your PS script?
Wake on LAN is notoriously unreliable so I’m just trying to cover the basics here.
I looked at your code compared to the WoL project I wrote and they look like they effectively do the same thing. The only difference is I’m sending on port 9 instead of 7. I had to research this again quickly and the consensus was that it doesn’t really matter, but people generally recommended port 9.
Now it works from the guilty computer. I’m stunned because I didn’t change anything. The only thing that changed are some Windows updates. Well at least I know the code is not at fault. If it messes up again, I’ll let you know… Thanks all for the help.