-in运算符不能在2.0版中运行?

问题描述 投票:2回答:1

我有以下代码行在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

为什么会这样?我该如何解决?

powershell powershell-v2.0
1个回答
3
投票

-in不存在于2.0中。你需要使用-contains(和相应的箔-notin / -notcontains)。相同的功能,但只是不太直观。这是引入-in的原因之一。

if ("AA","BB","CC" -contains $Type){"Do Stuff"}

虽然链接的文档是3.0及更高版本,但我不相信有关-contains的部分有任何不同。

© www.soinside.com 2019 - 2024. All rights reserved.