PowerShell XML parsing

omnipower321
omnipower321 used Ask the Experts™
on
Hello,

I am trying to get some data out of an xml file in PowerShell.  The XML contains the following:

<?xml version="1.0" encoding="iso8859-1"?>
<License xmlns:dt="urn:schemas-microsoft-com:datatypes">
        <Product>
                <Name>App1</Name>
                <LicenseState dt:dt="int">1</LicenseState>
                <Version>4.0</Version>
                <Clients>-1</Clients>
                <Throughput>
                        <Item>Documents</Item>
                        <TimeUnit>Year</TimeUnit>
                        <RefreshTime dt:dt="int">1</RefreshTime>
                        <RefreshCount dt:dt="int">46100000</RefreshCount>
                        <ActCount dt:dt="int">27440282</ActCount>
                </Throughput>
        </Product>
        <Product>
                <Name>App2</Name>
                <LicenseState dt:dt="int">0</LicenseState>
                <Version>4.0</Version>
                <Clients>-1</Clients>
                <Throughput>
                        <Item>Documents</Item>
                        <TimeUnit>Year</TimeUnit>
                        <RefreshTime dt:dt="int">1</RefreshTime>
                        <RefreshCount dt:dt="int">0</RefreshCount>
                        <ActCount dt:dt="int">0</ActCount>
                </Throughput>
        </Product>
</License>

Open in new window


So in PowerShell I am running:

[xml]$x = GC .\Test.xml

Open in new window


If I run
 $x.License.Product

Open in new window

I get:


Name         : App1
LicenseState : LicenseState
Version      : 4.0
Clients      : -1
Throughput   : Throughput

Name         : App2
LicenseState : LicenseState
Version      : 4.0
Clients      : -1
Throughput   : Throughput


If I run
$x.License.Product | %{$_.ThroughPut}

Open in new window

I get:

format-default : The member "Item" is already present.
    + CategoryInfo          : NotSpecified: (:) [format-default], ExtendedTypeSystemException
    + FullyQualifiedErrorId : AlreadyPresentPSMemberInfoInternalCollectionAdd,Microsoft.PowerShell.Commands.FormatDefaultComma
   nd


If I run
 $x.License.Product.ThroughPut

Open in new window

, I get nothing.

I am trying to gather the ActCount data, but can figure out how to get to it.

Thank you for any assistance!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I don't know loads about parsing xml, but when I test with your XML file, it seems to be an issue with the Item property already existing as a member on the object.

If you run

$x.License.Product | Gm

You should see a parameterizedproperty called Item.

When I renamed 'Item' in your xml file to something else, I had more success. Once the Item field had been renamed (i renamed it to item3), i ran

$x.License.Product | %{$_.ThroughPut} | Select -expand ActCount

and it bought back the following

dt                                                          #text
--                                                          -----
int                                                         27440282
int                                                         0

Would it be a big job to rename the Item field in the XML file?

Author

Commented:
Thank you for the info!  This is what I ended up with:

[xml]$x = GC ".\test.xml"
$x.License.Product | %{
      "Application Name: $($_.Name)"
      "Current Count: $(($_.ThroughPut | Select-Object -ExpandProperty ActCount)."#text")`n"
}

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial