karkou12
asked on
Read xml file attributes using powershell
I am trying to read a XML with powershell. According to the value of Name attribute in <Resource> tag, I should retrieve the value of ResourceUri attribute in < <Uri> tag.
Sample XML is given below. I tried the following. But I am able to achieve it.
If Name in <Resource> tag is XYZ then the ResouceUri value = http://ustfinder.com/ExternalApp.aspx/NewApp?CRWAAMONITOR should be retrived.
$var = Select-Xml -Path "C:\Temp\Desktop\EIB Autaomation\abc.xml" -XPath "/MLIFRTCUI/Businesses/Bus iness/Reso urce" | where { $_.Node.ParentNode.Name -eq "xyz"} | % {$_.Node.ResourceUri} | select -unique
Please help with this script.
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -
<MLIFRTCUI>
<!-- DATE-TIME: 2015-10-06 14:13:01 (UTC) -->
<!-- UI SERVER: MPHEW1VQSH001 -->
<!-- SQL FQDN: ifrtcdbrw.pl1.us.ml.com -->
<!-- SQL SERVER: MPHEW1MCSH004 -->
<Businesses>
<Business Name="Sikarbulls" Default="0" LegacyName="">
<Resource Name="XYZ">
<Uri UriType="PAGE" ResourceUri="http://ustfinder.com/ExternalApp.aspx/NewApp?CRWAAMONITOR" />
</Resource>
<Resource Name="ABC">
<Uri UriType="PAGE" ResourceUri="http://ustpfuiv4.com/USTCMTUI/CMTMain.aspx?MenuItem=PartyMaintenance" />
</Resource>
<Resource Name="FGH">
<Uri UriType="PAGE" ResourceUri="http://ustpfuiv4/USTCMTUI/CMTMain.aspx?MenuItem=RoleMaintenance" />
</Resource>
<Resource Name="CRWACTIONITEMS">
<Uri UriType="PAGE" ResourceUri="http://ustfinder.com/Dashboard.aspx/ActionItem" />
</Resource>
<Resource Name="CRWAFS">
<Uri UriType="PAGE" ResourceUri="http://ustpfuiv4/USTCMTUI/CMTMain.aspx?MenuItem=USTRelationships" />
</Resource>
</Business>
</Businesses>
Sample XML is given below. I tried the following. But I am able to achieve it.
If Name in <Resource> tag is XYZ then the ResouceUri value = http://ustfinder.com/ExternalApp.aspx/NewApp?CRWAAMONITOR should be retrived.
$var = Select-Xml -Path "C:\Temp\Desktop\EIB Autaomation\abc.xml" -XPath "/MLIFRTCUI/Businesses/Bus
Please help with this script.
--------------------------
<MLIFRTCUI>
<!-- DATE-TIME: 2015-10-06 14:13:01 (UTC) -->
<!-- UI SERVER: MPHEW1VQSH001 -->
<!-- SQL FQDN: ifrtcdbrw.pl1.us.ml.com -->
<!-- SQL SERVER: MPHEW1MCSH004 -->
<Businesses>
<Business Name="Sikarbulls" Default="0" LegacyName="">
<Resource Name="XYZ">
<Uri UriType="PAGE" ResourceUri="http://ustfinder.com/ExternalApp.aspx/NewApp?CRWAAMONITOR" />
</Resource>
<Resource Name="ABC">
<Uri UriType="PAGE" ResourceUri="http://ustpfuiv4.com/USTCMTUI/CMTMain.aspx?MenuItem=PartyMaintenance" />
</Resource>
<Resource Name="FGH">
<Uri UriType="PAGE" ResourceUri="http://ustpfuiv4/USTCMTUI/CMTMain.aspx?MenuItem=RoleMaintenance" />
</Resource>
<Resource Name="CRWACTIONITEMS">
<Uri UriType="PAGE" ResourceUri="http://ustfinder.com/Dashboard.aspx/ActionItem" />
</Resource>
<Resource Name="CRWAFS">
<Uri UriType="PAGE" ResourceUri="http://ustpfuiv4/USTCMTUI/CMTMain.aspx?MenuItem=USTRelationships" />
</Resource>
</Business>
</Businesses>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The code you posted worked well. Thank you Much.
I want to further enhance this to capture the data I need into a CSV file. The following code does it almost. For example in the below XML file in some <URI> tags the value of ResourceUri attribute have 2 urls and for some <URI> tags the value of ResourceUri attribute have 1 url.
The problem I see with the code is it is able to capture 1 URL values correctly, but for tags having 2 or more URLS, data is not captured in a good way.
--------------------------
Open in new window
--------------------------
<MLIFRTCUI>
<Businesses>
<Business Name="Sikarbulls" Default="0" LegacyName="">
<Resource Name="XYZ">
<Uri UriType="PAGE" ResourceUri="http://ustfinder.com/ExternalApp.aspx/NewApp?CRWAAMONITOR" />
</Resource>
<Resource Name="ABC">
<Uri UriType="PAGE" ResourceUri="http://ustpfuiv4.com/USTCMTUI/CMTMain.aspx?MenuItem=PartyMaintenance" />
</Resource>
<Resource Name="FGH">
<Uri UriType="PAGE" ResourceUri="http://ustpfuiv4/USTCMTUI/CMTMain.aspx?MenuItem=RoleMaintenance" />
</Resource>
<Resource Name="JKL">
<Uri UriType="PAGE" ResourceUri="http://ustfinder.com/Dashboard.aspx/ActionItem" />
<Uri UriType="PAGE" ResourceUri="http://xasdefr.aspx" />
</Resource>
<Resource Name="CRWAFS">
<Uri UriType="PAGE" ResourceUri="http://ustpfuiv4/USTCMTUI/CMTMain.aspx?MenuItem=USTRelationships" />
<Uri UriType="PAGE" ResourceUri="http://abc.aspx" />
</Resource>
</Business>
</Businesses>
</MLIFRTCUI>