-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultithread_commands.ps1
More file actions
94 lines (64 loc) · 2.59 KB
/
multithread_commands.ps1
File metadata and controls
94 lines (64 loc) · 2.59 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
function multithread_commands([string[]]$list) {
# --------------------------------------------------
$threads = 50 # how many simultanious threads. I've tested up to 1000 ok against ~3600 local IPs, ~900 active.
# --------------------------------------------------
$Host.UI.RawUI.CursorPosition = New-Object System.Management.Automation.Host.Coordinates 0 , 1
write-host " Threads: " -nonewline -foregroundcolor yellow
$threads
" Build Pool: "
" Drain Pool: "
" ---------------------"
write-host " Total Files: $($list.count) "
# BLOCK 1: Create and open runspace pool, setup runspaces array with min and max threads
$pool = [RunspaceFactory]::CreateRunspacePool(1, $threads)
$pool.ApartmentState = "MTA"
$pool.Open()
$runspaces = $results = @()
# --------------------------------------------------
# BLOCK 2: Create reusable scriptblock. This is the workhorse of the runspace. Think of it as a function.
$scriptblock = {
Param (
[string]$command
)
$temp = invoke-expression $command
return $temp
}
# --------------------------------------------------
# BLOCK 3: Create runspace and add to runspace pool
$counter=0
foreach ($command in $list) {
$runspace = [PowerShell]::Create()
$null = $runspace.AddScript($scriptblock)
$null = $runspace.AddArgument($command)
$runspace.RunspacePool = $pool
# BLOCK 4: Add runspace to runspaces collection and "start" it
# Asynchronously runs the commands of the PowerShell object pipeline
$runspaces += [PSCustomObject]@{ Pipe = $runspace; Status = $runspace.BeginInvoke() }
$Host.UI.RawUI.CursorPosition = New-Object System.Management.Automation.Host.Coordinates 16 , 2
$counter++
write-host "$counter " -nonewline
}
# --------------------------------------------------
# BLOCK 5: Wait for runspaces to finish
<#
do {
$Host.UI.RawUI.CursorPosition = New-Object System.Management.Automation.Host.Coordinates 5 , 9
$cnt = ($runspaces | Where {$_.Result.IsCompleted -ne $true}).Count
write-host "$cnt "
} while ($cnt -gt 0)
#>
# --------------------------------------------------
$total=$counter
$counter=0
# BLOCK 6: Clean up
foreach ($runspace in $runspaces ) {
# EndInvoke method retrieves the results of the asynchronous call
$results += $runspace.Pipe.EndInvoke($runspace.Status)
$runspace.Pipe.Dispose()
$Host.UI.RawUI.CursorPosition = New-Object System.Management.Automation.Host.Coordinates 16 , 3
$counter++
write-host "$($total-$counter) " -nonewline
}
$pool.Close()
$pool.Dispose()
}