Originally I ran into this while trying to combine AD groups and user members into an array of objects to process and export into a csv file but the following script fragment does the same thing. I am using Powershell ISE Versiion 3
Clear-Host
$Groups = 'Group1', 'Group2'
$Users = 'User1', 'User2'
[array]$GroupUsers=$null
$OneGroupUsers = New-Object PSCustomObject
Add-Member -InputObject $OneGroupUsers -MemberType NoteProperty -Name OneGroup -Value ""
Add-Member -InputObject $OneGroupUsers -MemberType NoteProperty -Name OneUser -Value ""
$Groups | ForEach-Object {
$OneGroup = $_ #.SamAccountName
###### Get-ADGroupMember -Identity $OneGroup -Recursive
$Users | ForEach-Object {
$OneUser = $_ #.SamAccountName
$OneGroupUsers.OneGroup =$OneGroup
$OneGroupUsers.OneUser = $OneUser
$GroupUsers += $OneGroupUsers
Write-Host "Inside the loops I seem to have the right data $($GroupUsers[-1]) "
} # End ForEach-Object
}# End ForEach-Object
Write-Host " what happened to the data once I exit the loops."
$GroupUsers.count
$GroupUsers
The results
Inside the loops I seem to have the right data @{OneGroup=Group1; OneUser=User1}
Inside the loops I seem to have the right data @{OneGroup=Group1; OneUser=User2}
Inside the loops I seem to have the right data @{OneGroup=Group2; OneUser=User1}
Inside the loops I seem to have the right data @{OneGroup=Group2; OneUser=User2}
what happened to the data once I exit the loops.
4
OneGroup OneUser
——– ——-
Group2 User2
Group2 User2
Group2 User2
Group2 User2
What am I doing wrong??