PowerShell 中的 If / Else 结构存在问题。我们的脚本适用于以前的 Windows 版本(直至 Windows Server 2012 R2),但不适用于较新的版本。
为了简单起见,我们以 PowerShell 帮助 ( Get-Help about_If) 中的一个示例为例:
if ($a -gt 2)
{
Write-Host "The value $a is greater than 2."
}
else
{
Write-Host "The value $a is less than or equal to 2, is not created or is not initialized."
}
Run Code Online (Sandbox Code Playgroud)
在 Windows Server 2012 R2(基于的 PowerShell 版本是 5.1.14409.1018)上$PSVersionTable.PSVersion,结果如下:
The value is less than or equal to 2, is not created or is not initialized.
Run Code Online (Sandbox Code Playgroud)
但是,较新的 PowerShell 版本,例如 5.1.14393.2879 (Windows Server 2016) 或 5.1.17763.503 (Windows Server 2019) 似乎不理解相同的语法:
else : The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Run Code Online (Sandbox Code Playgroud)
请注意,如果这些较新的 PowerShell 版本具有完全相同的 If/Then 示例,因此官方不应进行任何更改。
是的,我知道我可以轻松地将条件重写为:
if ($a -gt 2) {
Write-Host "The value $a is greater than 2."
} else {
Write-Host "The value $a is less than or equal to 2, is not created or is not initialized."
}
Run Code Online (Sandbox Code Playgroud)
但我们有很多脚本,如果有办法让 PowerShell 理解其他格式,我真的希望避免重新访问、修复和测试所有脚本。此行为是否已正式记录在 PowerShell 语言规范中,还是我们应该将其视为错误?
我们当前在基于网络的文档中提供的示例具有另一种格式,但它不适用于较新的版本(相同的错误):
if ($a -gt 2) {
Write-Host "The value $a is greater than 2."
}
else {
Write-Host ("The value $a is less than or equal to 2," + " is not created or is not initialized.")
}
Run Code Online (Sandbox Code Playgroud)
我找到了一个讨论类似问题的页面,但它与我们的情况并不完全匹配。
很烦人。提前感谢您的帮助。
更新 1:如果我将脚本保存到 .ps1 文件中并执行该文件,那么它似乎可以工作。如果我从编辑器(在我的例子中为记事本)复制/粘贴代码块,我只有一个问题。
更新 2:
$PSVersionTable结果:
Name Value
---- -----
PSVersion 5.1.17763.503
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.503
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Run Code Online (Sandbox Code Playgroud)
通过将记事本中的条件复制粘贴到 PowerShell 控制台中,我在较新版本中遇到此错误(在 Windows Server 2012 R2 中没有出现错误):
if ($a -gt 2)
>> {
>> Write-Host "The value $a is greater than 2."
>> }
PS C:\Scripts> else
else : The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ else
+ ~~~~
+ CategoryInfo : ObjectNotFound: (else:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)
更新 3:一如既往,细节决定成败。我使用鼠标右键将内容粘贴到控制台中,因为在 WS2012R2(及更早版本)中,用于粘贴的标准组合键Ctrl+V不起作用,并导致生成^V. 因此,我习惯了通过单击鼠标右键将脚本粘贴到控制台(在 WS2012R2 上,从上下文菜单中选择“粘贴”)。显然,粘贴功能的实现方式有所不同,并且右键单击内容会逐行解释,但在使用 粘贴时会解释为块Ctrl+V。好的。又学到了新东西。感谢大家的支持和想法!
在命令提示符下运行时,在任何版本中都是如此,但在脚本中运行时,它应该可以正常工作。我已经在 Server 2016 中用脚本确认了这一点,即使在其他之前有一个空行,并且采用 unicode、utf8 或 ansi 格式。
在命令提示符下演示,通过单击鼠标右键进行粘贴。使用control v 粘贴作品。
好的,如果我在 ISE 中使用 control v 进行粘贴,它实际上可以正常工作,不会出现错误。
PS C:\Users\js> if (0) { 'ran if' }
PS C:\Users\js> else { 'ran else' }
else : The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
At line:1 char:1
+ else { 'else' }
+ ~~~~
+ CategoryInfo : ObjectNotFound: (else:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18916 次 |
| 最近记录: |