如何让c# Cmdlet参数HelpMessage显示在“get - help”中

本文关键字:get help 显示 Cmdlet HelpMessage 参数 | 更新日期: 2023-09-27 18:14:03

我已经启动了一个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();
    }
}

但是当我使用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

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

<#
.Parameter Path
The local path to the root folder of the workspace.
#>

但是在c# cmdlet中等效的是什么?

如何让c# Cmdlet参数HelpMessage显示在“get - help”中

你做得完全正确!

您只需要检查-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