-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunspace_mulithreading.ps1
72 lines (40 loc) · 1.32 KB
/
runspace_mulithreading.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
63
64
65
66
67
68
69
$script_mg ={
param([object]$disk)
$fso = new-object -com Scripting.FileSystemObject
$random = (Get-Random -Minimum 1 -Maximum 10)
Start-Sleep -Seconds $random
return [pscustomobject]@{
process = $disk.name
Handles = $disk.handles
SI = $disk.SI
Random = $random
}
}
[void][runspacefactory]::CreateRunspacePool()
$SessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$RunspacePool = [runspacefactory]::CreateRunspacePool(1,5)
$RunspacePool.CleanupInterval = '00:03:00'
$PowerShell = [powershell]::Create()
$PowerShell.RunspacePool = $RunspacePool
[void]$RunspacePool.Open()
$jobs = new-object system.collections.arraylist
(Get-Process *).count
Get-Process *| select name,handles,si | ForEach {
$PowerShell = [powershell]::Create()
$PowerShell.RunspacePool = $RunspacePool
[void]$PowerShell.AddScript($script_mg)
[void]$PowerShell.AddArgument($_)
$Handle = $PowerShell.BeginInvoke()
$temp ='' | Select PowerShell,Handle
$temp.PowerShell = $PowerShell
$temp.handle = $Handle
[void]$jobs.Add($temp)
}
do {cls; "wating, remaining jobs: $(($jobs.handle.iscompleted).count)"}
while (!($jobs.count))
$return = $jobs | ForEach {
$_.powershell.EndInvoke($_.handle)
$_.PowerShell.Dispose()
}
#$jobs.clear()
$return | ft -a