Select-Xml
Hiermee zoekt u tekst in een XML-tekenreeks of -document.
Syntaxis
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
Met de cmdlet Select-Xml kunt u XPath-query's gebruiken om te zoeken naar tekst in XML-tekenreeksen en -documenten. Voer een XPath-query in en gebruik de parameter Inhoud, Padof xml- parameter om de XML op te geven die moet worden doorzocht.
Voorbeelden
Voorbeeld 1: AliasProperty-knooppunten selecteren
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
In dit voorbeeld worden de aliaseigenschappen opgehaald in types.ps1xml. (Zie about_Types.ps1xml voor meer informatie over dit bestand.)
Met de eerste opdracht wordt het pad naar het bestand Types.ps1xml opgeslagen in de variabele $Path.
Met de tweede opdracht wordt het XML-pad opgeslagen naar het knooppunt AliasProperty in de $XPath variabele.
De derde opdracht maakt gebruik van de cmdlet Select-Xml om de AliasProperty-knooppunten op te halen die worden geïdentificeerd door de XPath-instructie uit het bestand Types.ps1xml. De opdracht maakt gebruik van een pijplijnoperator om de AliasProperty-knooppunten naar de Select-Object-cmdlet te verzenden. De parameter ExpandProperty breidt het Node-object uit en retourneert de eigenschappen Name en ReferencedMemberName.
Het resultaat toont de naam en referencedMemberName van elke aliaseigenschap in het bestand Types.ps1xml. Er is bijvoorbeeld een eigenschap Count die een alias is van de eigenschap Length.
Voorbeeld 2: Een XML-document invoeren
PS C:\> [xml]$Types = Get-Content $Pshome\Types.ps1xml
PS C:\> Select-Xml -Xml $Types -XPath "//MethodName"
In dit voorbeeld ziet u hoe u de parameter XML- gebruikt om een XML-document op te geven aan de cmdlet Select-Xml-.
De eerste opdracht maakt gebruik van de Get-Content cmdlet om de inhoud van het bestand Types.ps1xml op te halen en op te slaan in de $Types variabele. De variabele [XML] cast de variabele als een XML-object.
De tweede opdracht maakt gebruik van de cmdlet Select-Xml om de MethodName-knooppunten op te halen in het bestand Types.ps1xml. De opdracht gebruikt de parameter Xml om de XML-inhoud op te geven in de variabele $Types en de parameter XPath om het pad naar het MethodName-knooppunt op te geven.
Voorbeeld 3: PowerShell Help-bestanden zoeken
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
...
In dit voorbeeld ziet u hoe u de cmdlet Select-Xml gebruikt om de Help-bestanden van de PowerShell XML-cmdlet te doorzoeken. In dit voorbeeld zoeken we naar de naam van de cmdlet die fungeert als een titel voor elk Help-bestand en het pad naar het Help-bestand.
Met de eerste opdracht maakt u een hash-tabel die de XML-naamruimte vertegenwoordigt die wordt gebruikt voor de Help-bestanden en slaat deze op in de $Namespace variabele.
Voorbeeld 4: Verschillende manieren om XML in te voeren
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
In dit voorbeeld ziet u twee verschillende manieren om XML te verzenden naar de cmdlet Select-Xml-.
Met de eerste opdracht wordt een hier-tekenreeks opgeslagen die XML bevat in de $Xml variabele. (Zie about_Quoting_Rules voor meer informatie over hier-tekenreeksen.)
Voorbeeld 5: De standaard-XMLNS-naamruimte gebruiken
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}
In dit voorbeeld ziet u hoe u de cmdlet Select-Xml gebruikt met XML-documenten die gebruikmaken van de standaard-XMLNS-naamruimte. In het voorbeeld worden de titels van door de gebruiker gemaakte codefragmentbestanden van Windows PowerShell ISE opgeslagen. Zie New-IseSnippet voor meer informatie over fragmenten.
Met de eerste opdracht maakt u een hash-tabel voor de standaardnaamruimte die XML-bestanden gebruikt en wijst deze toe aan de variabele $SnippetNamespace. De hashtabelwaarde is de XMLNS-schema-URI in het XML-fragment-XML. De naam van de hash-tabelsleutel, knipsel, is willekeurig. U kunt elke naam gebruiken die niet is gereserveerd, maar u kunt geen XMLN's gebruiken.
Parameters
-Content
Hiermee geeft u een tekenreeks op die de XML bevat om te zoeken. U kunt ook tekenreeksen doorsluizen naar Select-Xml-.
Type: | String[] |
Position: | Named |
Default value: | None |
Vereist: | True |
Pijplijninvoer accepteren: | True |
Jokertekens accepteren: | False |
-LiteralPath
Hiermee geeft u de paden en bestandsnamen van de XML-bestanden te zoeken. In tegenstelling tot Pad, wordt de waarde van de parameter LiteralPath exact gebruikt zoals deze is getypt. Er worden geen tekens geïnterpreteerd als jokertekens. Als het pad escapetekens bevat, plaatst u het tussen enkele aanhalingstekens. Enkele aanhalingstekens geven PowerShell aan dat er geen tekens als escapereeksen moeten worden geïnterpreteerd.
Type: | String[] |
Aliassen: | PSPath |
Position: | Named |
Default value: | None |
Vereist: | True |
Pijplijninvoer accepteren: | True |
Jokertekens accepteren: | False |
-Namespace
Hiermee geeft u een hash-tabel op van de naamruimten die worden gebruikt in de XML. Gebruik de notatie @{<namespaceName> = <namespaceValue>}.
Wanneer de XML gebruikmaakt van de standaardnaamruimte, die begint met XMLN's, gebruikt u een willekeurige sleutel voor de naamruimtenaam. U kunt geen XMLN's gebruiken. Geef in de XPath-instructie het voorvoegsel van elke knooppuntnaam met de naamruimtenaam en een dubbele punt, zoals //namespaceName:Node.
Type: | Hashtable |
Position: | Named |
Default value: | None |
Vereist: | False |
Pijplijninvoer accepteren: | False |
Jokertekens accepteren: | False |
-Path
Hiermee geeft u het pad en de bestandsnamen van de XML-bestanden te zoeken. Jokertekens zijn toegestaan.
Type: | String[] |
Position: | 1 |
Default value: | None |
Vereist: | True |
Pijplijninvoer accepteren: | True |
Jokertekens accepteren: | True |
-Xml
Hiermee geeft u een of meer XML-knooppunten op.
Een XML-document wordt verwerkt als een verzameling XML-knooppunten. Als u een XML-document doorsluist naar Select-Xml-, wordt elk documentknooppunt afzonderlijk doorzocht terwijl het via de pijplijn komt.
Type: | XmlNode[] |
Aliassen: | Node |
Position: | 1 |
Default value: | None |
Vereist: | True |
Pijplijninvoer accepteren: | True |
Jokertekens accepteren: | False |
-XPath
Hiermee geeft u een XPath-zoekquery op. De querytaal is hoofdlettergevoelig. Deze parameter is vereist.
Type: | String |
Position: | 0 |
Default value: | None |
Vereist: | True |
Pijplijninvoer accepteren: | False |
Jokertekens accepteren: | False |
Invoerwaarden
System.String or System.Xml.XmlNode
U kunt een pad of XML-knooppunt doorsluisen naar deze cmdlet.
Uitvoerwaarden
Notities
- XPath is een standaardtaal die is ontworpen om delen van een XML-document te identificeren. Zie XPath Reference en de sectie Selectiefilters van de Gebeurtenisselectie in de MSDN-bibliotheek voor meer informatie over de XPath-taal.