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
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 Job
            Represents the RSJob object being sent to query for.
 
        .PARAMETER Name
            The name of the jobs to query for.
 
        .PARAMETER ID
            The ID of the jobs to query for.
 
        .PARAMETER InstanceID
            The GUID of the jobs to query for.
 
        .PARAMETER Batch
            Name of the set of jobs to query for.
 
        .PARAMETER State
            The State of the job that you want to display. Accepted values are:
 
            NotStarted
            Running
            Completed
            Failed
            Stopping
            Stopped
            Disconnected
 
        .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/Max Kozlov
 
        .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='Job', Position=0)]
        [Alias('InputObject')]
        [RSJob[]]$Job,
        [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True,
        ParameterSetName='Name', Position=0)]
        [string[]]$Name,
        [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True,
        ParameterSetName='Id', Position=0)]
        [int[]]$Id,
        [parameter(ValueFromPipelineByPropertyName=$True,
        ParameterSetName='InstanceID')]
        [string[]]$InstanceID,
        [parameter(ValueFromPipelineByPropertyName=$True,
        ParameterSetName='Batch')]
        [string[]]$Batch,

        [parameter(ParameterSetName='Batch')]
        [parameter(ParameterSetName='Name')]
        [parameter(ParameterSetName='Id')]
        [parameter(ParameterSetName='InstanceID')]
        [parameter(ParameterSetName='All')]
        [ValidateSet('NotStarted','Running','Completed','Failed','Stopping','Stopped','Disconnected')]
        [string[]]$State,
        [parameter(ParameterSetName='Batch')]
        [parameter(ParameterSetName='Name')]
        [parameter(ParameterSetName='Id')]
        [parameter(ParameterSetName='InstanceID')]
        [parameter(ParameterSetName='All')]
        [Switch]$HasMoreData
    )
    Begin {
        If ($PSBoundParameters['Debug']) {
            $DebugPreference = 'Continue'
        }
        Write-Debug "ParameterSet: $($PSCmdlet.parametersetname)"
        $Hash = @{}
        $ResultJobs = New-Object System.Collections.ArrayList
    }
    Process {
        Write-Debug "ParameterSet: $($PSCmdlet.ParameterSetName)"
        $Property = $PSCmdlet.ParameterSetName

        if ($PSCmdlet.ParameterSetName -eq 'Job') {
            Write-Verbose "Adding Job $($PSBoundParameters[$Property].Id)"
            foreach ($v in $PSBoundParameters[$Property]) {
                $Hash.Add($v.ID,1)
            }
        }
        elseif ($PSCmdlet.ParameterSetName -ne 'All') {
            Write-Verbose "Adding $($PSBoundParameters[$Property])"
            foreach ($v in $PSBoundParameters[$Property]) {
                $Hash.Add($v,1)
            }
        }
    }
    End {
        #Job objects searched by ID
        if ($Property -eq 'Job') { $Property = 'ID' }
        $States = if ($PSBoundParameters.ContainsKey('State')) { '^' + ($State -join '$|^') + '$' } else { '.' }

        # IF faster than any scriptblocks
        if ($PSCmdlet.ParameterSetName -eq 'All') {
            Write-Verbose 'All Jobs'
            $ResultJobs = $PoshRS_Jobs
        }
        else {
            Write-Verbose "Filtered Jobs by $Property"
            foreach ($job in $PoshRS_Jobs) {
                if ($Hash.ContainsKey($job.$Property))
                {
                    [void]$ResultJobs.Add($job)
                }
            }
        }
        foreach ($job in $ResultJobs) {
            if (($job.State -match $States) -and
                (-not $PSBoundParameters.ContainsKey('HasMoreData') -or $job.HasMoreData -eq $HasMoreData)
               )
            {
                $job
            }
        }
    }
}