gpt4 book ai didi

powershell - 在 Param 中使用 [ValidateScript] 会导致关于已声明参数的错误

转载 作者:行者123 更新时间:2023-12-03 12:41:33 26 4
gpt4 key购买 nike

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

最佳答案

PowerShell 具有无处不在的参数,这些参数存在于每个 cmdlet 中。

退房 get-help about_common_parameters或点击 here .
-Debug-Verbose是两个常用参数。选择不同的名称以避免命名冲突。

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

退房 get-help about_Functions_Advanced或点击 here

get-help about_Functions_Advanced_Parameters或点击 here

关于powershell - 在 Param 中使用 [ValidateScript] 会导致关于已声明参数的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9508072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com