I have a powershell script that is trying to get every AD group and their members. Since my real code is running a Get-ADUser on every user in every group, I am using parallel loops to save a good amount of time (side note: after testing I have found that using multiple Get-ADUser commands is typically faster than Get-ADGroupMember). However, I have noticed that I cannot view the members of a group when running a parallel loop. I have written some basic code for testing:
$Groups = Get-ADGroup -Filter * -Properties Created,Modified,Description,Members | select-object -first 50
# Loop A
$Groups | foreach-object {
$psitem.Members
}
# Loop B
$Groups | foreach-object -parallel {
$psitem.Members
}
For the test code above I can verify that $Groups
does indeed have the Members property. They gettype() output is below:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False ADPropertyValueCollection System.Collections.CollectionBase
Loop A above prints every group member as expected, however Loop B always returns nothing. Does anybody know why this may be? I would like to use the double parallel loops if possible, just to save a lot of time as this script will be running periodically.
My PS version is 7.2.7
As stated in the comments, using $PSItem['Members']
does the trick. See GitHub Issue #14604 for details.
$Groups = Get-ADGroup -Filter * -Properties Created,Modified,Description,Members | select-object -first 50
$A = $Groups | foreach-object {
$psitem.Members
}
$B = $Groups | foreach-object -parallel {
$PSItem['Members']
}
echo "A: $($A.count) ----- B: $($B.count)"