【问题标题】:How to enable a PowerShell script to return help text when using Get-Help or -?使用 Get-Help 或 - 时如何启用 PowerShell 脚本返回帮助文本?
【发布时间】: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都用了吗?

【问题讨论】:

标签: powershell


【解决方案1】:

最简单的方法是在脚本中添加基于注释的帮助。这是一个特殊的注释块,它使用特定的关键字格式化,然后是这些关键字的相关文本。例如:

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

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

【讨论】:

  • 要明确,只需将其复制并粘贴到脚本顶部即可。您也可以对模块中的每个函数执行此操作。只需在 Function 声明之前或 Function 内粘贴注释块。
  • 要查看完整的帮助信息,必须提供 -full 参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
  • 2018-08-08
  • 2011-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多