Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In Powershell, how do I extract multiple values from XML

In this XML:

<?xml version="1.0">
<Devices ClientType="Desktop">
<Device HostName="12345-Desktop">
      <Parameter Name="DefaultEnvironment" Value="Test" />      
      <Parameter Name="WorkstationID" Value="T100" />
      <Parameter Name="WorkstationAgency" Value="A" />
      <Parameter Name="ServerIP_Trn" Value="tng.domain.com" />
      <Parameter Name="ServerIP_Stg" Value="stg.domain.com" />
      <Parameter Name="Production" Value="TRUE" />
      <Parameter Name="Training" Value="FALSE" />
      <Parameter Name="Staging" Value="FALSE" />
     </Device>
</Devices>

I’m trying to extract the hostname and WorkstationID in a single line. What I would like to see is

12345-Desktop   T100
 

This is what i’ve tried:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[xml]$Config = $wc.DownloadString("http://xmlsource/xmlfile.xml")
$device=$Config.Devices.Device

#to get hostname:
$devices.hostname

#To get parameters:
$devices.parameter | select Name, value | where {$_.name -eq 'WorkstationID'}

How do combine both outputs in one line like the above? Do I need to use XPath?

>Solution :

You’re on the right track, but you’ll need a calculated property in order to reach into the array:

$devices | Select-Object -Property @(
    'HostName'
    @{
        L = 'WorkstationID'
        E = { $PSItem.Parameter.Where{ $PSItem.Name -eq 'WorkstationID' }.Value }
    }
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading