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



我为许多函数利用了各种模块中的许多动态参数。

但是,get help中没有显示动态参数的.PARAMETER注释块。

.PARAMETER
Some details of the dynamic parameter that is defined in dynamicParam { block}

当通过这样的函数调用get-help cmdlet时,是否有任何方法可以向用户提供动态参数详细信息?

谢谢!

如果你通过复制后面有空格的参数名称来作弊,你可以达到一半:

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

这意味着您可以通过get-Help获得参数名称,但不能获得实际文本。这是一个可怕的黑客攻击,但它会让你半途而废。。。

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)

相关内容

最新更新