【发布时间】:2018-01-31 15:35:04
【问题描述】:
当用户通过以下方式请求帮助时,如何让我的 PowerShell 脚本显示帮助:
Get-Help -Name myscript.ps1
或
myscript.ps1 -?
例如,描述我的脚本参数等
更新问题
我尝试了建议的答案。但我没有看到预期的输出。
我只是将以下代码添加到 myScript.ps1。
<#
.SYNOPSIS
A short description of your script.
.DESCRIPTION
A longer description of your script.
.PARAMETER <-silent>
First parameter is -silent. It will do Collection Bootstrap silently.
.PARAMETER <action>
Second parameter is action. Action could be either bootstrap or join
#>
当我累了
获取帮助 .\myScript.ps1
,如下所示
NAME
C:\es\dev\myScript.ps1
SYNOPSIS
A short description of your script.
SYNTAX
C:\es\dev\myScript.ps1 [<CommonParameters>]
DESCRIPTION
A longer description of your script.
RELATED LINKS
REMARKS
To see the examples, type: "get-help C:\es\dev\myScript.ps1 -examples".
For more information, type: "get-help C:\es\dev\myScript.ps1 -detailed".
For technical information, type: "get-help C:\es\dev\myScript.ps1 -full".
我期待看到参数的描述。我尝试了 .PARAMETER 和 .PARAMETER -silent。结果相同。怎么了?
处理完更多相关问题更新2
添加参数部分后可以看到帮助文件如下:-
param (
[Parameter(ParameterSetName='taskJsonFile', Position=1, Mandatory=$true)]
[String]$taskJsonFile="tasks.json"
)
但查看我必须使用的完整帮助文件非常重要
Get-help .\myscript.ps1 -full
否则,它不会显示完整的帮助信息。
但是我的下一个问题来了。当我尝试-full时。它显示了一些不相关的信息。我想省略此信息以显示给用户。现在显示如下:-
名称 C:\es\dev\myscript.ps1
概要 它以静默方式或基于参数化的方式执行 Collection Bootstrapping。
语法 C:\es\dev\myscript.ps1 [[-action] ] [[-file] ] [[-sasHostname] ] []
描述 对于静默操作,用户必须在 Eurostep.SAS.Collection Bootstrap.Config.psd1 中使用所需的值填充配置文件。这个 配置文件必须存在于 Eurostep.SAS.CollectionBootstrap.ps1 PowerShell 脚本的同一目录。
参数 -动作
Required? false Position? 2 Default value bootstrap Accept pipeline input? false Accept wildcard characters? false -file <String> Required? false Position? 3 Default value bootstrap_collection.json Accept pipeline input? false Accept wildcard characters? false -sasHostname <String> Required? false Position? 4 Default value http://localhost:5000 Accept pipeline input? false Accept wildcard characters? false <CommonParameters> This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer, PipelineVariable, and OutVariable. For more information, see about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).输入
输出
-------------------------- EXAMPLE 1 -------------------------- C:\PS>.\myscript.ps1 For silent Collection Bootstrapping no parameter. It reads values from configuration file myscript.Config.psd1 -------------------------- EXAMPLE 2 -------------------------- C:\PS>.\myscript.ps1 bootstrap bootstrap_collection.json 'http://localhost:5000' All required parameter provided. Note that hostname must be inside ' '. Because it is a link.相关链接
我不想显示RELATED LINKS INPUTS OUTPUTS 等
无论如何要从帮助信息中删除它们。连-Full都用了吗?
【问题讨论】:
-
最简单的方法是添加基于评论的帮助:docs.microsoft.com/en-us/powershell/module/…
-
您只需要输入
.PARAMETER silent和.PARAMETER action,这是不必要的<、-、>字符会阻止它工作 -
我删除了
<、-、>字符。不过,它是一样的。无参数说明。 -
你的脚本有
param()块吗?
标签: powershell