0

我试图验证一个名为“收集”的参数只接受 3 个参数(基本、中等和完整),但是当我为“收集”参数分配一个“有效”值时,它得到一个“假”返回。

这就是我所做的:

[CmdLetBinding()]
param([string]$Collect)
)

if ($collect -ne ('basic' -or 'medium' -or 'full')) {
  Write-Host "'collect' is mandatory with mandatory values. For reference, use -help argument" -ForegroundColor Red
  exit
}

运行测试:

c:\script.ps1 -收集基本

'collect' is mandatory with mandatory values. For reference, use -help argument

PD:-我知道我可以使用 validateset,但这对我不起作用。-我认为问题出在嵌套的 $collect -ne ('basic' -or 'medium' -or 'full')中,但我该如何解决呢?

4

1 回答 1

0

-or操作总是评估为[bool]- 所以你的条件if基本上是$collect -ne $true

您将要使用-notin而不是-ne

if($collect -notin 'basic','medium','full'){ 
   # ...
}

或者更好的是,只需使用一个ValidateSet属性:

param(
  [ValidateSet('basic','medium','full')]
  [string]$Collect
)
于 2021-05-25T16:44:34.713 回答