如何获取帮助信息以显示我的Powershell脚本参数?

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

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

当我跑步

 PS > get-help .\setup.ps1 -detailed 

在参数部分,我的帮助信息不会出现:

 PARAMETERS -Targets <String[]> 

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

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

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

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