代码之家  ›  专栏  ›  技术社区  ›  Kellen Stuart Alan

如何定义自定义命令的文档(使用Get命令或Get Help)

  •  0
  • Kellen Stuart Alan  · 技术社区  · 7 年前

    假设我有一个自定义命令:

    function Search-ForStringInFile($string)
    {
            ls -Recurse | Select-String -Pattern "$string" -List | Select Path
    }
    

    我想跑起来 Get-Help Search-ForStringInFile Get-Command Search-ForStringInFile

    目录

    有没有特殊的注释语法我可以用在 function 要添加此文档吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   FoxDeploy    7 年前

    <#
    .Synopsis
       Short description
    .DESCRIPTION
       Long description
    .EXAMPLE
       Example of how to use this cmdlet
    .EXAMPLE
       Another example of how to use this cmdlet
    #>
    function Verb-Noun
    {
    }
    

    一旦你填写了以上每一个的值( see the docs here 对于所有可用字段)并按F5,帮助将显示在函数中。

    PS>Get-Help Verb-Noun
    NAME
        Verb-Noun
    
    SYNOPSIS
        Short description
    
    
    SYNTAX
        Verb-Noun [-Param1] <Object> [-Param2 <Int32>] [-WhatIf] [-Confirm] [<CommonParameters>]
    
        Verb-Noun [-Param3 <String>] [-WhatIf] [-Confirm] [<CommonParameters>]
    
    
    DESCRIPTION
        Long description
    
    
    PARAMETERS
        -Param1 <Object>
            Param1 help description
    
            Required?                    true
            Position?                    1
            Default value                
            Accept pipeline input?       true (ByValue, ByPropertyName)
            Accept wildcard characters?  false
    
        -Param2 <Int32>
            Param2 help description
    
            Required?                    false
            Position?                    named
            Default value                0
            Accept pipeline input?       false
            Accept wildcard characters?  false
    
        -Param3 <String>
            Param3 help description
    
            Required?                    false
            Position?                    named
            Default value                
            Accept pipeline input?       false
            Accept wildcard characters?  false
    
        -WhatIf [<SwitchParameter>]
    
            Required?                    false
            Position?                    named
            Default value                
            Accept pipeline input?       false
            Accept wildcard characters?  false
    
        -Confirm [<SwitchParameter>]
    
            Required?                    false
            Position?                    named
            Default value                
            Accept pipeline input?       false
            Accept wildcard characters?  false
    
        <CommonParameters>
            This cmdlet supports the common parameters: Verbose, Debug,
            ErrorAction, ErrorVariable, WarningAction, WarningVariable,
            OutBuffer, PipelineVariable, and OutVariable. For more information, see 
            about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216). 
    
    INPUTS
        Inputs to this cmdlet (if any)
    
    
    OUTPUTS
        Output from this cmdlet (if any)
    
    
    NOTES
    
    
            General notes
    
        -------------------------- EXAMPLE 1 --------------------------
    
        PS C:\>Example of how to use this cmdlet
    
    
    
    
    
    
        -------------------------- EXAMPLE 2 --------------------------
    
        PS C:\>Another example of how to use this cmdlet
    
    
    
    
    
    
    
    RELATED LINKS
    

    因此,要向您自己的cmdlet添加帮助,只需在函数中粘贴相同的注释块。您可以将其放置在以下三个位置之一:

    • 函数声明前
    • 函数声明后(EWW)
    • 在你的功能结束时(不是要发表意见,但选择这个选项在道德上是错误的)。

    这些文件给出了每一个例子 the approved locations here

    推荐文章