Wake On Lan works on 2 PC's but not on a third one

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
			}
		}
	}
}

What do you mean by ‘not working’? Do you get any error messages?

Where are $ConnectedToRouter, $NASRunning, and $NASName defined?

You have a closing brace that isn’t opened.

Well no error message but the NAS does not start and my own Test-Connection returns “$NASName is not online”.

These variable are defined in the top of the script.

Did you make sure wake on LAN is enabled in the BIOS on that one?

Is the 3rd PC on the same subnet as the NAS?

Yes it is enabled as the same script works on 2 other PC’s. Subnet? What’s this? Where do I set it up?

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).

Thanks Matt. Yes, my PC and my NAS have IP’s both starting by 192.168.0.

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.

Hello all,

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.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.