locked
write-output pipe to export-csv problems RRS feed

  • Question

  • I am trying to write the powershell way, but am getting headaches.  Rather than saving my output in an array, I'm trying to drop it into the pipeline and then export it to csv.  The problem is that the export csv has the object names instead of the values.  I've looked at other technet articles that fix this with select-object -ExpandProperty, but that only works for one property.  Should I give up and go to an array?

    function Get-GroupAudit {
        param(
            [string[]] $GroupList
        )
        foreach ($group in $GroupList) {
          $members = Get-ADGroupMember $group | Get-ADUser -Properties * | Select-Object ` SamAccountName,Name,Enabled,Description,DistinguishedName 
           $props = @{'SamAccountName'=$members.SamAccountName;
                              'Name'=$members.Name;
                              'Enabled'=$members.Enabled;
                              'Description'=$members.Description;
                              'DistinguishedName'=$members.DistinguishedName}
           $obj = New-Object -TypeName psobject -Property $props
           Write-Output $obj
       } 
    }

       Get-GroupAudit -GroupList "Domain Admins" | Export-Csv test.csv

    results:

    #TYPE System.Management.Automation.PSCustomObject
    "DistinguishedName","Enabled","Name","Description","SamAccountName"
    "System.Object[]","System.Object[]","System.Object[]","System.Object[]","System.Object[]"

    Get-GroupAudit -GroupList "Domain Admins"

    results:

    DistinguishedName : {CN=user1,CN=Users,DC=test,DC=edu, CN=another user...}
    Enabled           : {True, True, True, True...}
    Name              : {user1, anotheruser...}
    Description       : {Built-in account for administering the computer/domain, another description...}
    SamAccountName    : {user1, anotheruser...}

    Get-GroupAudit -GroupList "Domain Admins" | Get-Member

       TypeName: System.Management.Automation.PSCustomObject

    Name              MemberType   Definition                                       
    ----              ----------   ----------                                       
    Equals            Method       bool Equals(System.Object obj)                   
    GetHashCode       Method       int GetHashCode()                                
    GetType           Method       type GetType()                                   
    ToString          Method       string ToString()                                
    Description       NoteProperty System.Object[] Description=System.Object[]      
    DistinguishedName NoteProperty System.Object[] DistinguishedName=System.Object[]
    Enabled           NoteProperty System.Object[] Enabled=System.Object[]          
    Name              NoteProperty System.Object[] Name=System.Object[]             
    SamAccountName    NoteProperty System.Object[] SamAccountName=System.Object[]   

       

    John

    Wednesday, August 12, 2015 5:05 PM

Answers

  • Hi,

    Try it this way:

    function Get-GroupAudit {
    
        param ([string[]]$GroupList)
    
        foreach ($group in $GroupList) {
    
            Get-ADGroupMember -Identity $group -Recursive | Where { $_.ObjectClass -eq 'user' } |
                Get-ADUser -Properties Description |
                    Select @{N='GroupName';E={$group}},SamAccountName,Name,Enabled,Description,DistinguishedName
    
        }
    
    }
    
    Get-GroupAudit -GroupList 'Test Group 1','Test Group 2' | Export-Csv .\groupList.csv -NoTypeInformation


    • Proposed as answer by Wizend Wednesday, August 12, 2015 6:04 PM
    • Marked as answer by John York Wednesday, August 12, 2015 7:14 PM
    Wednesday, August 12, 2015 5:49 PM

All replies

  • Hi,

    Try it this way:

    function Get-GroupAudit {
    
        param ([string[]]$GroupList)
    
        foreach ($group in $GroupList) {
    
            Get-ADGroupMember -Identity $group -Recursive | Where { $_.ObjectClass -eq 'user' } |
                Get-ADUser -Properties Description |
                    Select @{N='GroupName';E={$group}},SamAccountName,Name,Enabled,Description,DistinguishedName
    
        }
    
    }
    
    Get-GroupAudit -GroupList 'Test Group 1','Test Group 2' | Export-Csv .\groupList.csv -NoTypeInformation


    • Proposed as answer by Wizend Wednesday, August 12, 2015 6:04 PM
    • Marked as answer by John York Wednesday, August 12, 2015 7:14 PM
    Wednesday, August 12, 2015 5:49 PM
  • Argh.  Spent half a day working on this, and just realized my $obj is an object of objects of objects.  I needed to use nested foreach loops to unwrap it.  I doubt that's the powershell way, and would be interested in seeing a better way, but this works:

    Import-Module ActiveDirectory
    function Get-GroupAudit {
        param(
            [string[]] $GroupList
        )
        foreach ($group in $GroupList) {   
          $memberList = Get-ADGroupMember $group      
          foreach ($member in $memberList) { 
              $memberData = Get-ADUser $member -Properties * | Select-Object SamAccountName,Name,Enabled,Description,DistinguishedName 
              $props = @{'SamAccountname'=$memberData.SamAccountname;
                         'Name'=$memberData.Name;
                         'Enabled'=$memberData.Enabled;
                         'Description'=$memberData.Description;
                         'distinguishedName'=$memberData.DistinguishedName;
                         }
               $obj = New-Object -TypeName psobject -Property $props
               Write-Output $obj
           }
        } 
    }
    
    Get-GroupAudit -GroupList "Domain Admins" | Export-Csv -NoTypeInformation test.csv


    John

    Wednesday, August 12, 2015 7:09 PM
  • Yep, that looks more like the PowerShell way, and it even works!  Between being a not-so-good coder and many years of kluging things together, I often get a headache when I try to do it the right way...

    John

    Wednesday, August 12, 2015 7:21 PM