当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 基于注释的帮助示例

作者:精品下载站 日期:2024-12-14 02:32:22 浏览:13 分类:玩电脑

基于注释的帮助示例


本主题包含演示如何对脚本和函数使用基于注释的帮助的示例。

示例 1:基于注释的函数帮助

以下示例函数包括基于注释的帮助。

function Add-Extension
{
    param ([string]$Name,[string]$Extension = "txt")
    $name = $name + "." + $extension
    $name

    <#
        .SYNOPSIS
        Adds a file name extension to a supplied name.

        .DESCRIPTION
        Adds a file name extension to a supplied name.
        Takes any strings for the file name or extension.

        .PARAMETER Name
        Specifies the file name.

        .PARAMETER Extension
        Specifies the extension. "Txt" is the default.

        .INPUTS
        None. You can't pipe objects to Add-Extension.

        .OUTPUTS
        System.String. Add-Extension returns a string with the extension or file name.

        .EXAMPLE
        PS> Add-Extension -name "File"
        File.txt

        .EXAMPLE
        PS> Add-Extension -name "File" -extension "doc"
        File.doc

        .EXAMPLE
        PS> Add-Extension "File" "doc"
        File.doc

        .LINK
        Online version: http://www.fabrikam.com/add-extension.html

        .LINK
        Set-Item
    #>
}

以下输出显示了 Get-Help 命令的结果,该命令显示了 Add-Extension 函数的帮助。

PS> Get-Help Add-Extension -full
NAME
    Add-Extension

SYNOPSIS
    Adds a file name extension to a supplied name.

SYNTAX
    Add-Extension [[-Name] <String>] [[-Extension] <String>] [<CommonParameters>]

DESCRIPTION
    Adds a file name extension to a supplied name. Takes any strings for the file name or extension.

PARAMETERS
    -Name
        Specifies the file name.

        Required?                    false
        Position?                    0
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?

    -Extension
        Specifies the extension. "Txt" is the default.

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?

    <CommonParameters>
        This cmdlet supports the common parameters: -Verbose, -Debug,
        -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
        -OutBuffer and -OutVariable. For more information, type
        "get-help about_commonparameters".

INPUTS
    None. You can't pipe objects to Add-Extension.

OUTPUTS
    System.String. Add-Extension returns a string with the extension or file name.

    -------------------------- EXAMPLE 1 --------------------------

    PS> Add-Extension -name "File"
    File.txt

    -------------------------- EXAMPLE 2 --------------------------

    PS> Add-Extension -name "File" -extension "doc"
    File.doc

    -------------------------- EXAMPLE 3 --------------------------

    PS> Add-Extension "File" "doc"
    File.doc

RELATED LINKS
    Online version: http://www.fabrikam.com/add-extension.html
    Set-Item

示例 2:基于注释的脚本帮助

以下示例函数包括基于注释的帮助。

请注意结束 #>Param 语句之间的空行。在没有 Param 语句的脚本中,帮助主题中的最后一个注释和第一个函数声明之间必须至少有两个空行。如果没有这些空行,Get-Help 会将帮助主题与函数(而不是脚本)关联起来。

<#
  .SYNOPSIS
  Performs monthly data updates.

  .DESCRIPTION
  The Update-Month.ps1 script updates the registry with new data generated
  during the past month and generates a report.

  .PARAMETER InputPath
  Specifies the path to the CSV-based input file.

  .PARAMETER OutputPath
  Specifies the name and path for the CSV-based output file. By default,
  MonthlyUpdates.ps1 generates a name from the date and time it runs, and
  saves the output in the local directory.

  .INPUTS
  None. You can't pipe objects to Update-Month.ps1.

  .OUTPUTS
  None. Update-Month.ps1 doesn't generate any output.

  .EXAMPLE
  PS> .\Update-Month.ps1

  .EXAMPLE
  PS> .\Update-Month.ps1 -inputpath C:\Data\January.csv

  .EXAMPLE
  PS> .\Update-Month.ps1 -inputpath C:\Data\January.csv -outputPath C:\Reports09\January.csv
#>

param ([string]$InputPath, [string]$OutPutPath)

function Get-Data { }

以下命令获取脚本帮助。由于脚本不在 Path 环境变量中列出的目录中,因此获取脚本帮助的 Get-Help 命令必须指定脚本路径。

PS> Get-Help c:\ps-test\update-month.ps1 -full
NAME
    C:\ps-test\Update-Month.ps1

SYNOPSIS
    Performs monthly data updates.

SYNTAX
    C:\ps-test\Update-Month.ps1 [-InputPath] <String> [[-OutputPath]
    <String>] [<CommonParameters>]

DESCRIPTION
    The Update-Month.ps1 script updates the registry with new data
    generated during the past month and generates a report.

PARAMETERS
    -InputPath
        Specifies the path to the CSV-based input file.

        Required?                    true
        Position?                    0
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?

    -OutputPath
        Specifies the name and path for the CSV-based output file. By
        default, MonthlyUpdates.ps1 generates a name from the date
        and time it runs, and saves the output in the local directory.

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?

    <CommonParameters>
        This cmdlet supports the common parameters: -Verbose, -Debug,
        -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
        -OutBuffer and -OutVariable. For more information, type,
        "get-help about_commonparameters".

INPUTS
        None. You can't pipe objects to Update-Month.ps1.

OUTPUTS
        None. Update-Month.ps1 doesn't generate any output.

-------------------------- EXAMPLE 1 --------------------------

PS> .\Update-Month.ps1

-------------------------- EXAMPLE 2 --------------------------

PS> .\Update-Month.ps1 -inputpath C:\Data\January.csv

-------------------------- EXAMPLE 3 --------------------------

PS> .\Update-Month.ps1 -inputpath C:\Data\January.csv -outputPath
C:\Reports09\January.csv

RELATED LINKS

示例 3:Param 语句中的参数描述

此示例演示如何在函数或脚本的 Param 语句中插入参数描述。当参数描述简短时,此格式最有用。

function Add-Extension
{
    param
    (
        [string]
        # Specifies the file name.
        $name,

        [string]
        # Specifies the file name extension. "Txt" is the default.
        $extension = "txt"
    )
    $name = $name + "." + $extension
    $name

    <#
        .SYNOPSIS
        Adds a file name extension to a supplied name.
...
    #>
}

结果与示例 1 的结果相同。Get-Help 将参数描述解释为带有 .Parameter 关键字。

示例 4:重定向到 XML 文件

您可以为函数和脚本编写基于 XML 的帮助主题。虽然基于注释的帮助更容易实现,但如果您希望更精确地控制帮助内容或将帮助主题翻译成多种语言,则需要基于 XML 的帮助。以下示例显示 Update 的前几行-Month.ps1 脚本。该脚本使用 .ExternalHelp 关键字指定脚本的基于 XML 的帮助主题的路径。

# .ExternalHelp C:\MyScripts\Update-Month-Help.xml

    param ([string]$InputPath, [string]$OutPutPath)

    function Get-Data { }

以下示例显示了在函数中使用 .ExternalHelp 关键字。

function Add-Extension
{
    param ([string] $name, [string]$extension = "txt")
    $name = $name + "." + $extension
    $name

    # .ExternalHelp C:\ps-test\Add-Extension.xml
}

示例 5:重定向到不同的帮助主题

以下代码摘自 PowerShell 中内置 Help 函数的开头部分,该函数一次显示一屏帮助文本。由于 Get-Help cmdlet 的帮助主题描述了帮助功能,因此帮助函数使用 .ForwardHelpTargetName.ForwardHelpCategory 关键字将用户重定向到 Get-Help cmdlet帮助主题。

function help
{
    <#
      .FORWARDHELPTARGETNAME Get-Help
      .FORWARDHELPCATEGORY Cmdlet
    #>
    [CmdletBinding(DefaultParameterSetName='AllUsersView')]
    param(
            [Parameter(Position=0, ValueFromPipelineByPropertyName=$true)]
            [System.String]
            ${Name},
    ...
}

以下命令使用此功能。当用户为 Help 功能键入 Get-Help 命令时,Get-Help 显示 Get-Help 的帮助主题 cmdlet。

PS> get-help help
NAME
    Get-Help

SYNOPSIS
    Displays information about Windows PowerShell cmdlets and concepts.
...

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯