简体   繁体   English

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

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

Any idea, how to get a Switch running with Powershell?任何想法,如何让开关与 Powershell 一起运行?

myscript.ps1 -Debug

A parameter with the name 'Debug' was defined multiple times for the command.为该命令多次定义了名为“Debug”的参数。

My Script我的脚本

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

When you create an advanced function or script that uses the CmdletBinding attribute or the Parameter attribute, PowerShell adds common parameters , including -Debug to your function.当您创建高级 function 或使用CmdletBinding属性或Parameter属性的脚本时,PowerShell 会向您的 ZC1C425268E68385D1AB5074C17A94F1 添加常用参数,包括-Debug So you should not add these on your own.所以你不应该自己添加这些。

To test for the -Debug common parameter, it is more flexible to check the value of the related preference variable $DebugPreference .要测试-Debug通用参数,检查相关首选项变量$DebugPreference的值会更灵活。 This works because -Debug overrides $DebugPreference within the scope of the script.这是因为-Debug覆盖了脚本的 scope 中的$DebugPreference

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

Now you can enable debug logging for your script either using the parameter...现在您可以使用参数为您的脚本启用调试日志记录...

.\myscript.ps1 -Debug

... or the preference variable: ...或偏好变量:

$DebugPreference = 'Continue'
.\myscript.ps1

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

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