如何获取有关 Powershell 脚本参数的帮助消息?
我有一个 powershell 脚本 (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[]>
我需要做什么才能显示我的参数帮助消息?
I have a powershell script (setup.ps1
), that we use as the entry point for our development environment setup scripts. It takes a parameter:
param(
[Parameter(Position=0,HelpMessage="The targets to run.")]
[Alias("t")]
[string[]]
$Targets = "Help"
)
When I run
PS > get-help .\setup.ps1 -detailed
in the parameters section, my help message doesn't appear:
PARAMETERS
-Targets <String[]>
What do I need to do to get my parameter help messages to display?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
行间距至关重要。请参阅 https://learn.microsoft.com/en-us/powershell/scripting/developer/help/placing-comment-based-help-in-scripts?view=powershell-7.4
Line spacing is critical. See https://learn.microsoft.com/en-us/powershell/scripting/developer/help/placing-comment-based-help-in-scripts?view=powershell-7.4
您将某种风格的注释放在文件顶部,PowerShell 帮助系统可以对其进行解码。下面是一个示例:
有关详细信息,请参阅帮助主题 -
man about_comment_based_help
。You put a certain style of comment at the top of the file that can be decoded by the PowerShell help system. Here's an example:
For more info see the help topic -
man about_comment_based_help
.显然,如果您定义了帮助标头,则只需在参数后面使用注释 (#)(在本例中:#要运行的目标。):
结果为:
Apparently if you have a help header defined, you can just use a remark (#) behind the parameter (in this example: #The targets to run.):
Results in:
只需要文件顶部的
<# .SYNOPSIS #>
部分即可使其正常工作,并且您可以很好地内联注释您的参数:(使用
PS 5.1.14409.1018
检查)one just needs the
<# .SYNOPSIS #>
part on top of the file to make it work and you can comment your params nicely inline:(checked with
PS 5.1.14409.1018
)