-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemove-CompletedRunspace.ps1
46 lines (40 loc) · 1.73 KB
/
Remove-CompletedRunspace.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
function Remove-CompletedRunspace
{
[CmdletBinding()]
param
(
[System.Collections.Specialized.OrderedDictionary] $AsyncObject,
[int] $MaxRunTime = 15
)
if ($AsyncObject.Count -gt 0)
{
for ($i = 0; $i -lt $AsyncObject.Count; $i++)
{
$endTime = $AsyncObject[$i].StartTime.AddMinutes($MaxRunTime)
$now = Get-Date
#if ($AsyncObject[$i].State.IsCompleted -or $now.CompareTo($endTime))
if ($AsyncObject[$i].State.IsCompleted -or ($now.CompareTo($endTime) -eq $true))
{
if ($AsyncObject[$i].State.IsCompleted)
{
Write-Verbose "Runspace completed, removing task"
}
if ($now.CompareTo($endTime) -eq $true)
{
Write-Verbose "Maximum runtime limit exceeded, this job may have failed. Removing task. Will output any info from stream."
}
if ($AsyncObject[$i].Powershell.Streams.Error.Count -gt 0)
{
Write-Error "An error occured within your task"
Write-Verbose $AsyncObject[$i].Powershell.Streams.Error
}
Write-Output $AsyncObject[$i].Powershell.Streams.Information
$AsyncObject[$i].Powershell.Dispose()
#$threadResponse = $AsyncObject[$i].Powershell.EndInvoke($AsyncObject[$i].State) # if exception is thrown this kills session
#Write-Verbose $threadResponse.ToString()
#Write-Verbose $AsyncObject[$i].Powershell.Streams.Verbose
$AsyncObject.Remove($AsyncObject[$i].Powershell.InstanceId.Guid)
}
}
}
}