Select-Xml
尋找 XML 字串或檔案中的文字。
語法
Xml (預設值)
Select-Xml
[-Xml] <XmlNode[]>
[-XPath] <String>
[-Namespace <Hashtable>]
[<CommonParameters>]
Path
Select-Xml
[-Path] <String[]>
[-XPath] <String>
[-Namespace <Hashtable>]
[<CommonParameters>]
LiteralPath
Select-Xml
[-XPath] <String>
-LiteralPath <String[]>
[-Namespace <Hashtable>]
[<CommonParameters>]
Content
Select-Xml
[-XPath] <String>
-Content <String[]>
[-Namespace <Hashtable>]
[<CommonParameters>]
Description
Select-Xml Cmdlet 可讓您使用 XPath 查詢來搜尋 XML 字串和檔案中的文字。 輸入 XPath 查詢,並使用 Content、Path或 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 變數中。
第二個命令會將 XML 路徑儲存至 $XPath 變數中的 AliasProperty 節點。
第三個命令會使用 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-strings 的詳細資訊,請參閱 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[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
Content
| Position: | Named |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-LiteralPath
指定要搜尋之 XML 檔案的路徑和檔名。 不同於 Path,LiteralPath 參數的值完全按照輸入的方式使用。 不會將任何字元解譯為通配符。 如果路徑包含逸出字元,請以單引弧括住它。 單引號會告知PowerShell不要將任何字元解譯為逸出序列。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | PSPath |
參數集
LiteralPath
| Position: | Named |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Namespace
指定 XML 中使用的命名空間哈希表。 使用 @{<namespaceName> = <namespaceValue>}格式。
當 XML 使用以 xmlns 開頭的預設命名空間時,請使用命名空間名稱的任意索引鍵。 您無法使用 xmlns。 在 XPath 語句中,在每個節點名稱前面加上命名空間名稱和冒號,例如 《Path)語句中,在每個節點名稱前面加上命名空間名稱和冒號,例如 「10000」namespaceName:Node。
參數屬性
| 類型: | Hashtable |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Path
指定要搜尋之 XML 檔案的路徑和檔名。 允許使用通配符字元。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
Path
| Position: | 1 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Xml
指定一或多個 XML 節點。
XML 檔會當做 XML 節點的集合來處理。 如果您使用管線將 XML 檔傳送至 Select-Xml,則每個文件節點都會在管線中個別搜尋。
參數屬性
| 類型: | XmlNode[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | Node |
參數集
Xml
| Position: | 1 |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-XPath
指定 XPath 搜尋查詢。 查詢語言會區分大小寫。 這是必要參數。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 Cmdlet 支援一般參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
System.String or System.Xml.XmlNode
您可以使用管線將路徑或 XML 節點傳送至此 Cmdlet。