Question

I'm running a script that first gets all Groups and then gets all members of those collected groups and put those in a .csv.

I'm now waiting for more than 3 hours already and it's not even half way there...

Below is the script.

Get-QADGroup -SizeLimit 0 -properties * | 
Select samaccountname| 
Export-CSV D:\Test_PowerShell\group.csv -NoTypeInformation

Get-Content "D:\Test_PowerShell\group.csv" | 
foreach-object {$group = $_ -replace '"', ""# grab current group name get-qadgroupmember $group -sizelimit 0 -indirect | 
`select-object samaccountname,@{n="GroupName";e={$group}}} | 
export-csv D:\Test_PowerShell\groupandmem.csv -notypeinformation
Was it helpful?

Solution

try this:

( Get-QADGroup -SizeLimit 0 ) | 
foreach-object {                    
                 $group = $_.samaccountname
                 get-qadgroupmember $_.samaccountname -sizelimit 0 -indirect | 
                 select-object samaccountname, @{n="GroupName";e={$group}}
               }

If you are in powershell version 3.0/4.0 you can try using a powershell workflow (I have not done any extensive testing about the performance but theoretically can at least double the speed):

workflow get-UserAndGroup 
{
   $a = inlinescript {   add-pssnapin Quest.ActiveRoles.ADManagement
                        Get-QADGroup -SizeLimit 0 | select -expa samaccountname
                    }                           

   foreach -parallel ( $group in $a ) 
   {                    
     inlinescript {
                    add-pssnapin Quest.ActiveRoles.ADManagement
                    Get-qadgroupmember $using:group -sizelimit 0 -indirect | 
                    select-object samaccountname, @{n="GroupName";e={$using:group}}
                  } 
    } 
}

use like this:

 get-UserAndGroup | select samaccountname, groupname | export-csv .\myfile.csv -notypeinformation

My performance test ( with 30 groups containing from 5 to 50 users ):

PS C:\ps> workflow get-UserAndGroup 
{
   $a = inlinescript {   add-pssnapin Quest.ActiveRoles.ADManagement
                        Get-QADGroup  -SizeLimit 0 | select -expa samaccountname
                    }



   foreach -parallel ( $group in $a ) 
   {                    
     inlinescript {
                    add-pssnapin Quest.ActiveRoles.ADManagement
                    Get-qadgroupmember $using:group -sizelimit 0 -indirect | 
                    select-object samaccountname, @{n="GroupName";e={$using:group}}
                  } 
    } 
}

PS C:\ps> Measure-Command { get-UserAndGroup }


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 23
Milliseconds      : 512
Ticks             : 235120228
TotalDays         : 0,000272129893518519
TotalHours        : 0,00653111744444444
TotalMinutes      : 0,391867046666667
TotalSeconds      : 23,5120228
TotalMilliseconds : 23512,0228


PS C:\ps> measure-command {
( Get-QADGroup -SizeLimit 0 ) | 
foreach-object {                    
                 $group = $_.samaccountname
                 get-qadgroupmember $_.samaccountname -sizelimit 0 -indirect | 
                 select-object samaccountname, @{n="GroupName";e={$group}}
               }}


Days              : 0
Hours             : 0
Minutes           : 1
Seconds           : 7
Milliseconds      : 16
Ticks             : 670161451
TotalDays         : 0,000775649827546296
TotalHours        : 0,0186155958611111
TotalMinutes      : 1,11693575166667
TotalSeconds      : 67,0161451
TotalMilliseconds : 67016,1451
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top