在Param中使用[ValidateScript]导致已经声明的参数出错

时间:2012-02-29 23:04:09

标签: powershell param

我的脚本开头有以下Param块:

Param
(
    [Parameter(Mandatory = $true)]
    [ValidateScript({Test-Path $_ -PathType Leaf})]
    [string]$Config,

    [switch]$OverThresholdOnly,
    [switch]$SendEmail,
    [switch]$Debug
)

当我运行脚本时,我收到错误:

"A parameter with the name 'Debug' was defined multiple times for this command. At line:1 char:1"

Line:1 and char:1 is the start of the Param block.

如果我将$ Debug更改为$ Verbose,我会得到与Verbose相同的错误。我已经尝试将$ debug放在Param块的顶部并出现相同的错误。

如果删除[ValidateScript]部分,它可以正常工作。

有谁可以告诉我它为什么这样做?为什么[ValidateScript]使用$ Debug以及如何解决这个变量的重命名?

1 个答案:

答案 0 :(得分:4)

PowerShell的每个cmdlet都有无处不在的参数。

结帐get-help about_common_parameters或点击here

-Debug-Verbose是两个常见参数。选择一个不同的名称以避免命名冲突。

添加参数属性时,它会改变PowerShell处理参数的方式。它成为一个高级函数a.k.a脚本cmdlet。脚本cmdlet自动接收公共参数。

结帐get-help about_Functions_Advanced或点击here

get-help about_Functions_Advanced_Parameters或点击here

相关问题