向自定义方法添加文档(ScriptMethod)

Sh4*_*4pe 8 powershell powershell-2.0 powershell-3.0

可以使用标准注释在其正文的开头记录自定义PowerShell脚本或函数:

function wellDocumented {
   <#
       .SYNOPSIS
       A well documented function
       .DESCRIPTION
       "Be verbose in documentation" - someone
       .PARAMETER foo
        should be 42 or 15, but not more
    #>
    param( [int]$foo )
    <# ... #>
}
Run Code Online (Sandbox Code Playgroud)

Get-Help wellDocumented然后返回一些不错的信息.但是如何ScriptMethod在自定义对象中记录自定义?以下不起作用:

$myClass | add-member -memType ScriptMethod -Name "Foo" -Value {
    <#
        .SYNOPSIS
        Something - but brief
        .DESCRIPTION
        Something
    #>
    <# ... #>
}
Run Code Online (Sandbox Code Playgroud)

是否有一些标准方法来记录您的ScriptMethods?

bri*_*ist 2

您可以首先将脚本方法编写为单独的函数(就像使用 一样wellDocumented),然后将该函数作为脚本块传递:

$myClass | add-member -memType ScriptMethod -Name "Foo" -Value ${function:wellDocumented}
Run Code Online (Sandbox Code Playgroud)

你仍然不能打电话Get-Help $myClass.Foo,但你可以打电话Get-Help wellDocumented

在我的测试中,我无法获得有关方法的帮助。