我有以下代码行在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
的部分有任何不同。