Convertto-xml help

Welcome Forums General PowerShell Q&A Convertto-xml help

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
3 years, 2 months ago.

  • Author
    Posts
  • #30952

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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.

The topic ‘Convertto-xml help’ is closed to new replies.