powershell

PowerShell script returns different results when executing more than a single query


I have two separate queries against an xml file, when I run them separately they work fine. However when I run them both in the same script, the output of the second query isn't correct.

The input file looks like this:

<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<Safex>
    <Add>
        <User folder="/" name="D2\\FRED@D2.com">
            <GroupMembership>ADMINS</GroupMembership>
            <GroupMembership>WebService</GroupMembership>
        </User>
        <User folder="/" name="D2\\WILMA@D2.com">
            <GroupMembership>WebService</GroupMembership>
        </User>
        <User folder="/" name="D2\\BARNEY@D2.com">
            <GroupMembership>WebService</GroupMembership>
        </User>
        <User folder="/" name="D2\\BETTY@D2.com">
            <GroupMembership>ADMINS</GroupMembership>
        </User>
        <User folder="/" name="D2\\PEBBLES@D2.com">
            <GroupMembership>WebService</GroupMembership>
        </User>
    </Add>
</Safex>

My script is as follows:

[xml]$xml = Get-Content $InputPath\test002.xml 

$a = $xml.Safex.Add.User | ForEach-Object {
    [PSCustomObject]@{
        'Name' = $_.name
        'Group' = $_.GroupMembership
    }
}
$a 


$b = $xml.Safex.Add.User | Where-Object { $_.GroupMembership -eq 'WebService' } | 
                           Select-Object @{Name = 'Name'; Expression = {$_.Name}}, GroupMembership

#$b

When I have $b commented out, then $a returns this:

Name               Group               
----               -----               
D2\\FRED@D2.com    {ADMINS, WebService}
D2\\WILMA@D2.com   WebService          
D2\\BARNEY@D2.com  WebService          
D2\\BETTY@D2.com   ADMINS              
D2\\PEBBLES@D2.com WebService 

When I have $a commented out, then $b returns this:

Name               GroupMembership     
----               ---------------     
D2\\FRED@D2.com    {ADMINS, WebService}
D2\\WILMA@D2.com   WebService          
D2\\BARNEY@D2.com  WebService          
D2\\PEBBLES@D2.com WebService 

When I have both $a and $b uncommented then I get this:

Name               Group               
----               -----               
D2\\FRED@D2.com    {ADMINS, WebService}
D2\\WILMA@D2.com   WebService          
D2\\BARNEY@D2.com  WebService          
D2\\BETTY@D2.com   ADMINS              
D2\\PEBBLES@D2.com WebService          
D2\\FRED@D2.com                        
D2\\WILMA@D2.com                       
D2\\BARNEY@D2.com                      
D2\\PEBBLES@D2.com  

I can't seem to figure out why when both $a and $b are uncommented, that the heading for $b doesn't appear, and the GroupMembership values don't appear.

Appreciate any assistance you can offer.


Solution

  • Use the same property names (same headers) in both array of objects ($a and $b) then your code works properly. Otherwise you will have a similar issue as the one explained in PowerShell Export-CSV - Missing Columns.

    $a = $xml.Safex.Add.User | ForEach-Object {
        [PSCustomObject]@{
            'Name'  = $_.name
            'Group' = $_.GroupMembership
        }
    }
    
    $a
    
    
    $b = $xml.Safex.Add.User | Where-Object { $_.GroupMembership -eq 'WebService' } |
        Select-Object Name, @{ N = 'Group'; E = 'GroupMembership' }
    
    $b