Forum Discussion
Script is not listing all groups
- Jul 21, 2022
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
farismalaeb So I think my issue is related to the export-csv portion. If I change "$AllGroups.name.count" to a set number like 50, I get up to 50 group columns. However, if I do that, then the last group for a user gets listed multiple times. Here is an example from my last run.
"Doe","Jane","JDoe","102138","jdoe@domain.local","10002","1216","1349","1920","1901","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902","1902"
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