Public/Administration/Get-VCDSTasks.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
function Get-VCDSTasks(){
    <#
    .SYNOPSIS
    Returns a collection of Tasks from the connected Cloud Director Service environment.

    .DESCRIPTION
    Returns a collection of Tasks from the connected Cloud Director Service environment.

    .PARAMETER EnvironmentId
    Optionally the Cloud Director Service Environment Id (the default is used if none is provided)

    .PARAMETER Id
    The Cloud Director service Task Id to filter the Tasks.

    .PARAMETER EntityId
    The Cloud Director service Entity Id to filter the Tasks.

    .PARAMETER TaskName
    The Cloud Director service Task Name to filter the Tasks by.

    .PARAMETER EventStatus
    The status to filter the events.

    .PARAMETER UserId
    The UserId to filter the events.

    .PARAMETER IncludeFiles
    If true returns the details of any files assosicated with the Task (e.g. Support Bundles)

    .EXAMPLE
    Get-VCDSTasks
    Returtns the VCDS tasks for default environment.

    .EXAMPLE
    Get-VCDSTasks -EnvironmentId "urn:vcdc:environment:3fccbd2a-003c-4303-8f1a-8569853236ac"
    Returns the VCDS tasks for the Environment with the Id "urn:vcdc:environment:3fccbd2a-003c-4303-8f1a-8569853236ac"

    .EXAMPLE
    Get-VCDSTasks -EntityId urn:vcdc:vcdInstance:35cdd922-dae7-415f-bbef-d333ac5573d9 -EventStatus "Failed"
    Returns all of the VCDS tasks for the Entity "urn:vcdc:vcdInstance:35cdd922-dae7-415f-bbef-d333ac5573d9" that have a status of failed.

    .EXAMPLE
    Get-VCDSTasks -TaskName "createSupportBundle"
    Returns all of the VCDS tasks with the Task Name "createSupportBundle"

    .NOTES
    AUTHOR: Adrian Begg
    LASTEDIT: 2020-07-07
    VERSION: 1.2
    #>

    Param(
        [Parameter(Mandatory=$False)]
            [ValidateNotNullorEmpty()] [String] $Id,
            [ValidateNotNullorEmpty()] [String] $TaskName,
            [ValidateNotNullorEmpty()] [String] $EntityId,
            [ValidateSet("SUCCESS","IN_PROGRESS","FAILED")] [String] $EventStatus,
            [ValidateNotNullorEmpty()] [String] $UserId,
            [ValidateNotNullorEmpty()] [String]  $EnvironmentId,
            [switch] $IncludeFiles
    )
    # TO DO : Implement filtering on the Task Properties
    # TO DO : Pending security fix from VMware (currently can see all tasks), adjust the URI to use the Organisation Id instead of just environment Id
    if(!$global:VCDService.IsConnected){
        throw "You are not currently connected to the VMware Cloud Services Portal (CSP) for VMware Cloud Director Service. Please use Connect-VCDService cmdlet to connect to the service and try again."
    }
    # 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
    }
    # Setup a Service URI for the environment
    $ServiceURI = $Environment.ServiceURI

    # A collection of filters
    [Hashtable] $htFilters = @{
        sortBy = "queuedTime"
        sortDir = "desc"
        page = 1
        limit = 100
    }

    # Setup a HashTable for the API call to the Cloud Gateway
    $TasksAPIEndpoint = "$ServiceURI/environment/$($Environment.id)/organization/$($VCDService.OrganizationId)/tasks"

    # Setup the URI for a single Task
    if($PSBoundParameters.ContainsKey("Id")){
        $TasksAPIEndpoint = "$ServiceURI/environment/$($Environment.id)/tasks/$Id"
    }

    # A Hashtable of Request Parameters
    [Hashtable] $RequestParameters = @{
        URI = $TasksAPIEndpoint
        Method = "Get"
        ContentType = "application/json"
        Headers = @{
            "Authorization" = "Bearer $($global:VCDService.AccessToken)"
            "Accept" = "application/json"
        }
        UseBasicParsing = $true
        Body = $htFilters
    }
    # Now make an inital call to the API
    $Response =  ((Invoke-WebRequest @RequestParameters).Content | ConvertFrom-Json)
    # Check if single result or multiple results
    if($PSBoundParameters.ContainsKey("Id")){
        $colTasks = $Response
    } else {
        # Add the inital tasks from the first API call to a collection
        $colTasks = $Response.values
        # Iterate over the results to retrieve all tasks
        if($Response.pageCount -ne 0){
            while ($Response.pageCount -gt $Response.page){
                # Increment to the next page and add the results
                ($htFilters.page)++ | Out-Null
                $RequestParameters.Body = $htFilters
                $Response = ((Invoke-WebRequest @RequestParameters).Content | ConvertFrom-Json)
                $colTasks += $Response.values
            }
        }
    }
    # Next check if the -IncludeFiles flag was provided for the task
    if ($PSBoundParameters.ContainsKey("IncludeFiles")) {
        # Iterate through all tasks and add any files to the object
        foreach($objTask in $colTasks){
            # Construct the URI
            $TaskFilesURI = "$TasksAPIEndpoint/${$objTask.id}files"
            # A Hashtable of Request Parameters
            [Hashtable] $FileRequestParameters = @{
                URI = $TaskFilesURI
                Method = "Get"
                ContentType = "application/json"
                Headers = @{
                    "Authorization" = "Bearer $($global:VCDService.AccessToken)"
                    "Accept" = "application/json"
                }
                UseBasicParsing = $true
            }
            # Now make an inital call to the API
            $FilesResponse =  ((Invoke-WebRequest @FileRequestParameters).Content | ConvertFrom-Json)
            # Check if anything was returned
            if($FilesResponse.Count -gt 0){
                # Add the files to the task object
                $objTask | Add-Member Note* files $FilesResponse
            }
        }
    }
    # Finally post call filters
    if($PSBoundParameters.ContainsKey("EntityId")){
        $colTasks = $colTasks | Where-Object{$_.entityId -eq $EntityId}
    }
    if($PSBoundParameters.ContainsKey("EventStatus")){
        $colTasks = $colTasks | Where-Object{$_.status -eq $EventStatus}
    }
    if($PSBoundParameters.ContainsKey("UserId")){
        $colTasks = $colTasks | Where-Object{$_.UserId -eq $UserId}
    }
    if($PSBoundParameters.ContainsKey("TaskName")){
        $colTasks = $colTasks | Where-Object{$_.name -eq $TaskName}
    }
    return $colTasks
}