2011-03-08 48 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