使用Get-Help cmdlet以相同格式显示基于注释的帮助

时间:2011-01-12 22:11:32

标签: powershell powershell-v2.0

我正在尝试使用Get-Help cmdlet以相同的格式显示基于注释的帮助,在该格式中,它显示从XML文件生成的cmdlet帮助主题。 TechNet上的about_Comment_based_Help中记录了执行此操作的能力,但是当我针对我的脚本执行get-help cmdlet时,我只返回了返回的脚本名称。任何帮助将不胜感激!

PS C:\Admin> Get-Help .\checksystem.ps1 -full
checksystem.ps1

checksystem.ps1脚本:

function IsAlive {
        <#
        .DESCRIPTION
        Checks to see whether a computer is pingable or not.

        .PARAMETER computername
        Specifies the computername.

        .EXAMPLE
        IsAlive -computername testwks01

        .NOTES
        This is just an example function.
        #>


            param (
                $computername
            )
            Test-Connection -count 1 -ComputerName $computername -TimeToLive 5 |
            Where-Object { $_.StatusCode -eq 0 } |
            Select-Object -ExpandProperty Address
        }

IsAlive -computername 192.168.1.1

3 个答案:

答案 0 :(得分:18)

它会工作,但您正在尝试运行获取脚本的帮助。您已将帮助添加到该功能。如果您点源脚本,然后键入get-help isalive,您将看到该函数的帮助。

. .\checksystem.ps1 ; get-help isalive -full

答案 1 :(得分:12)

它有效,你必须确保你有正确的标题。我总是把评论块放在函数的正上方。我不确定它是否应该在函数内部工作。

以下是我的一个功能示例,其中包含有效的doc帮助。

##############################################################################
#.SYNOPSIS
# Gets a COM object from the running object table (ROT) similar to GetObject
# in Visual Basic.
#
#.DESCRIPTION
# To maintain consistency with New-Object this cmdlet requires the -ComObject
# parameter to be provided and the TypeName parameter is not supported.
#
#.PARAMETER TypeName
# Not supported, but provided to maintain consistency with New-Object.
#
#.PARAMETER ComObject
# The ProgID of a registered COM object, such as MapPoint.Application.
#
#.PARAMETER Force
# If an existing object is not found, instead of writing an error, a new
# instance of the object will be created and returned.
#
#.EXAMPLE
# $olMailItem = 0
# Get-Object -ComObject Outlook.Application | %{$_.CreateItem($olMailItem).Display()}
##############################################################################
function Get-Object {

    [CmdletBinding(DefaultParameterSetName='Net')]
    param (

        [Parameter(ParameterSetName='Net', Position=1, Mandatory=$true)]
        [String]$TypeName,

        [Parameter(ParameterSetName='Com', Mandatory=$true)]
        [String]$ComObject,

        [Parameter()]
        [Switch]$Force

    )

    if ( $TypeName ) { throw '-TypeName is not supported. Use -ComObject instead.' }

    if ( $ComObject ) { 
        try {
            [System.Runtime.InteropServices.Marshal]::GetActiveObject($ComObject)
        }
        catch [System.Management.Automation.MethodInvocationException] {
            if ( $Force ) { New-Object -ComObject $ComObject }
            else { Write-Error "An active object of type $ComObject is not available." }
        }
    }

}

答案 2 :(得分:5)

注意 - 如果您忘记在.PARAMETER之后添加参数名称,则在运行get-help时不会显示任何自定义帮助文本

同样,如果拼错任何关键字,则不会显示自定义帮助。