Powershell 在Param中使用[ValidateScript]会导致有关已声明参数的错误
我在脚本的开头有以下参数块:Powershell 在Param中使用[ValidateScript]会导致有关已声明参数的错误,powershell,param,Powershell,Param,我在脚本的开头有以下参数块: Param ( [Parameter(Mandatory = $true)] [ValidateScript({Test-Path $_ -PathType Leaf})] [string]$Config, [switch]$OverThresholdOnly, [switch]$SendEmail, [switch]$Debug ) 当我运行脚本时,会出现以下错误: "A parameter with the n
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以及如何克服重命名变量的不足?PowerShell具有普遍存在的参数,适用于每个cmdlet 查看
获取有关常用参数的帮助
或单击
-Debug
和-Verbose
是两个常用参数。选择其他名称以避免命名冲突
添加参数属性时,会更改PowerShell处理参数的方式。此时它将成为一个高级函数,也称为脚本cmdlet。脚本cmdlet自动接收公共参数
查看获取有关功能的帮助\u Advanced
或单击
和
获取有关函数的帮助\u高级\u参数
或单击我之所以使用这些参数是因为它们很常见。坚持一个共同的设置,让生活更轻松。我还检查了他们没有预订。即使$Debug参数没有被[ValidateScript]验证,它仍然阻止我在自己的脚本中使用调试参数?@Pauby你看到我写的了吗?当您添加参数属性时,[parameter(Mandatory=$true)]
PowerShell会自动添加公共参数,因此您会遇到命名冲突。