The below one will fail in a script, both both work from the PowerShell prompt:
Success
Get-NetFirewallRule -DisplayGroup "File and Printer Sharing" | ForEach-Object { Write-Host $_.DisplayName ; Get-NetFirewallAddressFilter -AssociatedNetFirewallRule $_ }
Failure
Get-NetFirewallRule –DisplayGroup "File and Printer Sharing" | ForEach-Object { Write-Host $_.DisplayName ; Get-NetFirewallAddressFilter -AssociatedNetFirewallRule $_ }
The error you get this this:
At C:\bin\Show-File-and-Printer-Sharing-firewall-rules.ps1:5 char:52 + ... -TCP-NoScope" | ForEach-Object { Write-Host $_.DisplayName ; Get-NetF ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The string is missing the terminator: ". + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
Via [WayBack] script file 'The string is missing the terminator: ".' - Google Search, I quickly found these that stood out:
Cause and solution
Before DisplayGroup, the first line has a minus sign and the second an en-dash. You can see this via [WayBack] What Unicode character is this ?.
Apparently, when using Unicode on the console, it does not matter if you have a minus sign (-), en-dash (–), em-dash (—) or horizontal bar (―) as dash character. You can see this in [WayBack] tokenizer.cs at function [WayBack] NextToken and [WayBack] CharTraits.cs at function [WayBack] IsChar).
When saving to a non-Unicode file, it does matter, even though it does not display as garbage in the error message.
Similarly, PowerShell has support for these special characters:
internal static class SpecialChars { // Uncommon whitespace internal const char NoBreakSpace = (char)0x00a0; internal const char NextLine = (char)0x0085; // Special dashes internal const char EnDash = (char)0x2013; internal const char EmDash = (char)0x2014; internal const char HorizontalBar = (char)0x2015; // Special quotes internal const char QuoteSingleLeft = (char)0x2018; // left single quotation mark internal const char QuoteSingleRight = (char)0x2019; // right single quotation mark internal const char QuoteSingleBase = (char)0x201a; // single low-9 quotation mark internal const char QuoteReversed = (char)0x201b; // single high-reversed-9 quotation mark internal const char QuoteDoubleLeft = (char)0x201c; // left double quotation mark internal const char QuoteDoubleRight = (char)0x201d; // right double quotation mark internal const char QuoteLowDoubleLeft = (char)0x201E; // low double left quote used in german. }
The easiest solution is to use minus signs everywhere.
Another solution is to save files as Unicode UTF-8 encoding (preferred) or UTF-16 encoding (which I dislike).
--jeroen
No comments:
Post a Comment