Block of code prevents script from running, but runs interactively

后端 未结 1 491
[愿得一人]
[愿得一人] 2020-12-11 21:23

I have a script that is run as a scheduled task which fails with an unexpected token error on on the line where $As is defined. If I remove the code, the script runs properl

相关标签:
1条回答
  • 2020-12-11 21:31

    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.

    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'
              ^
    
    0 讨论(0)
提交回复
热议问题