4

我的脚本开头有以下 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 以及如何绕过重命名变量?

4

1 回答 1

4

PowerShell 具有普遍存在的参数,每个 cmdlet 都存在这些参数。

退房get-help about_common_parameters或点击这里

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

添加参数属性时,它会更改 PowerShell 处理参数的方式。那时它变成了一个高级功能,也就是脚本 cmdlet。脚本 cmdlet 会自动接收常用参数。

查看get-help about_Functions_Advanced或点击这里

get-help about_Functions_Advanced_Parameters或者点击这里

于 2012-03-01T02:17:21.953 回答