2011-03-08 15 views
40

我有一个powershell脚本(setup.ps1),我们用它作为开发环境设置脚本的入口点。它需要一个参数:如何获取有关我的Powershell脚本参数的帮助消息?

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

当我运行

PS > get-help .\setup.ps1 -detailed 
在参数部分

,不会出现我的帮助信息:

PARAMETERS 
    -Targets <String[]> 

什么我需要做的就是我的参数帮助信息显示?

回答

66

您可以在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 
    ) 
    ... 

欲了解更多信息,请参阅帮助主题 - man about_comment_based_help

+6

我明白了。所以'Parameter'属性上的'HelpMessage'属性实际上被* PowerShell帮助系统忽略*。这并不令人困惑。 :/ – 2011-03-08 20:39:34

+5

是的,它有点混乱。不过,参数表上的HelpMessage属性不会被忽略。它用于调用该命令时未指定强制参数。此时会提示您输入该参数的值。如果指定了“HelpMessage”,则该文本将显示为该提示的一部分。 – 2011-03-08 22:22:24

+4

但只有当你输入“!?”时当PowerShell提示输入该必需参数的值时。这是鲜为人知的。 – JasonMArcher 2011-03-09 20:06:19

11

很显然,如果你有一个帮助头文件中定义,你可以使用参数后面的备注(#)(在这个例子中:#The目标运行):

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

结果在:

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. 
+5

或者,您可以在参数前面加上注释,对于更长的描述和更长的参数名称可能会更好。 – 31eee384 2015-09-08 22:35:45

+0

你为什么不把目标参数放在你描述参数的部分, G。之前或之后'.PARAMETER Path' – Timo 2017-08-08 08:43:45

+0

在PS3中,您会得到一个不同的(更好的)“get-help -detailed”结果:显示'.PARAMETER'中的所有参数和描述。 – Timo 2017-08-08 08:48:10

相关问题