PowerWF News, Tutorials, and more...

Get the latest tips and tricks directly from the PowerWF development team. Find out about new releases and upcoming features.


Seamless Automation

From the Desktop to the Data Center

The PowerWF family of products are easy enough for desktop and departmental automation, yet powerful and scalable enough for the Data Center. PowerWF compliments Opalis and other RBA solutions, lets you leverage your workforce and preserves your investment as your automation needs grow.


Special - 20% Discount


In conjunction with our recently announced Silver Award from Windows IT Magazine we would like to offer our customers an opportunity to save 20% off any Devfarm product purchase through the end of the year. This includes all PowerWF products as well as Devfarm's new PowerVI product!

PowerVI

vSphere Automation fueled by PowerShell


Designed for the VMware Administrator, PowerVI eases the automation of vSphere infrastructures. PowerVI includes over 100 PowerShell automation scripts that simplify everyday VMware administration tasks and PowerVI makes it easy to author new scripts.
November 25, 2011

How to Export PowerShell Data to XML

I saw a question on StackOverflow the other day asking how to save PowerShell results as XML.  If you are lucky, this could be as simple as using the export-clixml cmdlet.

But what if you are doing something more complex where you are pulling data from multiple sources or you want to control every aspect of the structure of the XML you are creating.  In that case, I think you have 2 options.

The simplest way would be to build up a string as your xml -

$procs = get-Process

$xml = "<xml>`r`n"
foreach($proc in $procs)
{ 
   
$xml += "<process name='" + $proc.Name + "' id='Proc_"+$proc.Id+"'>`r`n"

$xml += "<modules>`r`n" foreach($module in $proc.Modules) { $xml += "<module>" $xml += $module.ModuleName $xml += "</module>`r`n" } $xml += "</modules>`r`n" $xml += "</process>`r`n" } $xml += "</xml>`r`n" $xml | out-File -FilePath c:\temp\proc1.xml

The downside of this approach is that you need to build in you are reinventing the wheel and will therefore be responsible for any error checking and insuring that your final document is well formatted XML.

In many cases, a better way would be to build up your output as psobject/arrays/hashtables/etc.  You can then use the PowerShell cmdlets for additional massaging if necessary and use the export-clixml to output it as XML. That would look something like this -

    $procs = get-Process

    $processObject = @()
    foreach($proc in $procs)
    {
        $procInfo = new-Object PSObject
        $procInfo | add-Member NoteProperty -Name "Process" $proc.Name
        $procInfo | add-Member NoteProperty -Name "ID" $proc.Id
        $moduleInfo = @()  
        foreach($module in $proc.Modules)
        {          
            $moduleInfo += $module.ModuleName
        }  
        $procInfo | add-Member NoteProperty -Name "Module" $moduleInfo
        $processObject += $procInfo
    }

    $processObject | export-Clixml c:\temp\procs.xml



blog comments powered by Disqus