代码块阻止脚本运行,但以交互方式运行 [英] Block of code prevents script from running, but runs interactively
问题描述
我有一个作为计划任务运行的脚本,该脚本在定义 $As 的行上出现意外令牌错误而失败.如果我删除代码,脚本将正常运行.如果我将整个脚本(包括有问题的部分)粘贴到 PowerShell 窗口中,一切都会按预期运行.
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.
这在 Server 2012R2 上运行,使用 PS 5.0.117,但也在版本 4 下发生.
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
}
我收到的确切错误是:
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.
如果我注释掉 $As,它会发生在 $Ns 和 $Os 上.如果我注释掉 $As、$Ns 和 $Os,它运行良好.
If I comment out the $As, it happens with $Ns, and $Os. If I comment out $As, $Ns and $Os, it runs fine.
推荐答案
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'
^
这篇关于代码块阻止脚本运行,但以交互方式运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!