2011-01-12 4 views
17

Intento usar el cmdlet Get-Help para mostrar la ayuda basada en comentarios en el mismo formato en el que muestra los temas de ayuda de cmdlet que se generan de archivos XML. La capacidad de hacerlo está documentada en about_Comment_based_Help en TechNet, pero cuando ejecuto el cmdlet get-help contra mi script solo obtengo el nombre del script. ¡Cualquier ayuda sería apreciada!Use el cmdlet Get-Help para mostrar la ayuda basada en comentarios en el mismo formato

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

guión 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 

Respuesta

16

Funcionará, pero está intentando recibir ayuda con el guión, pero ha agregado la ayuda a la función. Si asigna un punto a la fuente de su script y luego escribe get-help isalive, verá su ayuda para la función.

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

Funciona, solo tienes que asegurarse de que tiene los títulos correctos. Siempre puse el bloque de comentarios justo encima de la función también. No estoy seguro de si se supone que debe funcionar dentro de la función o no.

A continuación se muestra un ejemplo de una de mis funciones que tiene una ayuda doc de trabajo.

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

En PowerShell versión 3, los comentarios funcionan tanto dentro como fuera de una función: http://technet.microsoft.com/en-us/library/dd819489.aspx –

+1

Aún mejor, en PowerShell 3, ni siquiera necesita marcar el parámetro ayuda. Simplemente haga un comentario regular sobre el parámetro y PowerShell lo averigua. – Josh

+0

Según estos http://technet.microsoft.com/en-us/library/dd819489.aspx comentarios fuera de la función también se aplica a powershell 2.0. – Raj

4

Nota - Si se olvida de añadir el nombre de un parámetro después de .PARAMETER, ninguno de su texto de ayuda personalizado mostrará cuando se ejecuta get-help

mismo modo, si se escribe incorrectamente alguna de las palabras clave de la la ayuda personalizada no se mostrará.

Cuestiones relacionadas