Advertisement
amnich

Test-Get-ADUser.ps1

Aug 24th, 2017
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $ADUsers = Get-ADUser -Filter *
  2. $ADUsersHT = @{}
  3. [collections.arraylist]$ADUsersArray = @()
  4. foreach ($aduser in $ADUsers){
  5.     $ADUsersHT.Add($aduser.Samaccountname,$aduser)
  6.     $null = $ADUsersArray.Add($aduser)
  7. }
  8. $ADusersCSV = $ADUsers | ConvertTo-CSV | ConvertFrom-Csv
  9. $ADUsersJSON = $AdusersCSv | ConvertTo-Json | ConvertFrom-Json
  10.  
  11.  
  12. $users = @()
  13. for ($i=0;$i -lt 100;$i++){
  14.     $users += $adusers[$(Get-Random -Minimum 0 -Maximum ($ADUsers.Count - 1))].Samaccountname
  15. }
  16.  
  17. $ADCounter = 0
  18. $ADObjectsCounter = 0
  19. $ADObjectsForeachbreakCounter = 0
  20. $ADObjectsForeachCounter = 0
  21. $ADObjectsWhereCounter = 0
  22. $ADObjectsWhereFirstCounter = 0
  23. $CSVCounter = 0
  24. $CSVWhereCounter = 0
  25. $CSVWhereFirstCounter = 0
  26. $JSONCounter = 0
  27. $JSONForeachbreakCounter = 0
  28. $JSONForeachCounter = 0
  29. $ArrayCounter = 0
  30. $HTCounter = 0
  31. $Counter1
  32. $ADEnCounter = 0
  33. $HTEnCounter = 0
  34. $HTEnCounter2 = 0
  35. $JSONEnCounter = 0
  36. $ADEnObjectsCounter = 0
  37.     foreach ($user in $users){
  38.  
  39.         #Write-Output "Get-ADUser"
  40.         $ADCounter += Measure-Command {
  41.             Get-AdUser $user} | select -ExpandProperty TotalMilliSeconds
  42.  
  43.         #Write-Output "Where-Object on original results"
  44.         $ADObjectsCounter += Measure-Command {
  45.             $ADUsers | Where samaccountname -EQ "$user"} | select -ExpandProperty TotalMilliSeconds
  46.         #Write-Output "Foreach method with break on original results"
  47.         $ADObjectsForeachbreakCounter += Measure-Command {         
  48.             foreach ($aduser in $ADUsers){
  49.                 if ($aduser.samaccountname -eq $user){
  50.                     $aduser                
  51.                     break
  52.                 }
  53.             }
  54.         } | select -ExpandProperty TotalMilliSeconds
  55.         #Write-Output "Foreach method on original results"
  56.         $ADObjectsForeachCounter += Measure-Command {          
  57.             foreach ($aduser in $ADUsers){
  58.                 if ($aduser.samaccountname -eq $user){
  59.                     $aduser                                    
  60.                 }
  61.             }
  62.         } | select -ExpandProperty TotalMilliSeconds
  63.         #Write-Output "Where method on original results"
  64.         $ADObjectsWhereCounter += Measure-Command {
  65.             $ADUsers.Where({$_.samaccountname -EQ "$user"})} | select -ExpandProperty TotalMilliSeconds
  66.         #Write-Output "Where method on original results"
  67.         $ADObjectsWhereFirstCounter += Measure-Command {
  68.             $ADUsers.Where({$_.samaccountname -EQ "$user"},'First',1)} | select -ExpandProperty TotalMilliSeconds
  69.  
  70.         #Write-Output "Where-Object on CSV"
  71.         $CSVCounter += Measure-Command {
  72.             $ADUsersCSV | Where samaccountname -EQ "$user"} | select -ExpandProperty TotalMilliSeconds
  73.         #Write-Output "Where method on CSV"
  74.         $CSVWhereCounter += Measure-Command {
  75.             $ADUsersCSV.Where({$_.samaccountname -EQ "$user"})} | select -ExpandProperty TotalMilliSeconds
  76.         #Write-Output "Where method on CSV"
  77.         $CSVWhereFirstCounter += Measure-Command {
  78.             $ADUsersCSV.Where({$_.samaccountname -EQ "$user"},'First',1)} | select -ExpandProperty TotalMilliSeconds
  79.  
  80.         #Write-Output "Where-Object from JSON"
  81.         $JSONCounter += Measure-Command {
  82.             $ADUsersJSON | Where samaccountname -EQ "$user"} | select -ExpandProperty TotalMilliSeconds
  83.         #Write-Output "Foreach with break on JSON"
  84.         $JSONForeachbreakCounter += Measure-Command {
  85.             foreach ($aduser in $ADUsersJSON){
  86.                 if ($aduser.samaccountname -eq $user){
  87.                     $aduser                
  88.                     break
  89.                 }
  90.             }
  91.         } | select -ExpandProperty TotalMilliSeconds
  92.         #Write-Output "Foreach on JSON"
  93.         $JSONForeachCounter += Measure-Command {
  94.             foreach ($aduser in $ADUsersJSON){
  95.                 if ($aduser.samaccountname -eq $user){
  96.                     $aduser                                    
  97.                 }
  98.             }
  99.         } | select -ExpandProperty TotalMilliSeconds
  100.         #Write-Output "Where-Object on ArrayList"
  101.         $ArrayCounter += Measure-Command {
  102.             $ADusersArray | Where samaccountname -EQ "$user"} | select -ExpandProperty TotalMilliSeconds
  103.  
  104.         #Write-Output "$(($ADusersHT.gettype()).name)"
  105.         $HTCounter  += Measure-Command {
  106.             $ADUsersHT["$user"]} | select -ExpandProperty TotalMilliSeconds
  107.        
  108.         $ADEnObjectsCounter += (measure-command {$ADUsers | where {$_.samaccountname -eq $user -and $_.enabled -eq $true}}).TotalMilliSeconds
  109.         $JSONEnCounter += (measure-command {foreach ($aduser in $ADUsersJSON){
  110.                 if ($aduser.samaccountname -eq $user -and $aduser.Enabled -eq $true){
  111.                     $aduser.Enabled                
  112.                     break
  113.                 }
  114.             }}).TotalMilliSeconds      
  115.         $HTEnCounter2 += (measure-command {($ADUsersHT[$user]).enabled -eq $true}).TotalMilliSeconds
  116.         $ADEnCounter +=(measure-command {(Get-ADUser $user).enabled -eq $true}).TotalMilliSeconds
  117.     }
  118.  
  119.  
  120. " ======= "
  121. "ADCounter $ADCounter"
  122. "ADObjectsCounter $ADObjectsCounter"
  123. "ADObjectsWhereCounter $ADObjectsWhereCounter"
  124. "ADObjectsWhereFirstCounter $ADObjectsWhereFirstCounter"
  125. "ADObjectsForeachCounter $ADObjectsForeachCounter"
  126. "ADObjectsForeachbreakCounter $ADObjectsForeachbreakCounter"
  127. "CSVCounter $CSVCounter"
  128. "CSVWhereCounter $CSVWhereCounter"
  129. "CSVWhereFirstCounter $CSVWhereFirstCounter"
  130. "JSONCounter $JSONCounter"
  131. "JSONForeachbreakCounter $JSONForeachbreakCounter"
  132. "JSONForeachCounter $JSONForeachCounter"
  133. "ArrayCounter $ArrayCounter"
  134. "HTCounter $HTCounter"
  135. "=="
  136. "ADEnCounter $ADEnCounter"
  137. "ADEnObjects $ADEnObjectsCounter"
  138. "HTEnCounter $HTEnCounter"
  139. "HTEnCounter2 $HTEnCounter2"
  140. "JSONEnCounter $JSONEnCounter"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement