Author Posts

October 17, 2015 at 4:55 am

I have a custom psobject that a wish to export as un xml file

$array = @()
        Get-ADGroup -Filter * -SearchBase "$OU,$domain" |
            select-object "distinguishedName","GroupScope","name" | % {
                $array += New-object  PSObject -Property([ordered]@{"Name"="$($_.Name)";
                                                    "GroupScope" = "$($_.GroupScope)";
                                                    "DistinguishedName" = "$($_.distinguishedName)";
                                                    "Members"="$((Get-ADGroupMember $_.Name | Select-Object Name).Name)"})
                                                    }#end foreach

        $date = get-date -Format yyyyMMdd
        
        Switch ($ExportType)
        {
                HTML { $array | ConvertTo-Html -as List | Out-File "$Outfolder\ADGroups-html-$date.html" }
                XML { $array | ConvertTo-Xml | Out-File "$Outfolder\ADGroups-xml-$date.xml" }
                CSV { $array | ConvertTo-Csv| Out-File "$Outfolder\ADGroups-csv-$date.csv" }
                
        }#end switch

The export works fine with csv and HTML but i can't figure out why its failling with the XML

October 17, 2015 at 6:26 am

Technically ConvertTo-XML is working but in its current usage it is returning an XML document object and only showing the top two properties. You can drill down into the object and see the rest of it. What you are wanting is to view it differently.

A couple of options. You could specify the -as parameter (string | stream) for your ConverTo-XML cmdlet or you could use Export-CliXML.

October 17, 2015 at 7:08 am

Thanks a lot Curtis, "-as stream" has got me what i wanted, now i just need to go through Don jones book to format my HTML output.