如何获取C#Cmdlet参数HelpMessage以显示在“获取帮助”中

And*_*ott 4 c# powershell cmdlet

我已经启动了PowerShell cmdlet,并想提供参数的帮助消息。我试过使用ParameterAttribute.HelpMessage这个:

[Cmdlet(VerbsCommon.Get, "Workspace", SupportsShouldProcess = true)]
public class GetWorkspace : PSCmdlet
{
    [Parameter(
        Mandatory = true,
        Position = 1,
        HelpMessage = "The path to the root directory of the workspace.")]
    public string Path { get; set; }

    protected override void ProcessRecord()
    {
        base.ProcessRecord();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我使用PowerShell Get-Help命令时,它不会显示该参数的HelpMessage:

get-help Get-Workspace -det

NAME
    Get-Workspace

SYNTAX
    Get-Workspace [-Path] <string> [-WhatIf] [-Confirm]  [<CommonParameters>]


PARAMETERS
    -Confirm

    -Path <string>

    -WhatIf

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer, PipelineVariable, and OutVariable. For more information, see
        about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).


ALIASES
    None


REMARKS
    None
Run Code Online (Sandbox Code Playgroud)

在PowerShell脚本中编写cmdlet时,我可以使用以下语法为参数添加帮助消息:

<#
.Parameter Path
The local path to the root folder of the workspace.
#>
Run Code Online (Sandbox Code Playgroud)

但是C#cmdlet中的等效项是什么?

Mat*_*sen 5

您做的绝对正确!

您只需要使用以下命令检查-Full视图或获取有关特定参数的帮助-Parameter

PS C:\> Get-Help Get-Workspace -Parameter Path

-Path <string>
    The path to the root directory of the workspace.

    Required?                    true
    Position?                    1
    Accept pipeline input?       false
    Parameter set name           (All)
    Aliases                      None
    Dynamic?                     false
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。但我认为我不必用 ps1 cmdlet 来做这件事。只需`Get-Help Get-Workspace -det` 就足以看到参数帮助信息。这对于 C# cmdlet 是不是可以实现的? (2认同)