Tasks.psm1

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300


<#
.SYNOPSIS
    Retrieves an automation task
.DESCRIPTION
    Get-HidTask will return one or more Tasks
.PARAMETER Name
    Specifies the name of an existing task to retrieve, can be specified as an array of strings to retrieve multiple tasks
.PARAMETER Context
    When specified gets the automation tasks under the specified contexts
.PARAMETER Category
    If specified also requires a context. returns the tasks nder the specified category
.PARAMETER CompanyName
    The companyname that's used in the helloId URL to know which HelloID tenant to talk to. Required if not connected with Connect-HelloId.
.PARAMETER ApiKey
    The Apikey to use for the api call. Required if not connected with Connect-HelloId.
.PARAMETER ApiSecret
    The Apisecret belonging to the apikey, has to be a securestring. Required if not connected with Connect-HelloId.
.EXAMPLE
    Get-HidTask -CompanyName "MyCompany" -ApiKey "myapikey" -ApiSecret (ConvertTo-SecureString -AsPlainText -String "password" -Force)
    Returns all tasks in the tenant
.EXAMPLE
    Get-HidTask -Name "companyName"
    Returns the task with the name "companyName"
.INPUTS
    You can pipe a string that contains the name to Get-HidTask
.OUTPUTS
    Get-HidTask returns an object for each task that it gets.
#>

function Get-HidTask {
    [CmdletBinding(DefaultParameterSetName = 'Name',PositionalBinding = $false)]
    [Alias()]
    [OutputType([String])]
    Param (        
                
        # the name of an existing task
        [Parameter(Mandatory = $false,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true,
        ParameterSetName = "Name")]
        [ValidateNotNullOrEmpty()]
        [alias("TaskName")]
        [string[]]$Name,

        # the context of a task
        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [string]$Context,
        
        # the category of a task
        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [string]$Category,

        # Company name used in the URL
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$CompanyName,
        
        # Api key
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$ApiKey,

        # Api secret
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [securestring]$ApiSecret,

        # For preview tenants
        [Parameter(Mandatory = $false)]
        [switch]$IsPreviewTenant
    )
    
    begin {
        if ($PSBoundParameters.ContainsKey("CompanyName") -AND $PSBoundParameters.ContainsKey("ApiKey") -AND $PSBoundParameters.ContainsKey("ApiSecret") ){
            Write-Verbose -Message "Using connectioninfo and credentials from parameter"
            #Create credential object for authentication
            $Cred = New-Object System.Management.Automation.PSCredential ($ApiKey, $ApiSecret)
            if ($IsPreviewTenant){
                $BaseUrl = "https://$CompanyName.preview-helloid.com"
            }
            else {
                $BaseUrl = "https://$CompanyName.helloid.com"
            }
        }
        elseif ($Global:HelloIdConnection.ApiCredentials) {
            Write-Verbose -Message "Using Global connectioninfo and credentials from Connect-HelloId "
            $Cred = $Global:HelloIdConnection.ApiCredentials
            $CompanyName = $Global:HelloIdConnection.CompanyName
            $BaseUrl = $Global:HelloIdConnection.BaseUrl
        }
        else {            
            throw "Error finding connectioninfo. Connect using Connect-HelloId, or specifie CompanyName, ApiKey and ApiSecret"
        }

        #Headers
        $headers = @{
            "Content-Type" = "application/json"
        }
        
    } #End begin
    
    process {
        if ($PSBoundParameters.ContainsKey("Context")) {
            
            if ($PSBoundParameters.ContainsKey("Category")){

                if ($PSBoundParameters.ContainsKey("Name")){
                    foreach ($item in $Name){                        
                        $URI = "$BaseUrl/api/v1/automationstore/tasks/context/$Context/category/$Category/$item"
                        $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
                        $output
                    }
                } #end nested if Name
                else { #else for if cagegory is used without name parameter
                    $URI = "$BaseUrl/api/v1/automationstore/tasks/context/$Context/category/$Category"
                    $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
                    $output
                }
            } #end nested if Category
            else { #Only do this if there's no category used
                
                if ($PSBoundParameters.ContainsKey("Name")){
                    foreach ($item in $Name){                        
                        $URI = "$BaseUrl/api/v1/automationstore/tasks/context/$Context/$item"
                        $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
                        $output
                    }
                } #end nested if Name
                else { #do this only if parameter Context is only thing used, no category and no name
                    $URI = "$BaseUrl/api/v1/automationstore/tasks/context/$Context"
                    $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
                    $output
                }
            } #end elseif based on category
        
        } #end containskey Context
        elseif ($PSBoundParameters.ContainsKey("Category")) {
            throw "Please specify a context if you use the Category parameter"
        } #end elseif

        elseif ($PSBoundParameters.ContainsKey("Name")) {
            foreach ($item in $Name){
                $URI = "$BaseUrl/api/v1/automationstore/tasks/$item"
                $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
                $output
            }
        }
        else {
            $URI = "$BaseUrl/api/v1/automationstore/tasks"
            $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
            <#
            if ($output.psobject.Properties.name -contains "data"){
                $output = $output.data
            }
            #>

            return $output               
            
        }
        
        
    } #End process
    
    end {
        
    } #End end
} #End function




<#
.SYNOPSIS
    Retrieves an automation task
.DESCRIPTION
    Get-HidTask will return one or more Tasks
.PARAMETER TaskName
    Specifies the name of an existing task to retrieve, can be specified as an array of strings to retrieve multiple tasks
.PARAMETER Context
    When specified gets the automation tasks under the specified contexts
.PARAMETER CompanyName
    The companyname that's used in the helloId URL to know which HelloID tenant to talk to. Required if not connected with Connect-HelloId.
.PARAMETER ApiKey
    The Apikey to use for the api call. Required if not connected with Connect-HelloId.
.PARAMETER ApiSecret
    The Apisecret belonging to the apikey, has to be a securestring. Required if not connected with Connect-HelloId.
.EXAMPLE
    Get-HidTask -CompanyName "MyCompany" -ApiKey "myapikey" -ApiSecret (ConvertTo-SecureString -AsPlainText -String "password" -Force)
    Returns all tasks in the tenant
.EXAMPLE
    Get-HidTask -Name "companyName"
    Returns the task with the name "companyName"
.INPUTS
    You can pipe a string that contains the name to Get-HidTask
.OUTPUTS
    Get-HidTask returns an object for each task that it gets.
#>

function Get-HidTaskCategory {
    [CmdletBinding(PositionalBinding = $false)]
    [Alias()]
    [OutputType([String])]
    Param (   
        # The task's context
        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [string]$Context,

        # the name of a Task ONLY USABLE IF CONTEXT IS SET
        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [alias("Name")]
        [string]$TaskName,

        # Company name used in the URL
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$CompanyName,
        
        # Api key
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$ApiKey,

        # Api secret
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [securestring]$ApiSecret,

        # For preview tenants
        [Parameter(Mandatory = $false)]
        [switch]$IsPreviewTenant
    )
    
    begin {
        if ($PSBoundParameters.ContainsKey("CompanyName") -AND $PSBoundParameters.ContainsKey("ApiKey") -AND $PSBoundParameters.ContainsKey("ApiSecret") ){
            Write-Verbose -Message "Using connectioninfo and credentials from parameter"
            #Create credential object for authentication
            $Cred = New-Object System.Management.Automation.PSCredential ($ApiKey, $ApiSecret)
            if ($IsPreviewTenant){
                $BaseUrl = "https://$CompanyName.preview-helloid.com"
            }
            else {
                $BaseUrl = "https://$CompanyName.helloid.com"
            }
        }
        elseif ($Global:HelloIdConnection.ApiCredentials) {
            Write-Verbose -Message "Using Global connectioninfo and credentials from Connect-HelloId "
            $Cred = $Global:HelloIdConnection.ApiCredentials
            $CompanyName = $Global:HelloIdConnection.CompanyName
            $BaseUrl = $Global:HelloIdConnection.BaseUrl
        }
        else {            
            throw "Error finding connectioninfo. Connect using Connect-HelloId, or specifie CompanyName, ApiKey and ApiSecret"
        }
        
        #Headers
        $headers = @{
            "Content-Type" = "application/json"
        }
        
    } #End begin
    
    process {
        if ($PSBoundParameters.ContainsKey("Context")) { #conditional if based on context parameter exists
            foreach ($Cont in $Context){

                if ($PSBoundParameters.ContainsKey("TaskName")){ #if parameter name exists
                    $URI = "$BaseUrl/api/v1/automationstore/categories/context/$Cont/$TaskName"
                } #closing if based on $name
                else {
                    $URI = "$BaseUrl/api/v1/automationstore/categories/context/$Cont"
                }
                $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
                $output

            } #closing foreach cont in context
        } #closing if based on context
        elseif ($PSBoundParameters.ContainsKey("TaskName")){
            throw "Please specify a context if you use the TaskName parameter"
        }
        else {
            $URI = "$BaseUrl/api/v1/automationstore/categories/context/"
            $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
            <#
            if ($output.psobject.Properties.name -contains "data"){
                $output = $output.data
            }
            #>

            return $output            
        }
    } #End process
    end {        
    } #End end
} #End function