Select-Xml

查找 XML 字符串或文档中的文本。

语法

Select-Xml
      [-Xml] <XmlNode[]>
      [-XPath] <String>
      [-Namespace <Hashtable>]
      [<CommonParameters>]
Select-Xml
      [-Path] <String[]>
      [-XPath] <String>
      [-Namespace <Hashtable>]
      [<CommonParameters>]
Select-Xml
      -LiteralPath <String[]>
      [-XPath] <String>
      [-Namespace <Hashtable>]
      [<CommonParameters>]
Select-Xml
      -Content <String[]>
      [-XPath] <String>
      [-Namespace <Hashtable>]
      [<CommonParameters>]

说明

Select-Xml cmdlet 允许使用 XPath 查询在 XML 字符串和文档中搜索文本。 输入 XPath 查询,并使用 ContentPathXml 参数指定要搜索的 XML。

示例

示例 1:选择 AliasProperty 节点

PS C:\> $Path = "$Pshome\Types.ps1xml"
PS C:\> $XPath = "/Types/Type/Members/AliasProperty"
PS C:\> Select-Xml -Path $Path -XPath $Xpath | Select-Object -ExpandProperty Node

Name                 ReferencedMemberName
----                 --------------------
Count                Length
Name                 Key
Name                 ServiceName
RequiredServices     ServicesDependedOn
ProcessName          Name
Handles              Handlecount
VM                   VirtualSize
WS                   WorkingSetSize
Name                 ProcessName
Handles              Handlecount
VM                   VirtualMemorySize
WS                   WorkingSet
PM                   PagedMemorySize
NPM                  NonpagedSystemMemorySize
Name                 __Class
Namespace            ModuleName

此示例获取 Types.ps1xml 中的别名属性。 (有关此文件的信息,请参阅 about_Types.ps1xml.)

第一个命令将$Path变量中的 Types.ps1xml 文件的路径保存。

第二个命令将 XML 路径保存到 $XPath 变量中的 AliasProperty 节点。

第三个命令使用 Select-Xml cmdlet 从 Types.ps1xml 文件中获取 XPath 语句标识的 AliasProperty 节点。 该命令使用管道运算符将 AliasProperty 节点发送到 Select-Object cmdlet。 ExpandProperty 参数扩展 Node 对象,并返回其 Name 和 ReferencedMemberName 属性。

结果显示 Types.ps1xml 文件中每个别名属性的名称和 ReferencedMemberName。 例如,有一个 Count 属性,该属性是 Length 属性的别名。

示例 2:输入 XML 文档

PS C:\> [xml]$Types = Get-Content $Pshome\Types.ps1xml
PS C:\> Select-Xml -Xml $Types -XPath "//MethodName"

此示例演示如何使用 XML 参数向 Select-Xml cmdlet 提供 XML 文档。

第一个命令使用 Get-Content cmdlet 获取 Types.ps1xml 文件的内容,并将其保存在$Types变量中。 [xml] 将变量强制转换为 XML 对象。

第二个命令使用 Select-Xml cmdlet 获取 Types.ps1xml 文件中的 MethodName 节点。 该命令使用 Xml 参数来指定$Types变量中的 XML 内容,并使用 XPath 参数指定 MethodName 节点的路径。

示例 3:搜索 PowerShell 帮助文件

PS C:\> $Namespace = @{command = "https://schemas.microsoft.com/maml/dev/command/2004/10"; maml = "https://schemas.microsoft.com/maml/2004/10"; dev = "https://schemas.microsoft.com/maml/dev/2004/10"}

The second command saves the path to the help files in the $Path variable.If there are no help files in this path on your computer, use the Update-Help cmdlet to download the help files. For more information about Updatable Help, see about_Updatable_Help (https://go.microsoft.com/fwlink/?LinkId=235801).
PS C:\> $Path = "$Pshome\en-us\*dll-Help.xml"

The third command uses the **Select-Xml** cmdlet to search the XML for cmdlet names by finding Command:Name element anywhere in the files. It saves the results in the $Xml variable.**Select-Xml** returns a **SelectXmlInfo** object that has a Node property, which is a **System.Xml.XmlElement** object. The Node property has an InnerXML property, which contains the actual XML that is retrieved.
PS C:\> $Xml = Select-Xml -Path $Path -Namespace $Namespace -XPath "//command:name"

The fourth command sends the XML in the $Xml variable to the Format-Table cmdlet. The **Format-Table** command uses a calculated property to get the Node.InnerXML property of each object in the $Xml variable, trim the white space before and after the text, and display it in the table, along with the path to the source file.
PS C:\> $Xml | Format-Table @{Label="Name"; Expression= {($_.node.innerxml).trim()}}, Path -AutoSize

Name                    Path
----                    ----
Export-Counter          C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Diagnostics.dll-Help.xml
Get-Counter             C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Diagnostics.dll-Help.xml
Get-WinEvent            C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Diagnostics.dll-Help.xml
Import-Counter          C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Diagnostics.dll-Help.xml
Add-Computer            C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Management.dll-Help.xml
Add-Content             C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Management.dll-Help.xml
Checkpoint-Computer     C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Management.dll-Help.xml
...

此示例演示如何使用 Select-Xml cmdlet 搜索基于 PowerShell XML 的 cmdlet 帮助文件。 在此示例中,我们将搜索用作每个帮助文件的标题的 cmdlet 名称以及帮助文件的路径。

第一个命令创建一个哈希表,该表表示用于帮助文件的 XML 命名空间,并将其保存在$Namespace变量中。

示例 4:输入 XML 的不同方法

PS C:\> $Xml = @"
<?xml version="1.0" encoding="utf-8"?>
<Book>
  <projects>
    <project name="Book1" date="2009-01-20">
      <editions>
        <edition language="English">En.Book1.com</edition>
        <edition language="German">Ge.Book1.Com</edition>
        <edition language="French">Fr.Book1.com</edition>
        <edition language="Polish">Pl.Book1.com</edition>
      </editions>
    </project>
  </projects>
</Book>
"@

The second command uses the *Content* parameter of **Select-Xml** to specify the XML in the $Xml variable.
PS C:\> Select-Xml -Content $Xml -XPath "//edition" | foreach {$_.node.InnerXML}

En.Book1.com
Ge.Book1.Com
Fr.Book1.com
Pl.Book1.com

The third command is equivalent to the second. It uses a pipeline operator (|) to send the XML in the $Xml variable to the **Select-Xml** cmdlet.
PS C:\> $Xml | Select-Xml -XPath "//edition" | foreach {$_.node.InnerXML}

En.Book1.com
Ge.Book1.Com
Fr.Book1.com
Pl.Book1.com

此示例演示了将 XML 发送到 Select-Xml cmdlet 的两种不同的方法。

第一个命令保存一个此处字符串,该字符串包含$Xml变量中的 XML。 (有关此处字符串的详细信息,请参阅about_Quoting_Rules。

示例 5:使用默认 xmlns 命名空间

PS C:\> $SnippetNamespace = @{snip = "https://schemas.microsoft.com/PowerShell/Snippets"}

The second command uses the **Select-Xml** cmdlet to get the content of the Title element of each snippet. It uses the *Path* parameter to specify the Snippets directory and the *Namespace* parameter to specify the namespace in the $SnippetNamespace variable. The value of the *XPath* parameter is the "snip" namespace key, a colon (:), and the name of the Title element.The command uses a pipeline operator (|) to send each **Node** property that **Select-Xml** returns to the ForEach-Object cmdlet, which gets the title in the value of the **InnerXml** property of the node.
PS C:\> Select-Xml -Path $Home\Documents\WindowsPowerShell\Snippets -Namespace $SnippetNamespace -XPath "//snip:Title" | foreach {$_.Node.Innerxml}

此示例演示如何将 Select-Xml cmdlet 与使用默认 xmlns 命名空间的 XML 文档配合使用。 该示例获取 Windows PowerShell ISE 用户创建的代码段文件的标题。 有关代码段的信息,请参阅 New-IseSnippet。

第一个命令为 XML 文件使用的默认命名空间创建哈希表,并将其分配给$SnippetNamespace变量。 哈希表值是代码片段 XML 中的 XMLNS 架构 URI。 哈希表键名称(snip)是任意的。 可以使用不保留的任何名称,但不能使用 xmlns。

参数

-Content

指定要搜索的 XML 的字符串。 还可以通过管道将字符串传递给 Select-Xml

类型:String[]
Position:Named
默认值:None
必需:True
接受管道输入:True
接受通配符:False

-LiteralPath

指定要搜索的 XML 文件的路径和文件名。 与 Path不同,LiteralPath 参数的值与键入时完全相同。 不会将任何字符解释为通配符。 如果路径包含转义字符,请将它括在单引号中。 单引号告知 PowerShell 不要将任何字符解释为转义序列。

类型:String[]
别名:PSPath
Position:Named
默认值:None
必需:True
接受管道输入:True
接受通配符:False

-Namespace

指定 XML 中使用的命名空间的哈希表。 使用格式 @{<namespaceName> = <namespaceValue>}。

当 XML 使用以 xmlns 开头的默认命名空间时,请使用命名空间名称的任意键。 不能使用 xmlns。 在 XPath 语句中,使用命名空间名称和冒号(如 //namespaceName:Node)为每个节点名称添加前缀。

类型:Hashtable
Position:Named
默认值:None
必需:False
接受管道输入:False
接受通配符:False

-Path

指定要搜索的 XML 文件的路径和文件名。 允许使用通配符。

类型:String[]
Position:1
默认值:None
必需:True
接受管道输入:True
接受通配符:True

-Xml

指定一个或多个 XML 节点。

XML 文档将作为 XML 节点的集合进行处理。 如果将 XML 文档通过管道传递给 Select-Xml,则每个文档节点将在管道中单独搜索。

类型:XmlNode[]
别名:Node
Position:1
默认值:None
必需:True
接受管道输入:True
接受通配符:False

-XPath

指定 XPath 搜索查询。 查询语言区分大小写。 此参数是必需的。

类型:String
Position:0
默认值:None
必需:True
接受管道输入:False
接受通配符:False

输入

System.String or System.Xml.XmlNode

可以通过管道将路径或 XML 节点传递给此 cmdlet。

输出

SelectXmlInfo

备注

  • XPath 是一种标准语言,旨在标识 XML 文档的各个部分。 有关 XPath 语言的详细信息,请参阅 MSDN 库中 事件选择XPath 参考 和选择筛选器部分。