繁体   English   中英

Powershell:同名参数为命令定义了多次

[英]Powershell: A parameter with the name was defined multiple times for the command

任何想法,如何让开关与 Powershell 一起运行?

myscript.ps1 -Debug

为该命令多次定义了名为“Debug”的参数。

我的脚本

    [CmdletBinding()]
    Param (
        [switch]$Debug
    )
    
    if ($Debug) {
     Start-Transcript -Path "Debug.log" -Append -Force
}

当您创建高级 function 或使用CmdletBinding属性或Parameter属性的脚本时,PowerShell 会向您的 ZC1C425268E68385D1AB5074C17A94F1 添加常用参数,包括-Debug 所以你不应该自己添加这些。

要测试-Debug通用参数,检查相关首选项变量$DebugPreference的值会更灵活。 这是因为-Debug覆盖了脚本的 scope 中的$DebugPreference

[CmdletBinding()]
Param ()
    
if( $DebugPreference -eq 'Continue' ) {
    Start-Transcript -Path "Debug.log" -Append -Force
}

现在您可以使用参数为您的脚本启用调试日志记录...

.\myscript.ps1 -Debug

...或偏好变量:

$DebugPreference = 'Continue'
.\myscript.ps1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM