-
Notifications
You must be signed in to change notification settings - Fork 1
/
Invoke-MultithreadedOperation.ps1
62 lines (51 loc) · 1.93 KB
/
Invoke-MultithreadedOperation.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Function new-OuNames {
[CmdletBinding()]
Param($NumberOfOus)
$OuArray = @()
1..$NumberOfOus | ForEach-Object -proces {$OuArray += "OU$($_)"}
$OuArray
}
$ScriptToRun = {
Param($OuString)
$SleepSeconds= Get-Random -Minimum 1 -Maximum 5
Start-Sleep $SleepSeconds
[PsCustomObject]@{
ThreadID = [appdomain]::GetCurrentThreadId()
SleepSeconds = $SleepSeconds
Ou = $OuString
}
}
$ThreadCount = 100
$ScriptStart = Get-Date
$SessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$RunspacePool = [runspacefactory]::CreateRunspacePool(1, $ThreadCount)
$RunspacePool.Open()
$Jobs = [System.Collections.ArrayList]::new()
$OUs = new-OuNames -NumberOfOus 30
#region create threads and beging Invoke
$PrepStart = Get-date
Foreach ($Ou in $Ous ) {
$PowerShell = [powershell]::Create($SessionState)
$PowerShell.RunspacePool = $RunspacePool
$Powershell.AddScript($ScriptToRun) | Out-Null
$PowerShell.AddParameters(@{OuString =$Ou}) | Out-Null
$JobObject = [PsCustomObject]@{
Runspace = $PowerShell.BeginInvoke()
Powershell = $PowerShell
}
$Jobs.Add($JobObject) | Out-Null
}
#endregion
#region wait job execution finish
while ($Jobs.Runspace.Iscompleted -contains $false) {
$PercentProgress = [math]::Round(($Jobs.Runspace.IsCompleted | where-object {$_ -eq $true}).Count / $Jobs.Runspace.IsCompleted.Count * 100)
Write-Progress -activity "Collecting OU Acls" -PercentComplete $PercentProgress -Status $PercentProgress
}
$Result = foreach ($Job in $Jobs) {
$Job.Powershell.EndInvoke($Job.Runspace)
}
$ThreadTotalRunDuration =($Result.SleepSeconds | Measure-Object -Sum).Sum
$Result
$ScriptDuration = [Math]::Round(((Get-Date) - $ScriptStart).TotalSeconds)
Write-Host -ForegroundColor Cyan "Script ended in $ScriptDuration seconds."
Write-Host -ForegroundColor Blue "Threads spend total time: $ThreadTotalRunDuration seconds."