Trying to use WakeOnLan and keep getting errors

CoPilot was not helpful in trying to get WakeOnLan to function. Nothing seems to work. Does WakeOnLan command ever work. Using PowerShell 7.

I get these types of errors:
PS C:\Users\gharouff_dadmin.METROHEALTH> import-module wakeonlan

PS C:\Users\gharouff_dadmin.METROHEALTH> Invoke-wakeonlan -MacAddress 40:1c:83:40:ad:3f

InvalidArgument: Cannot convert value “0x40:1c:83:40:ad:3f” to type “System.Byte”. Error: "Additional non-parsable characters are at the

end of the string."

I used “-” instead of “:” and get the same error

Imported and installed WakeOnLan modules installed ok
Used Get-Command -Module WakeOnLan – returned command type – Function, Name – Invoke-WakeOnLan, version 1.0, source wakeonlan

I cannot reproduce. I recommend you type the mac instead of copy/pasting, it’s likely there are additional characters you can’t see.

Hello and welcome to the forum.
Whenever you’re sharing code please make sure to use the Preformatted text button. It’s often behind the gear icon.

The first thing I did was look up the module you’re using. WakeOnLane was published to the PowerShell Gallery in 2015 and written with Powershell v2 as a minimum. Just things to keep in mind.

InvalidArgument: Cannot convert value "0x40:1c:83:40:ad:3f" to type "System.Byte"

Your MAC address suddenly has a 0x40 at the beginning of it instead of your original “40”.
The param block from the source code takes your input in as a string:

Param (
    [Parameter(ValueFromPipeline)]
    [String[]]$MacAddress
)

And later converts it to bytes:

$Mac = $MacString.Split('-:') | Foreach {[Byte]"0x$_"}

 # WOL Packet is a byte array with the first six bytes 0xFF, followed by 16 copies of the MAC address

$Packet = [Byte[]](,0xFF * 6) + ($Mac * 16)

When I run just the relevant code in Windows Powershell v5.1 it works.
When I run it in Powershell 7.5 it fails with the error:

InvalidArgument: Cannot convert value "0x40:1c:83:40:ad:3f" to type "System.Byte". Error: "Additional non-parsable characters are at the end of the string."

I don’t know which part of this syntax it doesn’t like:

$Mac = $MacString.Split('-:') | Foreach {[Byte]"0x$_"}

But that’s your problem.

Seeing your PS prompt in your posted text we can see you’re on a Windows machine. I would advise using Windows Powershell to use this module instead.
I would also advise you edit your post and remove your username from your prompt as it reveals potentially sensitive information about your organization.

EDIT: got it. The author used the Split() method of a String object and included two characters to potentially split on: -, ;
In Windows Powershell v5.1, and .NET framework 4.5 that works:

# v5.1
PS> $MacString = "40:1c:83:40:ad:3f"
PS> $MacString.Split('-:')   
40
1c
83
40
ad
3f

But in PS v7.5 it doesn’t split at all:

# v7.5
PS> $MacString = "40:1c:83:40:ad:3f"
PS> $MacString.Split('-:')  
40:1c:83:40:ad:3f

The definition on [Microsoft’s docs]( String.Split Method (System) | Microsoft Learn) for both version of .NET says that Split will accept an array of characters as delimiters. But simply passing '-:' isn’t strictly an array, but somehow this worked in older versions of Powershell.
I was able to make it work in both versions by literally creating a character array.

$MacString = "40:1c:83:40:ad:3f"
[Char[]]$Chars = [Char]'-',[Char]':'
$MacString.Split($Chars)# | Foreach {[Byte]"0x$_"}

@LearnLaughOps solution of switching to the -Split operator with Regex pattern of This|That is what I would do.
You can edit your local copy of the module manually yourself, open an issue on github, or fork the project for yourself.

1 Like

To resolve this I think you can just edit the module and update the following line:
$Mac = $MacString.Split('-:') | Foreach {[Byte]"0x$_"}

Updated to:
$Mac = $MacString -split '-|:' | Foreach {[Byte]"0x$_"}

I can’t figure out what changed from version 5-7.

1 Like

Looks like a known problem. There is a fix, but the PR hasn’t been completed so there’s no updated version on the gallery.

Might be worth trying this branch of the fork from the author of the fix:

2 Likes

I see, I didn’t try it in powershell core, so that’s my mistake! OP you could use my function if you like, it works in 7.x as well

4 Likes

awesome @krzydoug I was just scaffolding out a new module project based on this conversation. Hope you don’t mind if I borrow some of your code.

Not at all. This is my number 1 visited script and most those visiting come from Reddit. At least you asked :stuck_out_tongue:

2 Likes

I spent longer on it than I intended but my first draft of a Wake-on-LAN module that’s compatible with Desktop and Core editions as well as cross platform is up.
PSWoL

1 Like