2

I have an xml file that contains two different values that I am trying to capture in each line. I am trying to use Powershell to parse them into a csv, two columns, one being Device ID and one being MAC address, with headings. Any suggestions would be greatly appreciated. Below is a sample of what the xml file looks like that I would be working with. Thank you for your time.

<?xml version="1.0" encoding="utf-8"?>
<DeviceConfig>
    <Device RegisterID="1021" MacID="A4F1E85D7D86" />
    <Device RegisterID="1022" MacID="A4F1E85D056E" />
    <Device RegisterID="1023" MacID="A4F1E85CAAEE" />
    <Device RegisterID="1024" MacID="A4F1E85DB284" />
    <Device RegisterID="1025" MacID="A4F1E85D7021" />
    <Device RegisterID="1026" MacID="A4F1E85D034A" />
    <Device RegisterID="1027" MacID="A4F1E85CAD59" />
    <Device RegisterID="1028" MacID="A4F1E85DAFA2" />
    <Device RegisterID="1029" MacID="A4F1E85D050E" />
    <Device RegisterID="1030" MacID="A4F1E85DAF89" />
    <Device RegisterID="1031" MacID="A4F1E85DA80E" />
</DeviceConfig>

3 Answers 3

4

You can replace my here string with a call to Get-Content for your actual file, but this works for me, and does not need to bring in .NET classes.

Code

$xml = @"
<?xml version="1.0" encoding="utf-8"?>
<DeviceConfig>
    <Device RegisterID="1021" MacID="A4F1E85D7D86" />
    <Device RegisterID="1022" MacID="A4F1E85D056E" />
    <Device RegisterID="1023" MacID="A4F1E85CAAEE" />
    <Device RegisterID="1024" MacID="A4F1E85DB284" />
    <Device RegisterID="1025" MacID="A4F1E85D7021" />
    <Device RegisterID="1026" MacID="A4F1E85D034A" />
    <Device RegisterID="1027" MacID="A4F1E85CAD59" />
    <Device RegisterID="1028" MacID="A4F1E85DAFA2" />
    <Device RegisterID="1029" MacID="A4F1E85D050E" />
    <Device RegisterID="1030" MacID="A4F1E85DAF89" />
    <Device RegisterID="1031" MacID="A4F1E85DA80E" />
</DeviceConfig>
"@

$xmlData = [xml]$xml

$xmlData.DeviceConfig.ChildNodes | ConvertTo-Csv -NoTypeInformation

Output

"RegisterID","MacID"
"1021","A4F1E85D7D86"
"1022","A4F1E85D056E"
"1023","A4F1E85CAAEE"
"1024","A4F1E85DB284"
"1025","A4F1E85D7021"
"1026","A4F1E85D034A"
"1027","A4F1E85CAD59"
"1028","A4F1E85DAFA2"
"1029","A4F1E85D050E"
"1030","A4F1E85DAF89"
"1031","A4F1E85DA80E"

With this information, you should be able to make progress...

Sign up to request clarification or add additional context in comments.

5 Comments

I like this approach too.
This worked perfectly - thank you so much for the assistance! By using Export-csv instead of ConvertTo-csv, I was able to export to a .csv file like I wanted. Cheers!
There is one additional piece of info I would like to add to the beginning of each row in the .csv file. I have a variable for $servername in my larger script, and I would like to put a column for "Server Name" in this .csv and add the value of $servername in each row (before the RegisterID and MacID). Any ideas on how to best approach this? Thank you!
If I should start a new thread for this, just let me know. :)
A new question would be best and in the spirit of SO.
2

I think something like this will work. Here I am reading the XML from a string, you may be importing it from a file:

$xml = @"
<?xml version="1.0" encoding="utf-8"?>
<DeviceConfig>
    <Device RegisterID="1021" MacID="A4F1E85D7D86" />
    <Device RegisterID="1022" MacID="A4F1E85D056E" />
    <Device RegisterID="1023" MacID="A4F1E85CAAEE" />
    <Device RegisterID="1024" MacID="A4F1E85DB284" />
    <Device RegisterID="1025" MacID="A4F1E85D7021" />
    <Device RegisterID="1026" MacID="A4F1E85D034A" />
    <Device RegisterID="1027" MacID="A4F1E85CAD59" />
    <Device RegisterID="1028" MacID="A4F1E85DAFA2" />
    <Device RegisterID="1029" MacID="A4F1E85D050E" />
    <Device RegisterID="1030" MacID="A4F1E85DAF89" />
    <Device RegisterID="1031" MacID="A4F1E85DA80E" />
</DeviceConfig>
"@

$csv = `
    Select-Xml -Content $xml -XPath "//Device" `
    | Select -ExpandProperty Node `
    | ConvertTo-Csv -NoTypeInformation

$csv

This is the content of $csv:

"RegisterID","MacID"
"1021","A4F1E85D7D86"
"1022","A4F1E85D056E"
"1023","A4F1E85CAAEE"
"1024","A4F1E85DB284"
"1025","A4F1E85D7021"
"1026","A4F1E85D034A"
"1027","A4F1E85CAD59"
"1028","A4F1E85DAFA2"
"1029","A4F1E85D050E"
"1030","A4F1E85DAF89"
"1031","A4F1E85DA80E"

You should be able to write that out to a file if you need.

1 Comment

This is a good approach. If the xml is simple and/or you don't need to build queries, I like the '[xml]' type casting.
0

You need to use .Net classes here, pure PS won't help. Fortunately, it's easy.

Add-Type -AssemblyName System.Xml.Linq
$xe = [System.Xml.Linq.XElement]::Parse($xml)
$xe.Elements() | % {$_.Attribute('RegisterID').Value + ", " + $_.Attribute('MacID').Value}

This prints what you need to output. Saving to file is covered by many other replies.

1 Comment

I actually found that it was not necessary to use .Net classes. Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.