如何在Powershell中显示动态参数的.PARAMETER帮助消息?

如何在Powershell中显示动态参数的.PARAMETER帮助消息?,powershell,Powershell,对于许多功能,我在各个模块中利用了大量的动态参数 但是,“获取帮助”中未显示动态参数的.PARAMETER注释块 .PARAMETER Some details of the dynamic parameter that is defined in dynamicParam { block} 当通过此类函数调用get-help cmdlet时,是否有任何方法可以向用户提供动态参数详细信息 谢谢 在System.Management.Automation.ParameterAttribute上有

对于许多功能,我在各个模块中利用了大量的动态参数

但是,“获取帮助”中未显示动态参数的.PARAMETER注释块

.PARAMETER
Some details of the dynamic parameter that is defined in dynamicParam { block}
当通过此类函数调用get-help cmdlet时,是否有任何方法可以向用户提供动态参数详细信息


谢谢

System.Management.Automation.ParameterAttribute
上有一个
HelpMessage
属性,可用于在动态参数中设置帮助文本:

# Create a collection of attributes
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]

$ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
$ParameterAttribute.Mandatory = $true
$ParameterAttribute.Position = 0
$ParameterAttribute.HelpMessage = "YOUR_HELP_MESSAGE"

# Add the attributes to the attributes collection
$AttributeCollection.Add($ParameterAttribute)

如果您通过复制参数名并在其后面加空格来作弊,则可以达到一半:

param ([string]${Name })

DynamicParam{
    #insert usual stuff here
    New-Object System.Management.Automation.RuntimeDefinedParameter(
        'Name',  [string], $attributeCollection
    )
    # more stuff
}
begin
{
    $name = $PSBoundParamters.'Name '
}

这意味着您可以通过“获取帮助”获取参数名,但不能获取实际文本。这是一个可怕的黑客,但它让你走到了一半

当我使用ge thelp mFunction时,它不会返回帮助消息。请尝试获取帮助mFunction-full。在获取帮助期间,我看不到帮助消息。\script.ps1,即使我使用-full。这不是获取帮助的工作方式。您必须查询函数,而不是脚本。看起来,只有在完全缺少基于注释的帮助时,动态参数帮助才起作用,并且基于注释的帮助是将脚本绑定到Get help系统的选项。