Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Tuesday, November 3, 2009

How to parse data in an XML file.

PowerShell gives us many options for working with different types of data. One of the questions from class was how to parse through data in an XML file. Before we explore parsing through an XML file, we need to first create one.

Get-Process | Export-Clixml C:\xmlfile.xml

If you double click the xmiFile.xml, it will open to show you the contents of the file in XML.

Now we need to import this file into a variable so we can work with it.

$a = import-Clixml c:\xmlfile.xml

We can view the contents of the file by typing $a.

Since we exported an object to the XML, the import will have properties. Go ahead and type $a | gm. The data was also entered into the variable $a as an array. Type $a.count to get the number of cells in the array. Since the variable is an array with properties, we can enumerate the values of each item. For example, type $a[0].name. Using this information we can parse the data for what we are looking for.

Let's filter the data:

$a | where {$_.cpu -gt 20}

By filtering, you can isolate the information that you are interested in. For Example, $A | FL Name, CPU. To find out all the properties that you can call up, type: $A | gm - MemberType Property

No comments: