Block of code prevents script from running, but runs interactively

南楼画角 提交于 2019-11-27 07:57:54

问题


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 properly. If I paste the whole script (including the problematic section) into a PowerShell window everything runs as expected.

I'm assuming this is a simple gotcha that I've just not encountered, but I cannot figure out what the problem is with it, more experienced eyes would be appreciated.

This is being run on Server 2012R2, with PS 5.0.117 but also happened under version 4.

# Sanitize $UserLogon
$Garbage = "[?\' ]",''
$As = '[?ÀÁÂÃÄÅÆàáâãäåæ]','a'
$Cs = '[?Çç]','c'
$Es = '[?ÈÉÊËèéêë]','e'
$Is = '[?ÌÍÎÏìíîï]','i'
$Ns = '[?Ññ]','n'
$Os = '[?ÒÓÔÕÖØðòóôõöø]','o'
$Ss = '[?ß]','s'
$Us = '[?ÙÚÛÜùúûü]','u'
$Thorns = '[?Þþ]','th'

$TextReplacers = $Garbage, $As, $Cs, $Es, $Is, $Ns, $Os, $Ss, $Us, $Thorns

foreach ($Replacement in $TextReplacers) {
    $UserLogon = $UserLogon -replace $Replacement
    }

The exact error I receive is:

At C:\Scripts\Onboarding\CreateUserAccount0.ps1:121 char:17
+     $As = '[?ÀÃÂÃÄÅÆàáâãäåæ]','a'
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Unexpected token 'ÃÄÅÆàáâãäåæ]','a'
    $Cs = '[?Çç]','c'
    $Es = '[?ÈÉÊËèéêë]','e'
    $Is = '[?ÃŒÃÃŽÃìíîï]','i'
    $Ns = '[?Ññ]','n'
    $Os = '[?ÒÓÔÕÖØðòóôõöø]','o'
    $Ss = '[?ß]','s'
    $Us = '[?ÙÚÛÜùúûü]','u'
    $Thorns = '[?Þþ]','th'

    $TextReplacers = $Garbage, $As, $Cs, $Es, $Is, $Ns, $Os, $Ss, $Us, $Thorns

    foreach ($Replacement in $TextReplacers) {
        $UserLogon = $UserLogon -replace $Replacement
        }
# Check if AD user already exists.
$UserExists = Get-ADUser -Filter {SamAccountName -eq $UserLogon}
if ($UserExists -ne $Null){
    $email = new-object Net.Mail.SMTPClient($mailServer)
    $err += "$UserLogon' in expression or statement.

If I comment out the $As, it happens with $Ns, and $Os. If I comment out $As, $Ns and $Os, it runs fine.


回答1:


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'
          ^


来源:https://stackoverflow.com/questions/37219078/block-of-code-prevents-script-from-running-but-runs-interactively

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!