Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Measure-Command{
- #MyList is the imported CSV containing the users
- #MaxThread is the maxing allowed threads that can be run at one time
- #SleepTimer is the timer used to check threads before continueing
- Param($mylist = (Import-Csv -Path 'D:\Folder\FileName.csv'),
- $MaxThreads = 5,
- $SleepTimer = 5)
- #This will clear any jobs current on the system
- "Killing existing jobs . . ."
- Get-Job | Remove-Job -Force
- "Done."
- #All Variables requiring a count value
- $usertotal = $mylist.Count
- $gcount = $gclist.Count
- $stop = $mylist.count
- #Empty Array
- $myBucket = @()
- #Zeroing of variables
- $tracker = 0
- $gcindex = 0
- $jobCount = 0
- $CurrentThreadCreated = 0
- $myIndex = 0
- #GC section
- $gclist = @("gc1:3268","gc2:3268","gc3:3268")
- $GCone = "gc1:3268"
- foreach ($listitem in $mylist )
- {
- #Building the array, until there is 100 users before proceeding
- $myBucket += $listitem
- #Incrementing the Tracker and MyIndex
- $tracker ++
- $myIndex ++
- if ($tracker -eq 100 )
- {
- #This is used to confirm the current job count against the max jobs allowed
- While ($(Get-Job -state running -Verbose).count -ge $MaxThreads)
- {
- 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)
- Start-Sleep -Milliseconds $SleepTimer
- }#While
- $CurrentThreadCreated ++
- $jobCount ++
- #Traker is given the value of 0 so that it can rebuild the array
- $tracker = 0
- #As the index changes, so does the GC that is used in the search
- $sendthisDC = $gclist[$gcindex]
- write-host $sendthisDC $myBucket
- Start-Job -Name JobNumber$Jobcount -ArgumentList $myBucket, $sendthisDC -ScriptBlock{
- foreach($User in $args[0])
- {
- $Decode = [system.convert]::frombase64string($User.ImmutableId)
- $GUID = [GUID]$Decode
- Get-ADUser -Identity $GUID -Server $args[1] -Properties UserPrincipalName, DistinguishedName
- }#ForEach
- }#ScriptBlock
- 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)
- If ($gcindex -lt 2) { $gcindex ++ } else { $gcindex = 0 }
- $myBucket = @()
- }#If Tracker
- elseif (($tracker -lt 100 ) -and ( $myIndex -eq $stop))
- {
- While ($(Get-Job -state running -Verbose).count -ge $MaxThreads)
- {
- 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)
- Start-Sleep -Milliseconds $SleepTimer
- }
- $jobCount ++
- Start-Job -Name JobNumber$Jobcount -ArgumentList $myBucket,$GCone -ScriptBlock{
- foreach($User in $args[0])
- {
- $Decode = [system.convert]::frombase64string($User.ImmutableId)
- $GUID = [GUID]$Decode
- Get-ADUser -Identity $GUID -Server $args[1] -Properties UserPrincipalName, DistinguishedName
- }#ForEach
- }#ScriptBlock
- }#ElseIf
- }
- While(Get-Job -State Running)
- {
- Start-Sleep -Milliseconds $SleepTimer
- }
- #All jobs are saved into a variable and then exported to a file
- $jobs = get-job * | where { $_.Name -like "JobNumber*"}
- foreach ( $singlejob in $jobs ) {
- get-job -Id $singlejob.Id | receive-job |select UserPrincipalName, DistinguishedName | Export-Csv -NoTypeInformation -Delimiter ";" -Path 'D:\folder\file.csv' -Append
- }
- }#Measure
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement