Convert XML to CSV not working
Convert XML to CSV not working
XML file Content:
<?xml version="1.0"?>
<servstate>
<title>2018-06-11 12:00:01</title>
<services>
<service>
<name>Oracle Weblogic AdminServer 1</name>
<state>RUNNING.</state>
</service>
<service>
<name>Oracle WebLogic NodeManager 1</name>
<state>RUNNING.</state>
</service>
<service>
<name>OracleProcessManager_1</name>
<state>RUNNING.</state>
</service>
<service>
<name>OracleProcessManager_2</name>
<state>RUNNING.</state>
</service>
<service>
<name>Oracle WebLogic NodeManager 2</name>
<state>RUNNING.</state>
</service>
<service>
<name>Oracle Weblogic AdminServer 2</name>
<state>RUNNING.</state>
</service>
</services>
</servstate>
I am new to PowerShell and written a script to parse the above XML as CSV file but I got the result as below.
Script:
$xml = [xml](Get-Content .monitoring_services.xml)
$xml.servstate.services.service |
Select-Object @(@{l="name";e={$_.name."#text"}}, @{l="state";e={$_.state."#text"}}) |
Export-Csv test.csv -NoTypeInformation
Result:
Expected result:
Could you help correct what went wrong the script?
@{l="name"...
@{n="name"...
@iRon - Tried but same result ..
– Dheeban Chakkaravarthy
2 days ago
@iRon L for label or n for name are both valid. But that's not he point see RobinSidharth's answer.
– LotPings
2 days ago
2 Answers
2
Why don't you just use this?
$xml.servstate.services.service | Export-Csv test.csv -NoTypeInformation
Why are you complicating things? Why don't you just select the Name and the State and export it to CSV:
$xml.servstate.services.service | Select-Object Name, State | Export-Csv test.csv -NoTypeInformation
You don't need to create custom properties if you already have something that you need.
Since at that level there are only name and state the select isn't neccessary, and your answer differs in nothing from @RobinSidharth answer.
– LotPings
2 days ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
@{l="name"...
should be:@{n="name"...
– iRon
2 days ago