Sha*_*awn 2 powershell powershell-2.0
我有以下代码行在 powershell 3.0 中工作
$Type = "DD"
if ($Type -in "AA","BB","CC") {Write-Host "Type = $Type" -ForegroundColor Yellow }
Run Code Online (Sandbox Code Playgroud)
但是,使用 PowerShell 2.0 运行时出现错误
- : You must provide a value expression on the right-hand side of the '-' operator.
+ CategoryInfo: ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedValueExpression
Run Code Online (Sandbox Code Playgroud)
为什么呢?我该如何解决?
-in2.0 中不存在。您需要-contains改用(以及相应的箔片-notin/ -notcontains)。相同的功能,但不那么直观。这也是-in被介绍的原因之一。
if ("AA","BB","CC" -contains $Type){"Do Stuff"}
Run Code Online (Sandbox Code Playgroud)
虽然链接的文档适用于 3.0 及更高版本,但我认为相关部分没有-contains任何不同。
| 归档时间: |
|
| 查看次数: |
1198 次 |
| 最近记录: |