购买
下载掌阅APP,畅读海量书库
立即打开
畅读海量书库
扫码下载掌阅APP

PowerShell文档本身没有什么特别之处,可是文档和帮助内容集成到语言的方式着实令人佩服。本节将介绍如何在提示符窗口中显示命令的帮助页、如何通过“关于主题”获取关于语言的更多信息,以及如何使用 Update-Help 命令来更新文档。

类似于Linux系统中的 man 命令,PowerShell提供了 help 命令和 Get-Help cmdlet。如果想知道某个 Content cmdlet的作用,可以将命令名称传给 Get-Help 命令,以获取帮助信息。帮助信息往往分成几部分,包括 SYNOPSIS SYNTAX DESCRIPTION RELATED LINKS REMARKS 。这几部分详述了命令的作用、获取更多信息的途径,以及相关命令。 Add-Content 命令的文档如代码清单1-7所示。

代码清单1-7 Add-Content 命令的帮助页

PS> Get-Help Add-Content

NAME
    Add-Content

SYNOPSIS
    Appends content, such as words or data, to a file.

--snip--

将命令名称提供给 Get-Help 可以获得一些帮助信息,但帮助信息中最有用的部分是示例,即指定 Examples 参数。指定这个参数后将显示不同场景中的真实用例。可以试试用 Get-Help CommmandName -Examples 获取某个命令的帮助页,大部分内置命令提供了示例,以便你更好地理解命令的作用。例如,可以使用这个命令查看 Add-Content cmdlet的用法示例,如代码清单1-8所示。

代码清单1-8 获取 Add-Content 命令的用法示例

PS> Get-Help Add-Content -Examples

NAME
    Add-Content

SYNOPSIS
    Appends content, such as words or data, to a file.


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

    C:\PS>Add-Content -Path *.txt -Exclude help* -Value "END"

    Description

    -----------

    This command adds "END" to all text files in the current directory,
    except for those with file names that begin with "help."
--snip--

如果想要获取更多信息,可以将 Detailed Full 参数传给 Get-Help cmdlet,以全面阐述相应命令的用途。

除了各个命令的帮助内容,PowerShell的帮助系统还提供了“关于主题”,以便为更宽泛的主题和特定命令提供帮助信息。例如,本章将学习一些PowerShell核心命令,微软为这些命令创建了“关于主题”,从而对这些命令进行整体说明。查看核心命令的“关于主题”的方法是执行 Get-Help about_Core_Commands 命令,如代码清单1-9所示。

代码清单1-9 PowerShell核心命令的“关于主题”

PS> Get-Help about_Core_Commands
TOPIC
    about_Core_Commands

SHORT DESCRIPTION
    Lists the cmdlets that are designed for use with Windows PowerShell
    providers.


LONG DESCRIPTION
    Windows PowerShell includes a set of cmdlets that are specifically
    designed to manage the items in the data stores that are exposed by Windows
    PowerShell providers. You can use these cmdlets in the same ways to manage
    all the different types of data that the providers make available to you.
    For more information about providers, type "get-help about_providers".


    For example, you can use the Get-ChildItem cmdlet to list the files in a
    file system directory, the keys under a registry key, or the items that
    are exposed by a provider that you write or download.

    The following is a list of the Windows PowerShell cmdlets that are designed
    for use with providers:

--snip--

如果想获取可用“关于主题”的完整列表,可以在 Name 参数中使用通配符(wildcard)。PowerShell中的通配符是一个星号( * ),可代表零个或多个字符。 Get-Help 命令的 Name 参数可以使用通配符,如代码清单1-10所示。

代码清单1-10 Get-Help 命令的 Name 参数中使用通配符

PS> Get-Help -Name About*

About 后面加上通配符后,PowerShell将搜索所有以“About”开头的主题。如果有多个匹配结果,那么PowerShell会显示一个列表,并简述各个主题。如果想要获取某个匹配结果的完整信息,则需要将完整名称直接传给 Get-Help 命令,如前面的代码清单1-9所示。

虽然 Get-Help 命令有个 Name 参数,但是也可以直接将参数值传给 -Name ,如代码清单1-10所示。这叫作 位置参数 ,即通过参数在命令中的位置来判断参数的值。很多PowerShell命令支持位置参数,这是一种捷径,可以减少输入量。 ba9BFOuGFlh3DYvsRT+RI5U99V3luO68eHKSsVwnMNAnlgeczWqFIfREgLSTc1n3

点击中间区域
呼出菜单
上一章
目录
下一章
×