如何更改参数的抛出消息? [英] How do you change a parameter's throw message?

查看:49
本文介绍了如何更改参数的抛出消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:希望更改显示的错误消息以说明他们需要将-passed、-warning 或-failed"包含在 cmdlet 参数中,或者不关心这三个是否丢失并写入消息(假设它在那里).

Question: Looking to either change the error message displayed to say they need the '-passed, -warning, or -failed' to be included with the cmdlet parameters or to not care if the three are missing and to write the message (assuming its there).

说明:创建了一个接受三个参数的函数:消息、第二条消息和消息状态(传递、失败,警告).我收到一条默认错误消息无法使用指定的命名来解析参数参数."无论您是否通过以下方式传递消息,都会发生这种情况:

Explanation: Created a function that takes in three arguments: a message, 2nd message, and state of message (pass, fail, warn). I get a default error message of "Parameter cannot be resolved using the specified named parameters." This happens regardless of if you're passing a message through:

PS C:\> Write-Message;
Write-Message : Parameter set cannot be resolved using the specified named parameters.
...
//or
PS C:\> Write-Message -Message "Hello World";

但是如果您要输入状态"参数,它就可以正常工作:

But if you were to enter in the 'status' parameter it would work without any issues:

PS C:\> Write-Message -Message "Hello World" -Passed;
Hello World 

(这里的Hello World"应该是绿色,但 Stack Overflow 还没有这个功能)

('Hello World' is supposed to be the color Green here, but Stack Overflow doesn't have that feature yet)

有问题的功能:

Function Write-Message {
    param(
        [string]$Message,
        [string]$MessageTwo,
        [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName = $true, ParameterSetName ="Passed")][switch]$Passed,
        [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName = $true, ParameterSetName ="Warning")][switch]$Warning,
        [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName = $true, ParameterSetName ="Failed")][switch]$Failed
    );
    $NewLineNeeded = [string]::IsNullOrEmpty($MessageTwo);

    if ($Passed){
        $color = 'green';
    }
    if($Warning){
        $color = 'yellow';
    }
    if($Failed){
        $color = 'red';
    }

    if($Passed -or $Warning -or $Failed){
        if(!$NewLineNeeded){
            Write-Host $MessageOne -NoNewline;
            Write-Host $MessageTwo -ForegroundColor $color;
        } else {
            Write-Host $Message -ForegroundColor $color;
        }  
    }
}

推荐答案

添加默认"参数集来解决此问题.您实际上不必在代码中使用参数集,因此即使使用默认"之类的内容也无需对代码进行额外更改.我自己在编写更复杂的 cmdlet 时遇到过这种情况,其中我将参数集用于互斥参数,但有些参数与参数集无关.

Add a "default" parameter set to resolve this. You don't actually have to use the parameter set in your code, so even using something like "Default" would work without additional changes to your code. I've run into this myself when writing more complex cmdlets where I use parameter sets for mutually exclusive parameters, but some parameters are parameter-set agnostic.

Function Write-Message {
  [CmdletBinding(DefaultParameterSetName="Default")]
  Param(
...

当您只使用没有参数集的参数并且您还在 cmdlet 中定义了参数集时,Powershell 似乎对正在使用的参数集感到困惑.

It seems that Powershell gets confused about which parameter set is in use when you use only parameters without a parameter set, and you also have defined parameter sets in your cmdlet.

您只需要满足以下所有条件即可:

You only need to do this all of the following conditions are true:

  • 明确定义了两个或多个参数集
  • 尚未将现有参数集之一绑定为默认值
  • 具有(并使用)未绑定到任一集合的参数

正如 mklement0 的回答 中所解释的,以及一些关于发生的事情的幕后信息,这可能是各种错误,此解决方案是解决方法.

As explained in mklement0's answer, along with some under-the-hood information of what happens, this is possibly a bug of sorts and this solution is the workaround.

这篇关于如何更改参数的抛出消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆