Advertisement
Guest User

Untitled

a guest
May 25th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. Measure-Command{
  2. #MyList is the imported CSV containing the users
  3. #MaxThread is the maxing allowed threads that can be run at one time
  4. #SleepTimer is the timer used to check threads before continueing
  5. Param($mylist = (Import-Csv -Path 'D:\Folder\FileName.csv'),
  6. $MaxThreads = 5,
  7. $SleepTimer = 5)
  8.  
  9.  
  10. #This will clear any jobs current on the system
  11. "Killing existing jobs . . ."
  12. Get-Job | Remove-Job -Force
  13. "Done."
  14.  
  15.  
  16. #All Variables requiring a count value
  17. $usertotal = $mylist.Count
  18. $gcount = $gclist.Count
  19. $stop = $mylist.count
  20.  
  21.  
  22. #Empty Array
  23. $myBucket = @()
  24.  
  25.  
  26. #Zeroing of variables
  27. $tracker = 0
  28. $gcindex = 0
  29. $jobCount = 0
  30. $CurrentThreadCreated = 0
  31. $myIndex = 0
  32.  
  33. #GC section
  34. $gclist = @("gc1:3268","gc2:3268","gc3:3268")
  35. $GCone = "gc1:3268"
  36.  
  37.  
  38. foreach ($listitem in $mylist )
  39. {
  40. #Building the array, until there is 100 users before proceeding
  41. $myBucket += $listitem
  42.  
  43.  
  44. #Incrementing the Tracker and MyIndex
  45. $tracker ++
  46. $myIndex ++
  47.  
  48.  
  49. if ($tracker -eq 100 )
  50. {
  51. #This is used to confirm the current job count against the max jobs allowed
  52. While ($(Get-Job -state running -Verbose).count -ge $MaxThreads)
  53. {
  54.  
  55. Write-Progress -Activity "Creating User List" -Status "Waiting for threads to close" -CurrentOperation "$CurrentThreadCreated threads created - $($(Get-Job -state running).count) threads open" -PercentComplete ($CurrentThreadCreated / $mylist.count * 100)
  56.  
  57.  
  58. Start-Sleep -Milliseconds $SleepTimer
  59. }#While
  60.  
  61.  
  62. $CurrentThreadCreated ++
  63. $jobCount ++
  64.  
  65. #Traker is given the value of 0 so that it can rebuild the array
  66. $tracker = 0
  67.  
  68.  
  69. #As the index changes, so does the GC that is used in the search
  70. $sendthisDC = $gclist[$gcindex]
  71.  
  72.  
  73. write-host $sendthisDC $myBucket
  74.  
  75.  
  76. Start-Job -Name JobNumber$Jobcount -ArgumentList $myBucket, $sendthisDC -ScriptBlock{
  77.  
  78. foreach($User in $args[0])
  79. {
  80.  
  81.  
  82. $Decode = [system.convert]::frombase64string($User.ImmutableId)
  83. $GUID = [GUID]$Decode
  84.  
  85. Get-ADUser -Identity $GUID -Server $args[1] -Properties UserPrincipalName, DistinguishedName
  86.  
  87.  
  88. }#ForEach
  89.  
  90. }#ScriptBlock
  91.  
  92. Write-Progress -Activity "Creating User List" -Status "Waiting for threads to close" -CurrentOperation "$CurrentThreadCreated threads created - $($(Get-Job -state running).count) threads open" -PercentComplete ($CurrentThreadCreated / $mylist.count * 100)
  93.  
  94.  
  95. If ($gcindex -lt 2) { $gcindex ++ } else { $gcindex = 0 }
  96.  
  97. $myBucket = @()
  98.  
  99.  
  100. }#If Tracker
  101.  
  102.  
  103. elseif (($tracker -lt 100 ) -and ( $myIndex -eq $stop))
  104. {
  105.  
  106. While ($(Get-Job -state running -Verbose).count -ge $MaxThreads)
  107. {
  108.  
  109. Write-Progress -Activity "Creating User List" -Status "Waiting for threads to close" -CurrentOperation "$CurrentThreadCreated threads created - $($(Get-Job -state running).count) threads open" -PercentComplete ($CurrentThreadCreated / $mylist.count * 100)
  110.  
  111.  
  112. Start-Sleep -Milliseconds $SleepTimer
  113. }
  114.  
  115.  
  116. $jobCount ++
  117. Start-Job -Name JobNumber$Jobcount -ArgumentList $myBucket,$GCone -ScriptBlock{
  118.  
  119. foreach($User in $args[0])
  120. {
  121.  
  122.  
  123. $Decode = [system.convert]::frombase64string($User.ImmutableId)
  124. $GUID = [GUID]$Decode
  125.  
  126. Get-ADUser -Identity $GUID -Server $args[1] -Properties UserPrincipalName, DistinguishedName
  127.  
  128.  
  129. }#ForEach
  130.  
  131. }#ScriptBlock
  132.  
  133.  
  134. }#ElseIf
  135.  
  136.  
  137. }
  138.  
  139.  
  140.  
  141.  
  142. While(Get-Job -State Running)
  143. {
  144.  
  145.  
  146. Start-Sleep -Milliseconds $SleepTimer
  147.  
  148.  
  149. }
  150.  
  151.  
  152. #All jobs are saved into a variable and then exported to a file
  153. $jobs = get-job * | where { $_.Name -like "JobNumber*"}
  154.  
  155.  
  156. foreach ( $singlejob in $jobs ) {
  157.  
  158.  
  159. get-job -Id $singlejob.Id | receive-job |select UserPrincipalName, DistinguishedName | Export-Csv -NoTypeInformation -Delimiter ";" -Path 'D:\folder\file.csv' -Append
  160.  
  161.  
  162. }
  163.  
  164.  
  165. }#Measure
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement