且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

代码块阻止脚本运行,但以交互方式运行

更新时间:2023-10-26 11:35:22

PowerShell 可以从脚本文件 BOM 中检测以下编码:UTF-8、UTF-16(LE 和 BE)和 UTF-32(LE 和 BE).如果 BOM 不存在,则 PowerShell 使用 Encoding.Default 用于脚本文件.因此,您的 UTF-8 脚本文件应包含用于识别 UTF-8 的 BOM.

PowerShell can detect following encodings from script file BOM: UTF-8, UTF-16 (LE and BE) and UTF-32 (LE and BE). If BOM is not present, then PowerShell use Encoding.Default for script file. So that, your UTF-8 script file should include BOM for UTF-8 to be recognized.

在您的情况下,错误发生是由于 PowerShell 将以下所有字符解释为:'''‚‛ — 作为单引号字符.因此,当以错误的编码读取您的脚本文件时,字符串文字的某些部分会获得特殊含义并导致语法违规.

In your case, error happens due to PowerShell interpret all following characters: '‘’‚‛ — as single quote character. So, when your script file was read with incorrect encoding, some parts of what was string literals obtain special meaning and cause syntax violation.

$As = '[?ÀÃÂÃÄÅÆà áâãäåæ]','a'
             ^
$Ns = '[?Ññ]','n'
          ^
$Os = '[?Ã’Ã"Ã"ÕÖØðòóôõöø]','o'
          ^