Public/Get-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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
Function Get-RSJob {
    <#
        .SYNOPSIS
            Gets runspace jobs that are currently available in the session.

        .DESCRIPTION
            Get-RSJob will display all jobs that are currently available to include completed and currently running jobs.
            If no parameters are given, all jobs are displayed to view.

        .PARAMETER Name
            The name of the jobs to query for.

        .PARAMETER ID
            The ID of the jobs that you want to display.

        .PARAMETER InstanceID
            The GUID of the jobs that you want to display.

        .PARAMETER State
            The State of the job that you want to display. Accepted values are:

            NotStarted
            Running
            Completed
            Failed
            Stopping
            Stopped
            Disconnected

        .PARAMETER Batch
            Name of the set of jobs

        .PARAMETER HasMoreData
            Displays jobs that have data being outputted. You can specify -HasMoreData:$False to display jobs
            that have no data to output.

        .NOTES
            Name: Get-RSJob
            Author: Boe Prox

        .EXAMPLE
            Get-RSJob -State Completed

            Description
            -----------
            Displays a list of jobs which have completed.

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

            Id Name State HasMoreData HasErrors Command
            -- ---- ----- ----------- --------- -------
            1 Test_1 Completed True False ...
            5 Test_5 Completed True False ...
            78 Test_78 Completed True False ...

            Description
            -----------
            Displays list of jobs with IDs 1,5,78.
    #>

    [OutputType('RSJob')]
    [cmdletbinding(
        DefaultParameterSetName='All'
    )]
    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(ParameterSetName='Batch')]
        [parameter(ParameterSetName='Name')]
        [parameter(ParameterSetName='Id')]
        [parameter(ParameterSetName='Guid')]
        [parameter(ParameterSetName='All')]
        [ValidateSet('NotStarted','Running','Completed','Failed','Stopping','Stopped','Disconnected')]
        [string[]]$State,
        [parameter(ValueFromPipelineByPropertyName=$True,
        ParameterSetName='Batch')]
        [string[]]$Batch,
        [parameter(ParameterSetName='Batch')]
        [parameter(ParameterSetName='Name')]
        [parameter(ParameterSetName='Id')]
        [parameter(ParameterSetName='Guid')]
        [parameter(ParameterSetName='All')]
        [Switch]$HasMoreData,
        [parameter(ValueFromPipeline=$True,ParameterSetName='Job')]
        [RSJob[]]$Job        
    )
    Begin {
        If ($PSBoundParameters['Debug']) {
            $DebugPreference = 'Continue'
        }        
        Write-Debug "ParameterSet: $($PSCmdlet.parametersetname)"
        $List = New-Object System.Collections.ArrayList
        $WhereList = New-Object System.Collections.ArrayList
        
        #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 ($PSCmdlet.ParameterSetName -ne 'All' -AND -NOT $Bound) {
            Write-Verbose "Adding $($_)"
            [void]$List.Add($_)
        }
    }
    End {        
        Switch ($PSCmdlet.parametersetname) {
            'Name' {
                $Items = '"{0}"' -f (($list | ForEach {"^{0}$" -f $_}) -join '|') -replace '\*','.*'
                [void]$WhereList.Add("`$_.Name -match $Items")                    
            }
            'Id' {
                $Items = '"{0}"' -f (($list | ForEach {"^{0}$" -f $_}) -join '|')
                [void]$WhereList.Add("`$_.Id -match $Items")                
            }
            'Guid' {
                $Items = '"{0}"' -f (($list | ForEach {"^{0}$" -f $_}) -join '|')
                [void]$WhereList.Add("`$_.InstanceId -match $Items")  
            }
            'Job' {
                $Items = '"{0}"' -f (($list | ForEach {"^{0}$" -f $_.Id}) -join '|')
                [void]$WhereList.Add("`$_.id -match $Items")
            }   
            'Batch' {
                $Items = '"{0}"' -f (($list | ForEach {"^{0}$" -f $_}) -join '|')
                [void]$WhereList.Add("`$_.batch -match $Items")
            }                      
        }
        If ($PSBoundParameters['State']) {
            [void]$WhereList.Add("`$_.State -match `"$($State -join '|')`"")
        }
        If ($PSBoundParameters.ContainsKey('HasMoreData')) {
            [void]$WhereList.Add("`$_.HasMoreData -eq `$$HasMoreData")
        }
        Write-Debug "WhereListCount: $($WhereList.count)"
        If ($WhereList.count -gt 0) {
            $WhereString = $WhereList -join ' -AND '
            $WhereBlock = [scriptblock]::Create($WhereString)
        }               
        If ($WhereBlock) {
            Write-Debug "WhereString: $($WhereString)" 
            Write-Verbose "Using scriptblock"
            $PoshRS_Jobs | Where $WhereBlock 
        } 
        Else {
            $PoshRS_Jobs
        }
    }
}