PSScriptAnalyzer icon indicating copy to clipboard operation
PSScriptAnalyzer copied to clipboard

Warn against unicode characters

Open shaneing opened this issue 6 years ago • 10 comments

Summary of the new feature

When using the double quote in Chinese, PSSA should throw an error or warning.

Proposed technical implementation details (optional)

Three cases as follows:

Invoke-ScriptAnalyzer -ScriptDefinition '$a = "b“; Write-Output $a'
Invoke-ScriptAnalyzer -ScriptDefinition '$a = “b“; Write-Output $a'
Invoke-ScriptAnalyzer -ScriptDefinition '$a = “b"; Write-Output $a'

What is the latest version of PSScriptAnalyzer at the point of writing

v1.18.3

shaneing avatar Oct 12 '19 03:10 shaneing

Why? What is the problem with double quotes in Chinese?

bergmeister avatar Oct 12 '19 05:10 bergmeister

Why? What is the problem with double quotes in Chinese?

I run the script in powershell 4.0 (Windows Server 2012 R2) and its output as follows:

+ $a = "b�Write-Output $a
+      ~~~~~~~~~~~~~~~~~~~
The string is missing terminator:
    + CategoryInfo          : ParserError: (:) [], ParseExcept
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

shaneing avatar Oct 12 '19 06:10 shaneing

Is this a true double quote or a non-standard unicode character that just looks like a double quote? What is the behaviour for PowerShell 5.1 or 6.2? Is this known @rjmholt ?

bergmeister avatar Oct 12 '19 06:10 bergmeister

It's no problem with PowerShell 6.2.

shaneing avatar Oct 12 '19 07:10 shaneing

It's because of the encoding of the script. You want PSUseBOMForUnicodeEncodedFile.

The script is in UTF-8, but Windows PowerShell 5.1 and under default to an extended ASCII encoding. PS 6+ handles it fine because it defaults to UTF-8 when there's no BOM. The character itself is a perfectly legal string terminator in all versions of PowerShell.

If you re-save your script as UTF-8 with BOM, WinPS will pick that up and it will work. Please be mindful of source encoding; the PowerShell interpreter only sees bytes, so it's something you must explicitly handle in your editor and the way in which you move scripts around. If you share code, ensure that everyone is saving the file with a portable encoding (the ISE is not good for this).

rjmholt avatar Oct 14 '19 16:10 rjmholt

It's because of the encoding of the script. You want PSUseBOMForUnicodeEncodedFile.

The script is in UTF-8, but Windows PowerShell 5.1 and under default to an extended ASCII encoding. PS 6+ handles it fine because it defaults to UTF-8 when there's no BOM. The character itself is a perfectly legal string terminator in all versions of PowerShell.

If you re-save your script as UTF-8 with BOM, WinPS will pick that up and it will work. Please be mindful of source encoding; the PowerShell interpreter only sees bytes, so it's something you must explicitly handle in your editor and the way in which you move scripts around. If you share code, ensure that everyone is saving the file with a portable encoding (the ISE is not good for this).

ok, I get it now, thanks. The reason is that the script automated encoding conversion to UTF-8 when I input the double quote in Chinese uncarefully. I do not recommend using it. So ...

shaneing avatar Oct 15 '19 10:10 shaneing

I do not recommend using it. So ...

That's a fair point. A rule that warns against obscure but accepted characters in PowerShell scripts might be a good idea. Particularly long dashes and styled quotes, which tend to come from MS Word.

rjmholt avatar Oct 15 '19 17:10 rjmholt

There is already an issue with a viable solution where someone made a vs code extension, see here https://github.com/PowerShell/PSScriptAnalyzer/issues/981#issuecomment-420852777 https://marketplace.visualstudio.com/items?itemName=GlenBuktenica.unicode-substitutions

There is also another vs code extension to highlight dodgy characters https://marketplace.visualstudio.com/items?itemName=nachocab.highlight-dodgy-characters

bergmeister avatar Oct 15 '19 18:10 bergmeister

Going to close this as a duplicate of https://github.com/PowerShell/PSScriptAnalyzer/issues/981

rjmholt avatar Oct 15 '19 18:10 rjmholt

I coincidentally bounced into this request (which is still open😃) Apparently, I missed this one while I opened a similar request: #1999 again...

iRon7 avatar Jul 31 '25 09:07 iRon7