PowerShell error in a script but not on the console: The string is missing the terminator: “.
Posted by jpluimers on 2021/09/29
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:
- [WayBack] Reddit: Getting error- the string is missing the terminator: “. : PowerShell
That hyphen character in your
-Reset
(noticed by /u/SeeminglyScience) is an En-Dash (Decimal unicode character 8211). (Copy paste it and do[int]([char]'–')
to see).PowerShell can handle en-dash as a hyphen (for the curious, see the case for it in the PowerShell tokenizer source code here where hyphen, en-dash, em-dash and horizontalBar all fall into the same code block), but because it’s a Unicode character your source code will have to be saved as a unicode encoded .ps1 file. (UCS2-LE, UTF8+BOM, UTF8-BOM seem to work for me).
If you don’t, and you save it as ASCII/ANSI, what gets saved is
–
which has a string opening in it. So the code becomes:Set-ADAccountPassword $uname -NewPassword $newpwd –Reset -confirm -PassThru
Which now has an open string that doesn’t finish, and throws errors about unterminated strings.
- [WayBack] azure – PowerShell script error: the string is missing the terminator: – Stack Overflow
- [WayBack] powershell is missing the terminator: ” – Stack Overflow
Look closely at the two dashes in
unzipRelease –Src '$ReleaseFile' -Dst '$Destination'
This first one is not a normal dash but an en-dash (
–
in HTML). Replace that with the dash found beforeDst
.
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
Leave a Reply