When working with PowerShell, even small formatting issues can cause big problems. A common culprit is the use of “smart quotes,” which look correct but aren’t expected the ASCII character that PowerShell requires. This leads to frustrating command failures. In many cases his is because blogging software replaces the standard quote with a "smart quote" automatically. We can fix that with a WordPress plugin, but it's still something to be aware of. This post looks at a similar issue, but this time the problem wasn’t a quote at all, it was a hyphen. In this case, a non-standard dash character had silently replaced the regular ASCII hyphen, causing a perfectly normal-looking command to fail when pasted into PowerShell.
Dodgy PowerShell Code
If we look at the command that was pasted direct into Notepad, on the surface it looks OK.
Here is the command directly pasted:
Get-ExchangeServer | FOREACH {$Srv =$_.Name; Write-Host $Srv -ForegroundColor Magenta; (Get-WmiObject –Class Win32_Service -ComputerName $_.Name -Filter "StartMode='auto' AND State='stopped' AND (name > 'clra' or name < 'clr') AND (not Name like 'sppsvc') AND (not Name like 'edgeupdate')" | Set-Service -ComputerName $Srv -Status Running)}
While it may look OK, but on the surface, he looks calm and ready, there’s vomit on his sweater already, mom’s spaghetti...
In other words, brace for impact as this is not going to go well...
As expected, we had issues. Else why would there be a post?
The red arrow in the above image shows there is a missing hyphen before the work "Class".
If we go back and really, really look at the Notepad and pasted text there is an issue. It is subtle, but the hyphen before the word "Class" is different. It is not a valid ASCII character, rather it is a Unicode character.
Validate ASCII Charater
Let's take what was in that command and paste it into one of the ASCII character mapping tools.
Validate ASCII – Online ASCII Tools (onlinetools.com)
That's not a valid ASCII character. So what is it?
Time to go and check against Unicode.
What Unicode character is this ? (babelstone.co.uk)
Here you can see that it is mapped to a Unicode hyphed (dash) character rather than ASCII. These characters are different.
As a quick summary:
- Unicode+2013 is part of Unicode, so it supports international text standards and is not limited to ASCII
- EN DASH (–) is longer than the ASCII hyphen (-) and looks more typographically correct for ranges
- ASCII dash is the simplest and most common because it’s in the basic ASCII set
Cheers,
Rhoderick