Convertto-xml help


This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Cyril Muffat Cyril Muffat 1 year, 9 months ago.

  • Author
  • #30952
    Profile photo of Cyril Muffat
    Cyril Muffat

    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

  • #30954
    Profile photo of Curtis Smith
    Curtis Smith

    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.

  • #30955
    Profile photo of Cyril Muffat
    Cyril Muffat

    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.

You must be logged in to reply to this topic.