如何为Powershell脚本参数显示帮助消息?

Aar*_*sen 60 powershell powershell-2.0

我有一个powershell脚本(setup.ps1),我们将其用作开发环境设置脚本的入口点.它需要一个参数:

param(
    [Parameter(Position=0,HelpMessage="The targets to run.")]
    [Alias("t")]
    [string[]]
    $Targets = "Help"
)
Run Code Online (Sandbox Code Playgroud)

我跑的时候

PS > get-help .\setup.ps1 -detailed
Run Code Online (Sandbox Code Playgroud)

在参数部分,我的帮助消息没有出现:

PARAMETERS
    -Targets <String[]>
Run Code Online (Sandbox Code Playgroud)

我需要做什么才能显示参数帮助消息?

Kei*_*ill 93

您在文件的顶部放置了某种注释样式,可以通过PowerShell帮助系统进行解码.这是一个例子:

<#
.SYNOPSIS
    .
.DESCRIPTION
    .
.PARAMETER Path
    The path to the .
.PARAMETER LiteralPath
    Specifies a path to one or more locations. Unlike Path, the value of 
    LiteralPath is used exactly as it is typed. No characters are interpreted 
    as wildcards. If the path includes escape characters, enclose it in single
    quotation marks. Single quotation marks tell Windows PowerShell not to 
    interpret any characters as escape sequences.
.EXAMPLE
    C:\PS> 
    <Description of example>
.NOTES
    Author: Keith Hill
    Date:   June 28, 2010    
#>
function AdvFuncToProcessPaths
{
    [CmdletBinding(DefaultParameterSetName="Path")]
    param(
        [Parameter(Mandatory=$true, Position=0, ParameterSetName="Path", 
                   ValueFromPipeline=$true, 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Path to ...")]
        [ValidateNotNullOrEmpty()]
        [string[]]
        $Path,

        [Alias("PSPath")]
        [Parameter(Mandatory=$true, Position=0, ParameterSetName="LiteralPath", 
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage="Path to ...")]
        [ValidateNotNullOrEmpty()]
        [string[]]
        $LiteralPath
    )
    ...
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅帮助主题 - man about_comment_based_help.

  • 但只有你输入"!?" 当PowerShell提示输入该必需参数的值时.这鲜为人知. (8认同)
  • 我知道了.因此,PowerShell帮助系统实际上*忽略了`Parameter`属性的`HelpMessage`属性.这并不令人困惑.:/ (7认同)
  • 是的,它有点令人困惑.但是,不会忽略parmeter上的HelpMessage属性.在不指定必需参数的情况下调用命令时使用它.此时,系统会提示您输入该参数的值.如果指定"HelpMessage",则该文本将显示为该提示的一部分. (7认同)

iRo*_*Ron 20

显然,如果你定义了一个帮助头,你可以在参数后面使用一个注释(#)(在这个例子中:#要运行的目标.):

<#
.SYNOPSIS
    .
.DESCRIPTION
    .
.PARAMETER Path
    The path to the .
.PARAMETER LiteralPath
    Specifies a path to one or more locations. Unlike Path, the value of 
    LiteralPath is used exactly as it is typed. No characters are interpreted 
    as wildcards. If the path includes escape characters, enclose it in single
    quotation marks. Single quotation marks tell Windows PowerShell not to 
    interpret any characters as escape sequences.
#>

Param(
    [String]$Targets = "Help"   #The targets to run.
)
Run Code Online (Sandbox Code Playgroud)

结果是:

PS C:\> Get-help .\Setup.ps1 -Detailed

NAME
    C:\Setup.ps1

SYNOPSIS
    .


SYNTAX
    C:\Setup.ps1 [[-Targets] <String>] [<CommonParameters>]


DESCRIPTION
    .


PARAMETERS
    -Targets <String>
        The targets to run.
Run Code Online (Sandbox Code Playgroud)

  • 或者,您可以将注释放在参数之前的行上,这可能更适合更长的描述和更长的参数名称. (6认同)

And*_*ich 6

一个只需要<# .SYNOPSIS #>文件顶部部分即可使其工作,您可以很好地内联注释您的参数

<# .SYNOPSIS #>
param(
   [String]$foo   ## my 1st cool param
  ,[Switch]$bar  ## my 2nd crazy switch
)
...
Run Code Online (Sandbox Code Playgroud)

(检查PS 5.1.14409.1018