共用方式為


Select-Xml

尋找 XML 字串或文件中的文字。

Syntax

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>]

Description

Select-Xml Cmdlet 可讓您使用 XPath 查詢來搜尋 XML 字串和檔中的文字。 輸入 XPath 查詢,並使用 ContentPath 或 Xml 參數指定要搜尋的 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)。

第一個命令將 Types.ps1xml 檔案的路徑儲存在 $Path 變數中。

第二個命令將 AliasProperty 節點的 XML 路徑儲存在 $XPath 變數中。

第三個命令使用 Select-Xml Cmdlet 取得由 Types.ps1xml 檔案的 XPath 陳述式所識別的 AliasProperty 節點。 此命令會使用管線運算子將 AliasProperty 節點傳送至 Select-Object Cmdlet。 ExpandProperty 參數會展開 Node 物件,並傳回其 Name 和 ReferencedMemberName 屬性。

結果顯示 Types.ps1xml 檔案中每個別名屬性的 Name 和 ReferencedMemberName。 例如,有 Count 屬性是 Length 屬性的別名。

範例 2:輸入 XML 檔

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

此範例示範如何使用 XML 參數,將 XML 檔提供給 Select-Xml Cmdlet。

第一個命令會使用 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 的 here-string。 (如需有關 here-string 的詳細資訊,請參閱 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

Type:String[]
Position:Named
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-LiteralPath

指定要搜尋的 XML 檔案的路徑和檔案名稱。 與 Path 不同,LiteralPath 參數值將完全依照其輸入值來使用。 沒有字元會被視為萬用字元。 如果路徑包含逸出字元,請將它括在單引號中。 單引號會告訴PowerShell不要將任何字元解譯為逸出序列。

Type:String[]
Aliases:PSPath, LP
Position:Named
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Namespace

指定用於 XML 的命名空間雜湊表。 使用 @{<namespaceName> = <namespaceValue>} 格式。

當 XML 使用以 xmlns 開頭的預設命名空間時,請使用命名空間名稱的任意索引鍵。 您無法使用 xmlns。 在 XPath 語句中,在每個節點名稱前面加上命名空間名稱和冒號,例如 《Path)語句中,在每個節點名稱前面加上命名空間名稱和冒號,例如 「10000」namespaceName:Node。

Type:Hashtable
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Path

指定要搜尋的 XML 檔案的路徑和檔案名稱。 允許使用萬用字元。

Type:String[]
Position:1
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:True

-Xml

指定一或多個 XML 節點。

XML 文件將會視為 XML 節點的集合處理。 如果您透過管線將 XML 檔傳送至 Select-Xml,則會個別搜尋每個文件節點,因為它會通過管線。

Type:XmlNode[]
Aliases:Node
Position:1
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-XPath

指定 XPath 搜尋查詢。 查詢語言會區分大小寫。 此為必要參數。

Type:String
Position:0
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

輸入

System.String or System.Xml.XmlNode

您可以使用管線將路徑或 XML 節點傳送至此 Cmdlet。

輸出

SelectXmlInfo

備註

XPath 是一種設計來識別 XML 文件組件的標準語言。 如需 XPath 語言的詳細資訊,請參閱事件選取範圍的 XPath 參考和選取範圍篩選一節。