Public/Administration/Watch-VCDSTaskCompleted.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
function Watch-VCDSTaskCompleted(){
    <#
    .SYNOPSIS
     This cmdlet monitors a running task and returns True when the task completes.

    .DESCRIPTION
     This cmdlet monitors a running task and returns True when the task completes

    .PARAMETER Task
    A PSObject containing a Task object returned by an API POST call

    .PARAMETER Timeout
    Optionally the timeout in seconds before the cmdlet should terminate if the task has not completed.

    Default is 180 seconds.
    For no timeout set to -1

    .EXAMPLE
    Watch-VCDSTaskCompleted -Task $RemoveTask -Timeout 180
    Monitors the task in the object $RemoveTask for a maximum of 60 seconds and returns True when the task completes

    .NOTES
    NAME: Watch-VCDSTaskCompleted
    AUTHOR: Adrian Begg
    LASTEDIT: 2020-02-15
    #>

    Param(
        [Parameter(Mandatory=$True)]
            [ValidateNotNullorEmpty()] [PSObject] $Task,
        [Parameter(Mandatory=$False)]
            [ValidateRange(-1,3600)] [int] $Timeout = 180,
        [Parameter(Mandatory=$False)]
            [ValidateNotNullorEmpty()] [String] $EnvironmentId
    )
    $boolTaskComplete = $false

    # Next check if the EnvironmentId has been provided and is valid
    if($PSBoundParameters.ContainsKey("EnvironmentId")){
        $Environment = $global:VCDService.VCDSEnvironments | Where-Object {$_.id -eq $EnvironmentId}
        if($Environment.count -eq 0){
            throw "An VCDS Environment with the Id $EnvironmentId can not be found. Please check the Id and try again."
        }
    } else {
        $Environment = $global:VCDService.DefaultEnvironment
    }
    Do {
        $objTaskStatus = Get-VCDSTasks -Id $Task.id -EnvironmentId $Environment.Id
        # Generate the percentage based on the steps if set
        if($objTaskStatus.steps.length -gt 0){
            [double] $Percentage = ([int]($objTaskStatus.steps.Split("/")[0]) / [int]($objTaskStatus.steps.Split("/")[1]))* 100
        } else {
            [double] $Percentage = 0
        }
        Write-Debug $objTaskStatus
        # Check if the Percentage is greater then 100 (this can happen) and normalise it
        if($Percentage -gt 100){
            $Percentage = 100
        }
        Write-Progress -Activity "Task Id: $($Task.id)" -PercentComplete ($Percentage)
        if($objTaskStatus.status -ne "IN_PROGRESS"){
            $boolTaskComplete = $true
            if($objTaskStatus.status -ne "SUCCESS"){
                throw "An error occurred executing Task Id $($Task.id). Errors: $($objTaskStatus.message)"
                Break
            }
        }
        if($Timeout -ne -1){
            $Timeout--
        }
        Start-Sleep -Seconds 1
    } Until (($Timeout -eq 0) -or $boolTaskComplete)
    if(($Timeout -eq 0) -and !$boolTaskComplete){
        throw "A timeout occurred waiting for the Task Id $($Task.id) to complete."
    }
    $boolTaskComplete
}