Public/Wait-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
Function Wait-RSJob {
    <#
        .SYNOPSIS
            Waits until all RSJobs are in one of the following states:
 
        .DESCRIPTION
            Waits until all RSJobs are in one of the following states:
 
        .PARAMETER Job
            The job object to wait for.
 
        .PARAMETER Name
            The name of the jobs to wait for.
 
        .PARAMETER ID
            The ID of the jobs that you want to wait for.
 
        .PARAMETER InstanceID
            The GUID of the jobs that you want to wait for.
 
        .PARAMETER State
            The State of the job that you want to wait for. Accepted values are:
            NotStarted
            Running
            Completed
            Failed
            Stopping
            Stopped
            Disconnected
 
        .PARAMETER Batch
            Name of the set of jobs that you want to wait for.
 
        .PARAMETER HasMoreData
            Waits for jobs that have data being outputted. You can specify -HasMoreData:$False to wait for jobs
            that have no data to output.
 
        .PARAMETER Timeout
            Timeout after specified number of seconds. This is a global timeout meaning that it is not a per
            job timeout.
 
        .PARAMETER ShowProgress
            Displays a progress bar
 
        .NOTES
            Name: Wait-RSJob
            Author: Ryan Bushe/Boe Prox/Max Kozlov
 
        .EXAMPLE
            Get-RSJob | Wait-RSJob
            Description
            -----------
            Waits for jobs which have to be completed.
    #>

    [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,
        [int]$Timeout,
        [switch]$ShowProgress
    )
    Begin {
        If ($PSBoundParameters['Debug']) {
            $DebugPreference = 'Continue'
        }
        $List = New-Object System.Collections.ArrayList
    }
    Process {
        Write-Debug "ParameterSet: $($PSCmdlet.ParameterSetName)"
        $Property = $PSCmdlet.ParameterSetName
        if ($PSCmdlet.ParameterSetName -ne 'All' -and $PSBoundParameters[$Property]) {
            Write-Verbose "Adding $($PSBoundParameters[$Property])"
            [void]$List.AddRange($PSBoundParameters[$Property])
        }
    }
    End {
        if ($PSCmdlet.ParameterSetName -ne 'All') {
            $PSBoundParameters[$Property] = $List
        }
        if (-not $List.Count) { return } # No jobs selected to search
        [void]$PSBoundParameters.Remove('Timeout')
        [void]$PSBoundParameters.Remove('ShowProgress')
        [array]$WaitJobs = Get-RSJob @PSBoundParameters

        $TotalJobs = $WaitJobs.Count
        $Completed = 0
        Write-Verbose "Wait for $($TotalJobs) jobs"
        $Date = Get-Date
        while ($Waitjobs.Count -ne 0) {
            Start-Sleep -Milliseconds 100
            #only ever check $WaitJobs State once per loop, and do all operations based on that snapshot to avoid bugs where the state of a job may have changed mid loop
            $JustFinishedJobs = New-Object System.Collections.ArrayList
            $RunningJobs = New-Object System.Collections.ArrayList
            ForEach ($WaitJob in $WaitJobs) {
                If($WaitJob.State -match 'Completed|Failed|Stopped|Suspended|Disconnected' -and $WaitJob.Completed) {
                    [void]$JustFinishedJobs.Add($WaitJob)
                } Else {
                    [void]$RunningJobs.Add($WaitJob)
                }
            }
            $WaitJobs = $RunningJobs

            $JustFinishedJobs

            $Completed += $JustFinishedJobs.Count
            Write-Debug "Wait: $($Waitjobs.Count)"
            Write-Debug "Completed: ($Completed)"
            Write-Debug "Total: ($Totaljobs)"
            Write-Debug "Status: $($Completed/$TotalJobs)"
            If ($ShowProgress) {
                Write-Progress -Activity "RSJobs Tracker" -Status ("Remaining Jobs: {0}" -f $Waitjobs.Count) -PercentComplete (($Completed/$TotalJobs)*100)
            }
            if($Timeout -and (New-Timespan $Date).TotalSeconds -ge $Timeout){
                break
            }
        }
        If ($ShowProgress) {
            Write-Progress -Activity "RSJobs Tracker" -Completed
        }
    }
}