-in 运算符在 2.0 版中不起作用?
-in operator not working in version 2.0?
我有以下在 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 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
有关的部分没有任何不同。