Public/Remove-RSJob.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
Function Remove-RSJob {
    <#
        .SYNOPSIS
            Deletes a Windows PowerShell runspace job.

        .DESCRIPTION
            Deletes a Windows PowerShell background job that has been started using Start-RSJob

        .PARAMETER Name
            The name of the jobs to remove..

        .PARAMETER ID
            The ID of the jobs to remove.

        .PARAMETER InstanceID
            The GUID of the jobs to remove.

        .PARAMETER Batch
            Name of the set of jobs
            
        .PARAMETER Job
            The job object to remove.
            
        .PARAMETER Force
            Force a running job to stop prior to being removed.

        .NOTES
            Name: Remove-RSJob
            Author: Boe Prox

        .EXAMPLE
            Get-RSJob -State Completed | Remove-RSJob

            Description
            -----------
            Deletes all jobs with a State of Completed.

        .EXAMPLE
            Remove-RSJob -ID 1,5,78

            Description
            -----------
            Removes jobs with IDs 1,5,78.
    #>

    [cmdletbinding(
        DefaultParameterSetName='Job',
        SupportsShouldProcess = $True
    )]
    Param (
        [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True,
        ParameterSetName='Name')]
        [string[]]$Name,
        [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True,
        ParameterSetName='Id')]
        [int[]]$Id,
        [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True,
        ParameterSetName='Guid')]
        [guid[]]$InstanceID,
        [parameter(ValueFromPipelineByPropertyName=$True,
        ParameterSetName='Batch')]
        [string[]]$Batch,
        [parameter(ValueFromPipeline=$True,ParameterSetName='Job')]
        [PoshRS.PowerShell.RSJob[]]$Job,
        [parameter()]
        [switch]$Force
    )
    Begin {        
        If ($PSBoundParameters['Debug']) {
            $DebugPreference = 'Continue'
        }      
        $List = New-Object System.Collections.ArrayList
        $StringBuilder = New-Object System.Text.StringBuilder

        #Take care of bound parameters
        If ($PSBoundParameters['Name']) {
            [void]$list.AddRange($Name)
            $Bound = $True
        }
        If ($PSBoundParameters['Batch']) {
            [void]$list.AddRange($Batch)
            $Bound = $True
        }
        If ($PSBoundParameters['Id']) {
            [void]$list.AddRange($Id)
            $Bound = $True
        }
        If ($PSBoundParameters['InstanceId']) {
            [void]$list.AddRange($InstanceId)
            $Bound = $True
        }
        If ($PSBoundParameters['Job']) {
            [void]$list.AddRange($Job)
            $Bound = $True
        }
    }
    Process {
        If (-Not $Bound) {
            [void]$List.Add($_)
        }
    }
    End {
        Write-Debug "ParameterSet: $($PSCmdlet.parametersetname)"
        Switch ($PSCmdlet.parametersetname) {
            'Name' {
                $Items = '"{0}"' -f (($list | ForEach {"^{0}$" -f $_}) -join '|') -replace '\*','.*'
                [void]$StringBuilder.Append("`$_.Name -match $Items") 
                $ScriptBlock = [scriptblock]::Create($StringBuilder.ToString())                    
            }
            'Id' {
                $Items = '"{0}"' -f (($list | ForEach {"^{0}$" -f $_}) -join '|')
                [void]$StringBuilder.Append("`$_.Id -match $Items") 
                $ScriptBlock = [scriptblock]::Create($StringBuilder.ToString())                
            }
            'Guid' {
                $Items = '"{0}"' -f (($list | ForEach {"^{0}$" -f $_}) -join '|')
                [void]$StringBuilder.Append("`$_.InstanceId -match $Items")   
                $ScriptBlock = [scriptblock]::Create($StringBuilder.ToString())   
            }
            'Batch' {
                $Items = '"{0}"' -f (($list | ForEach {"^{0}$" -f $_}) -join '|')
                [void]$StringBuilder.Append("`$_.batch -match $Items")   
                $ScriptBlock = [scriptblock]::Create($StringBuilder.ToString())   
            }     
            Default {$ScriptBlock=$Null}
        }
        If ($ScriptBlock) {
            Write-Verbose "Using ScriptBlock"
            $ToRemove = $PoshRS_jobs | Where $ScriptBlock
        } Else {
            $ToRemove = $List
        }
        [System.Threading.Monitor]::Enter($PoshRS_Jobs.syncroot) 
        $ToRemove | ForEach {
            If ($PSCmdlet.ShouldProcess("Name: $($_.Name), associated with JobID $($_.Id)",'Remove')) {
                If ($_.State -notmatch 'Completed|Failed|Stopped') {
                    If ($PSBoundParameters.ContainsKey('Force')) {
                        [void] $_.InnerJob.Stop()
                        $PoshRS_Jobs.Remove($_)
                    } Else {
                        Write-Error "Unable to remove job $($_.InstanceID)"
                    }
                } Else {
                    [void]$PoshRS_Jobs.Remove($_)
                }
            }
        }
        [System.Threading.Monitor]::Exit($PoshRS_Jobs.syncroot) 
    }
}