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

Powershell 如何定义自定义命令的文档(使用Get命令或Get帮助),powershell,documentation-generation,powershell-5.0,powershell-v5.1,Powershell,Documentation Generation,Powershell 5.0,Powershell V5.1,假设我有一个自定义命令: function Search-ForStringInFile($string) { ls -Recurse | Select-String -Pattern "$string" -List | Select Path } 我希望能够运行Get-Help-Search-forstringfile或Get-Command-Search-forstringfile,以获取命令功能的描述 描述:在当前文件中/下的所有文件中搜索关键字 目录 是否可以在函数中使

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

function Search-ForStringInFile($string)
{
        ls -Recurse | Select-String -Pattern "$string" -List | Select Path
}
我希望能够运行
Get-Help-Search-forstringfile
Get-Command-Search-forstringfile
,以获取命令功能的描述

描述:在当前文件中/下的所有文件中搜索关键字 目录


是否可以在
函数中使用特殊的注释语法来添加此文档?

这称为基于注释的帮助。事实上,PowerShell ISE有一个非常棒的片段,可以精确地完成您想要做的事情。只需点击Control+J并选择“Cmdlet-Advanced Function”即可加载我将在下面提供的代码段:

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

功能动词名词
{
}
一旦您为上述每个字段(所有可用字段)填写值并点击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
PS>获取帮助动词名词
名称
动名词
提要
简短描述
语法
动词名词[-Param1][-Param2][-WhatIf][-Confirm][]
动词名词[-Param3][-WhatIf][-Confirm][]
描述
长描述
参数
-参数1
参数1帮助说明
必修的?真的
位置?1.
默认值
接受管道输入?true(按值、按属性名称)
是否接受通配符?假的
-参数2
参数2帮助说明
必修的?假的
位置?命名
默认值0
接受管道输入?假的
是否接受通配符?假的
-Param3
参数3帮助说明
必修的?假的
位置?命名
默认值
接受管道输入?假的
是否接受通配符?假的
-WhatIf[]
必修的?假的
位置?命名
默认值
接受管道输入?假的
是否接受通配符?假的
-确认[]
必修的?假的
位置?命名
默认值
接受管道输入?假的
是否接受通配符?假的
此cmdlet支持常用参数:详细、调试、,
ErrorAction,ErrorVariable,WarningAction,WarningVariable,
exputffer、PipelineVariable和OutVariable。有关详细信息,请参阅
关于公共参数(https:/go.microsoft.com/fwlink/?LinkID=113216)。
投入
此cmdlet的输入(如果有)
输出
此cmdlet的输出(如果有)
笔记
一般说明
--------------------------例1--------------------------
PS C:\>如何使用此cmdlet的示例
--------------------------例2--------------------------
PS C:\>如何使用此cmdlet的另一个示例
相关链接
因此,要向您自己的cmdlet添加帮助,只需在函数中粘贴相同的注释块。您可以将其放置在以下三个位置之一:

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

文档还提供了每种方法的示例。

这称为基于注释的帮助。事实上,PowerShell ISE有一个非常棒的片段,可以精确地完成您想要做的事情。只需点击Control+J并选择“Cmdlet-Advanced Function”即可加载我将在下面提供的代码段:

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

功能动词名词
{
}
一旦您为上述每个字段(所有可用字段)填写值并点击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
PS>获取帮助动词名词
名称
动名词
提要
简短描述
语法
动词名词[-Param1][-Param2][-WhatIf][-Confirm][]
动词名词[-Param3][-WhatIf][-Confirm][]
描述
长描述
参数
-参数1
参数1帮助说明
必修的?真的
位置?1.
默认值
接受管道输入?true(按值、按属性名称)
是否接受通配符?假的
-参数2
参数2帮助说明
必修的?假的
位置?命名
默认值0
接受管道输入?假的
是否接受通配符?假的
-Param3
参数3帮助说明
必修的?假的
位置?命名
默认值
接受管道输入?假的
是否接受通配符?假的
-WhatIf[]
必修的?假的
位置?命名
默认值
接受管道输入?假的
是否接受通配符?假的
-确认[]
必修的?假的
位置?命名
默认值
接受管道输入?假的
是否接受通配符?假的
此cmdlet支持常用参数:详细、调试、,
ErrorAction,ErrorVariable,WarningAction,WarningVariable,
exputffer、PipelineVariable和OutVariable。有关详细信息,请参阅
关于公共参数(https:/go.microsoft.com/fwlink/?LinkID=113216)。
投入
此cmdlet的输入(如果有)
输出
此cmdlet的输出(如果有)
笔记
一般说明
--------------------------例1--------------------------
PS C:\>如何使用此cmdlet的示例
--------------------------例2--------------------------
PS C:\>如何使用thi的另一个示例