forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-runspace.ps1
More file actions
58 lines (46 loc) · 1.8 KB
/
remove-runspace.ps1
File metadata and controls
58 lines (46 loc) · 1.8 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
Function Remove-Runspace {
[CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = "id")]
[Outputtype("None")]
Param
(
[Parameter(Mandatory, Position = 0, ParameterSetName = "id")]
[ValidateNotNullorEmpty()]
[int32]$ID,
[Parameter(Mandatory, ValueFromPipeline, Position = 0, ParameterSetName = "runspace")]
[System.Management.Automation.Runspaces.Runspace]$Runspace
)
Begin {
Write-Verbose "Starting $($MyInvocation.Mycommand)"
} #begin
Process {
if ($id) {
Write-Verbose "Getting runspace ID $id"
$runspace = Get-Runspace -id $id
if (-not $Runspace) {
Throw "Failed to find a runspace with an id of $id"
}
}
Write-Verbose "Removing runspace $($runspace.id)"
if ($PSCmdlet.ShouldProcess("$($Runspace.id) - $($Runspace.name)")) {
if ($Runspace.RunspaceStateInfo -eq "closing" -OR $runspace.RunspaceAvailability -eq "busy") {
Write-Warning "Can't remove this runspace in its current state"
Write-Warning ($runspace | Out-String)
}
else {
$Runspace.close()
$Runspace.dispose()
}
}
} #process
End {
Write-Verbose "Ending $($MyInvocation.Mycommand)"
} #end
} #close function
Register-ArgumentCompleter -CommandName Remove-Runspace -ParameterName Runspace -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
(Get-Runspace).where( {$_.runspaceavailability -ne 'busy'}).name|
foreach-object {
# completion text,listitem text,result type,Tooltip
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}