【问题标题】:Use the Get-Help cmdlet to display comment-based help in the same format使用 Get-Help cmdlet 以相同格式显示基于注释的帮助
【发布时间】:2018-07-09 00:22:42
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: powershell powershell-2.0


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      它有效,你只需要确保你有正确的标题。我也总是把注释块放在函数的正上方。我不确定它是否应该在函数内部工作。

      下面是我的一个函数的示例,它有一个有效的文档帮助。

      ##############################################################################
      #.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." }
              }
          }
      
      }
      

      【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-08
        相关资源
        最近更新 更多