如何在使用 Get-Help 或 - 时启用 PowerShell 脚本以返回帮助文本?

mas*_*boo 5 powershell

当用户通过以下方式请求帮助时,如何让我的 PowerShell 脚本显示帮助:

Get-Help -Name myscript.ps1
Run Code Online (Sandbox Code Playgroud)

或者

myscript.ps1 -?
Run Code Online (Sandbox Code Playgroud)

例如,描述我的脚本参数等。

更新的问题

我尝试了建议的答案。但我没有看到预期的输出。

我只是将以下代码添加到 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

#>
Run Code Online (Sandbox Code Playgroud)

当我累了

获取帮助 .\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".
Run Code Online (Sandbox Code Playgroud)

我期待看到参数的描述。我尝试了 .PARAMETER <-silent> 和 .PARAMETER -silent。结果一样。怎么了?

处理后相关问题更新2

添加参数部分后,我可以看到帮助文件如下:-

param (

    [Parameter(ParameterSetName='taskJsonFile', Position=1, Mandatory=$true)]
    [String]$taskJsonFile="tasks.json"
)
Run Code Online (Sandbox Code Playgroud)

但是查看完整帮助文件非常重要,我必须使用
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).
Run Code Online (Sandbox Code Playgroud)

输入

输出

-------------------------- 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.
Run Code Online (Sandbox Code Playgroud)

相关链接

我不想显示RELATED LINKS INPUTS OUTPUTS等。

有没有办法将它们从帮助信息中删除。甚至 -Full 使用?

Mar*_*agg 5

最简单的方法是向脚本添加基于注释的帮助。这是一个特殊的注释块,使用特定关键字以及这些关键字的关联文本进行格式化。例如:

<#
.SYNOPSIS
    A short description of your script.

.DESCRIPTION
    A longer description of your script.

.PARAMETER SomeParameter
    A description of the SomeParameter parameter.  

.PARAMETER OtherParameter
    A description of the OtherParameter parameter. Have as many of these lines as you have parameters.

.EXAMPLE
    YourScript.ps1 -SomeParameter 'thing' -OtherParameter 1

    Does something. Have as many examples as you think useful.
#>
Run Code Online (Sandbox Code Playgroud)

这些是我默认使用的关键字,但请查看此处描述的完整列表,了解您可能想要包含的其他关键字:https: //learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/关于/about_comment_based_help?view=powershell-5.1