Mark Duncker
Mark Duncker

Reputation: 21

Powershell XML file to table view

In our Remote Management System we can execute HPiLOcmdlets to retrieve data from the iLO hardware management card in a HP Server.

Example:

$tempstatus = Get-HPiLOTemperature -Server $server -Username $Username -Password $Password -Output XML

This will generate an XML file, that i want to use as input and output as a table in Powershell

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<RIBCL VERSION="2.23">
   <TEMPERATURE>
      <TEMP>
         <LABEL VALUE="01-Inlet Ambient" />
         <LOCATION VALUE="Ambient" />
         <STATUS VALUE="0K" />
         <CURRENTREADING VALUE="23" UNIT="Celsius" />
         <CAUTION VALUE="42" UNIT="Celsius" />
         <CRITICAL VALUE="46" UNIT="Celsius" />
      </TEMP>
      <TEMP>
         <LABEL VALUE="02-CPU 1" />
         <LOCATION VALUE="CPU" />
         <STATUS VALUE="0K" />
         <CURRENTREADING VALUE="40" UNIT="Celsius" />
         <CAUTION VALUE="70" UNIT="Celsius" />
         <CRITICAL VALUE="N/A" />
      </TEMP>
      <TEMP>
         <LABEL VALUE="03-CPU 2" />
         <LOCATION VALUE="CPU" />
         <STATUS VALUE="0K" />
         <CURRENTREADING VALUE="49" UNIT="Celsius" />
         <CAUTION VALUE="70" UNIT="Celsius" />
         <CRITICAL VALUE="N/A" />
      </TEMP>
   </TEMPERATURE>
</RIBCL>

So far i tried :

[xml]$XmlDocument = get-content test.xml
$XmlDocument.RIBCL.TEMPERATURE.ChildNodes | Format-Table

Result:

xml

Wanted output:

xml

Thanks for any help !!!

Upvotes: 2

Views: 2077

Answers (3)

Garett
Garett

Reputation: 16838

In addition to the answers already given. The following also works.

[xml]$XmlDocument = get-content test.xml

$XmlDocument.RIBCL.TEMPERATURE.TEMP | Format-Table -Property @{LABEL="LABEL"; Expression={$_.LABEL.VALUE}}, 
    @{LABEL="LOCATION"; Expression={$_.LOCATION.VALUE}}, 
    @{LABEL="STATUS"; Expression={$_.STATUS.VALUE}}, 
    @{LABEL="CURRENTREADING"; Expression={$_.CURRENTREADING.VALUE + " " + $_.CURRENTREADING.UNIT}}, 
    @{LABEL="CAUTION"; Expression={$_.CAUTION.VALUE + " " + $_.CAUTION.UNIT}}, 
    @{LABEL="CRITICAL"; Expression={$_.CRITICAL.VALUE + " " + $_.CRITICAL.UNIT}}

Upvotes: 0

lit
lit

Reputation: 16266

The following code might work for you.

[xml]$XmlDocument = get-content hplio.xml

foreach ($r in $XmlDocument.RIBCL.TEMPERATURE.TEMP) {
    $info = [ordered]@{}
    $info.LABEL = $r.LABEL.VALUE
    $info.LOCATION = $r.LOCATION.VALUE
    $info.STATUS = $r.STATUS.VALUE
    $info.CURRENTREADING = $r.CURRENTREADING.VALUE + ' ' + $r.CURRENTREADING.UNIT
    $info.CAUTION = $r.CAUTION.VALUE + ' ' + $r.CAUTION.UNIT
    $info.CRITICAL = $r.CRITICAL.VALUE + ' ' + $r.CRITICAL.UNIT

    $reading = New-Object -TypeName PSObject -Prop $info
    Write-Output $reading
}

Sample output using .\temp.ps1 | ft

LABEL            LOCATION STATUS CURRENTREADING CAUTION    CRITICAL
-----            -------- ------ -------------- -------    --------
01-Inlet Ambient Ambient  0K     23 Celsius     42 Celsius 46 Celsius
02-CPU 1         CPU      0K     40 Celsius     70 Celsius N/A
03-CPU 2         CPU      0K     49 Celsius     70 Celsius N/A

Upvotes: 0

Mike Shepard
Mike Shepard

Reputation: 18176

I didn't get the text 'celsius' in the table (because those columns are handled differently, but here's some code that splits up the xml the way you want. It just creates new objects with the value

[xml]$XmlDocument = get-content test.xml
$XmlDocument.RIBCL.Temperature.TEMP | foreach-object {
    $obj=[ordered]@{}
    foreach($node in $_.ChildNodes){
       $obj[$node.Name]=($node.Value+ " " + $node.Unit).Trim()
    }
    write-output ([pscustomobject]$obj)
  } | format-table 

Upvotes: 1

Related Questions