
close
close
Upcoming FREE Conference on Identity Management and Privileged Access Management
In a previous article, I guided you through using the Export-Clixml and Import-Clixml cmdlets to serialize and deserialize data from a PowerShell expression. However, as I demonstrated in earlier articles in this series, you could also use ConvertTo-XML. This cmdlet will generate XML that is more compatible with non-PowerShell applications. But what if you also wanted to bring the data back to life in PowerShell? There is no ConvertFrom-XML cmdlet, at least as of today, so it will take a little extra work.
Here is an XML file I created in a previous article from a Get-Service command.
[xml]$in = Get-Content C:\work\wu2.xml
The [xml] tells PowerShell, “treat the content as an XML document.” Which it now becomes.
$data = $in.Objects.Object | foreach { #initialize an empty hash table $hash = @{} #enumerate each property and add it to the hashtable foreach ($prop in $_.property) { $hash.Add($prop.name,$prop.'#text') } #Write the hashtable to the pipeline as a custom object [pscustomobject]$hash }
The results seem ok at first.
$data2 = $in.Objects.Object | foreach { #initialize an empty hash table $hash = @{} #enumerate each property and add it to the hashtable foreach ($prop in $_.property) { $hash.Add($prop.name,$prop.'#text' -as $prop.Type) } #Write the hashtable to the pipeline as a custom object [pscustomobject]$hash }
Unfortunately, this may not always be 100 percent successful.
$theType = $in.Objects.Object[0].Type $data3 = $in.Objects.Object | foreach { #initialize an empty hash table $hash = @{} #enumerate each property and add it to the hashtable foreach ($prop in $_.property) { $hash.Add($prop.name,$prop.'#text' -as $prop.Type) } #Turn the hashtable into custom object $obj = [pscustomobject]$hash #insert a new typename $obj.psobject.typenames.insert(0,$theType) #write the object to the pipeline $obj }
Now look at the results:
[xml]$in = Get-Content C:\work\wu3.xml #skip XML declaration node $nodes = $in.ChildNodes | Select -Skip 1 foreach ($node in $nodes.ChildNodes) { $hash = @{} #enumerate each property and add it to the hashtable #this assumes you don't know or care what the node #might be called foreach ($item in $node.($node.firstchild.LocalName)) { Try { $hash.Add($item.name,$item.'#text' -as $item.Type) } Catch { #ignore conversion errors } } #Turn the hashtable into custom object $obj = [pscustomobject]$hash #insert a new typename $obj.psobject.typenames.insert(0,$node.type) #write the object to the pipeline $obj }
This example uses properties of the XML document itself to enumerate itself.
The main takeaway from all of this is that you need to know in advance what your XML file looks like so that you can write a script or function to properly import it into PowerShell. If your XML file doesn’t include type information, and it matters to you, then you’ll have to take other steps to convert those items to the proper type.
If you have anything more complex, then bringing it into PowerShell will take even more effort. But I’ll try to guide you through that process next time.
More in PowerShell
Most popular on petri