Forum Discussion

Baron164's avatar
Baron164
Brass Contributor
Jul 19, 2022

Script is not listing all groups

I'm running this script to export some user data to a csv. It works fine except for the GroupGid portion which is currently only provided up to 8 groups per user. Some user accounts are members of ma...
  • farismalaeb's avatar
    farismalaeb
    Jul 21, 2022

    Baron164 

    Hi

    Yes, You are right, Actually the problem is in the way the object is passed to the export-csv.

    I updated the script to fix this issue and try it on my side with the export.

    This should fix your issue 

    Let me know

    [System.Collections.ArrayList]$fullReport=@()
    $AllUsers=Get-ADUser -Filter "uidNumber -ge 0" -Properties Name,givenName,sn,uidNumber,userPrincipalName 
    $CSVheaderNumber=0
    $CSVIndex=0
    foreach ($singleuser in $AllUsers)
    
    {
        $Report=[PSCustomObject]@{
            Name = $singleuser.Name
            givenName=$singleuser.GivenName
            sn=$singleuser.sn
            uidNumber=$singleuser.uidNumber
            userPrincipalName=$singleuser.userPrincipalName
        }
        $AllGroups=Get-ADPrincipalGroupMembership $singleuser.SamAccountName -Server aud-dc-n2
    
        if ($AllGroups.Count -gt $CSVheaderNumber){ $CSVheaderNumber=$AllGroups.Count;$CSVIndex=$fullReport.Count}
    
        for ($i = 0; $i -lt $AllGroups.name.count; $i++) 
        {
        $GroupGid=Get-ADGroup -Properties gidNumber -Identity $AllGroups[$i].SamAccountName
    
            $Report | Add-Member -NotePropertyName "Group$i" -NotePropertyValue $GroupGid.gidNumber
        }
    
        $fullReport.Add($Report) | Out-Null
    
    }
    $fullReport[$CSVIndex] | Export-Csv -Path C:\Users\f.malaeb\myusers.csv -NoTypeInformation
    $fullReport[0..($CSVIndex -1)+($CSVIndex +1)..$fullReport.count] | Export-Csv -Path C:\Users\f.malaeb\myusers.csv -NoTypeInformation -Append -Force

    If this answer helps, please mark this as Best Response and give a like 🙂

    Thanks

     

Resources