我有以下代码行可在 powershell 3.0 中运行
$Type = "DD"
if ($Type -in "AA","BB","CC") {Write-Host "Type = $Type" -ForegroundColor Yellow }
但是使用 PowerShell 2.0 运行时出现错误
- : You must provide a value expression on the right-hand side of the '-' operator.
+ CategoryInfo: ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedValueExpression
为什么会这样呢?我该如何修复它?
最佳答案
-in
在 2.0 中不存在。您需要使用-contains
相反(以及相应的箔 -notin
/-notcontains
)。功能相同,但直观性稍差一些。这就是引入 -in
的原因之一。
if ("AA","BB","CC" -contains $Type){"Do Stuff"}
虽然链接的文档适用于 3.0 及更高版本,但我不认为与 -contains
相关的部分有任何不同。
关于powershell - -in 运算符在 2.0 版本中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32632876/