DataSources.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495



<#
.SYNOPSIS
    Retrieves an HelloId DataSource
.DESCRIPTION
    Get-HidDataSource will return one or more Datasources
.PARAMETER DataSourceGuid
    Specifies the Guid of an existing datasource to retrieve, can be specified as an array of strings to retrieve multiple variables
.PARAMETER Name
    Specifies the name of an existing datasource to retrieve, can be specified as an array of strings to retrieve multiple variables
.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-HidDataSource -CompanyName "MyCompany" -ApiKey "myapikey" -ApiSecret (ConvertTo-SecureString -AsPlainText -String "password" -Force)
    
    Returns all datasources
.EXAMPLE
    Get-HidDataSource

    Returns all datasources in the tenant
.EXAMPLE
    Get-HidDatasource -DataSourceGuid "812302af-c3db-4a66-c39d-08d7d4a10f72"
    
    Returns the datasource with guid 812302af-c3db-4a66-c39d-08d7d4a10f72
.EXAMPLE
    "AD-group-generate-table" | Get-HidDataSource

    Returns the datasource with name AD-group-generate-table. The name can be piped
.INPUTS
    You can pipe a string that contains the name to Get-HidDataSource
.OUTPUTS
    Returns an object for each datasource that it gets.
#>

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

        # 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("DatasourceGuid")){            
            foreach ($guid in $DataSourceGuid){
                $URI = "$BaseUrl/api/v1/datasource/$guid"
                $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing               
                $output
            }

        }
        elseif ($PSBoundParameters.ContainsKey("Name")) {
            foreach ($item in $Name){
                $URI = "$BaseUrl/api/v1/datasource/named/$item"
                $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
                $output
            }
        }
        else {
            $URI = "$BaseUrl/api/v1/datasource/all"
            $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
    Creates or updates a datasource
.DESCRIPTION
    Creates a new record of a datasource if the DataSourceGUID is left empty. Updates an existing DataSource if the passed identifier guid matches an existing datasource.
.PARAMETER DataSourceGuid
    If specified it specifies the Guid of an existing datasource to update, if not specified it creates a new datasource
.PARAMETER Name
    Sets the name of the datasource to create or update
.PARAMETER Type
    Sets the Type of the datasource to create or update.
    2 is for Static DataSources
    3 is for Task DataSources
    4 is for Powershell datasources
.PARAMETER Model
    An array containing objects that each represent one of the Data source's output properties.
    Each objects requires the following properties:
    {Key} Name of this output property
    {Type} Type of this output property
.PARAMETER Value
    An array containing zero or more objects. It represents the records a Static Data source will return, Required for Static Data sources
.PARAMETER AutomationTaskGuid
    A guid referring to the Task associated with this Task Data Source
.PARAMETER Input
    An array containing objects that each represent a Data Source Input property. Each object requires the following properties:
    {Key} Name of this input property - Required
    {InputFieldType} Type of this input property. Either 1 (for an input property that can be configured for each form element that is linked to this Data source, also known as a Form input property) or 2 (for an input property that has a predefined value, also known as a Predefined input property) - Required
    {Options} Either 0 (for an input field that is not required) or 1 (for one that is) - Required
    {Description} The description that will show up when configuring a form field that uses this data source. Only relevant for Form input properties. Required for Form input properties
    {PredefinedValue} The predefined value of this input property. May contain HelloID language such as '{{requester.FirstName}}' Required for Predefined input properties
.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
    $ApiKey = "myapikey"
    $ApiSecret = ConvertTo-SecureString -AsPlainText -String "mypassword" -Force
    $CompanyName = "contoso"

    $parameterSplat = @{
    Companyname = $CompanyName
    ApiKey = $ApiKey
    ApiSecret = $ApiSecret
    }
    $sourceDS = Get-HidDatasource -DatasourceGuid "812302af-c3db-4a66-c39d-08d7d4a10f72" @ParameterSplat
    Set-HidDataSource -Name "testDS" -Type $sourceDS.type -Model $sourceDS.model -AutomationTaskGuid $sourceDS.automationTaskGUID @ParameterSplat

    Creates a new Task Datasource with name testDS based on the source datasource with the specified Guid
.EXAMPLE
    Get-HidDataSource -Name testDS | Set-HidDataSource -Name testDS2
    
    Changes the name from the datasource from testDS to testDS2

.INPUTS
    You can pipe that output from Get-DataSouce to Set-DataSource with all the changes
.OUTPUTS

#>

function Set-HidDataSource {
    [CmdletBinding(DefaultParameterSetName = 'taskdatasource',
        PositionalBinding = $false)]
    [Alias()]
    [OutputType([String])]
    Param (
        # Optional, the GUID of an existing datasource
        [Parameter(Mandatory = $false,
            Position = 0,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            ValueFromRemainingArguments = $false)]
        [ValidateNotNullOrEmpty()]
        [guid]$DataSourceGuid,

        # name of the datasource
        [Parameter(Mandatory= $true,
            ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        # Either 2 (if it is a static Data source) or 3 (if it is a Task Data source) or 4 (if its a powershell datasource)
        [Parameter(Mandatory= $true,
            ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet(2,3,4)]
        [int]$Type,

        # An array containing objects that each represent one of the Data source's output properties
        [Parameter(Mandatory= $true,
            ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [array]$Model,
        
        # An array containing zero or more JSON objects. It represents the records a Static Data source will return, Required for Static Data sources
        [Parameter(Mandatory= $true,
            ParameterSetName = "staticdatasource",
            ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [array]$Value,

        # A guid referring to the Task associated with this Task Data Source, Required for Task Data sources
        [Parameter(Mandatory= $true,
            ParameterSetName = "taskdatasource",
            ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$AutomationTaskGuid,

        # An array containing objects that each represent a Data Source Input property, for Task Data sources only
        [Parameter(Mandatory= $false,
            ParameterSetName = "taskdatasource",
            ValueFromPipelineByPropertyName = $true)]
        [Parameter(Mandatory= $false, 
            ParameterSetName = "powershelldatasource")]
        [alias("Input")]
        [array]$DataSourceInput,

        # Powershell script, for PowerShell Data sources only
        [Parameter(Mandatory= $false,
        ParameterSetName = "powershelldatasource",
        ValueFromPipelineByPropertyName = $true)]
        [alias("PowerShellScript")]
        [string]$Script,

        # 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"
        }

        
        $JsonModel = ConvertTo-Json $Model -Depth 15
        $JsonValue = ConvertTo-Json $Value -Depth 15
        $JsonInput = ConvertTo-Json $DataSourceInput -Depth 15
        #Headers
        $headers = @{
            "Content-Type" = "application/json"
        }

        #Uri
        $URI = "$BaseUrl/api/v1/datasource"
    } #End begin
    
    process {
        if ($Type -eq 2) { #static datasource

            $SbBody = [System.Text.StringBuilder]::new()
            $null = $SbBody.AppendLine("{")
            if ($PSBoundParameters.ContainsKey("DataSourceGuid")){ $null = $SbBody.AppendLine("`"dataSourceGUID`": `"$DataSourceGuid`"`,") }
            $null = $SbBody.AppendLine("`"type`": `"$Type`",")
            $null = $SbBody.AppendLine("`"name`": `"$Name`",")
            $null = $SbBody.AppendLine("`"model`": $JsonModel,")
            $null = $SbBody.AppendLine("`"value`": $JsonValue")
            $null = $SbBody.AppendLine("}")
            
        } #END if static datasource

        if ($Type -eq 3) { #task datasource

            $SbBody = [System.Text.StringBuilder]::new()
            $null = $SbBody.AppendLine("{")
            if ($PSBoundParameters.ContainsKey("DataSourceGuid")){ $null = $SbBody.AppendLine("`"dataSourceGUID`": `"$DataSourceGuid`"`,") }
            $null = $SbBody.AppendLine("`"type`": `"$Type`",")
            $null = $SbBody.AppendLine("`"name`": `"$Name`",")
            $null = $SbBody.AppendLine("`"model`": $JsonModel,")
            $null = $SbBody.AppendLine("`"automationTaskGUID`": `"$AutomationTaskGuid`",")
            $null = $SbBody.AppendLine("`"input`": $JsonInput")
            $null = $SbBody.AppendLine("}")
            
        } #END if task datasource

        if ($Type -eq 4){ # new Powershell datasource

            $SbBody = [System.Text.StringBuilder]::new()
            $null = $SbBody.AppendLine("{")
            if ($PSBoundParameters.ContainsKey("DataSourceGuid")){ $null = $SbBody.AppendLine("`"dataSourceGUID`": `"$DataSourceGuid`"`,") }
            $null = $SbBody.AppendLine("`"type`": `"$Type`",")
            $null = $SbBody.AppendLine("`"name`": `"$Name`",")
            $null = $SbBody.AppendLine("`"model`": $JsonModel,")
            if ($PSBoundParameters.ContainsKey("DataSourceInput")) { $null = $SbBody.AppendLine("`"input`": $JsonInput,") }
            $null = $SbBody.AppendLine("`"script`": `"$Script`"")
            $null = $SbBody.AppendLine("}")


        } #END if Powershell datasource

        Write-Debug "Invoking REST method"
        Write-Debug "Uri = $URI"
        Write-Debug "Headers = $headers"
        Write-Debug "body = $($SbBody.ToString())"
        Write-Debug "Credential = $Cred"
        Write-Verbose "Creating / Setting datasource with name: $Name"
        $output = Invoke-RestMethod -Uri $URI -Method "POST" -Headers $headers -Body $SbBody.ToString() -Credential $Cred -UseBasicParsing


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







<#
.SYNOPSIS
    Removes a Data Source
.DESCRIPTION
    Removes a Data Source based on the guid
.PARAMETER DataSourceGuid
    Specifies the Guid of an existing data source to remove, can be specified as an array of strings to remove multiple variables
.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
    Remove-HidDataSource -DataSourceGuid "c31fd53a-b346-4bb4-9a67-f9406d3968db"

    Removes the Data Source with the specified GUID
.INPUTS
    Inputs to this cmdlet (if any)
.OUTPUTS
    Output from this cmdlet (if any)
#>

function Remove-HidDataSource {
    [CmdletBinding(DefaultParameterSetName = 'Parameter Set 1',
        PositionalBinding = $false)]
    [Alias()]
    [OutputType([String])]
    Param (
        # the GUID of an existing Data Source
        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true)]
        [alias("Guid")]
        [ValidateNotNullOrEmpty()]
        [guid[]]$DataSourceGuid,        
        
        # 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 {        

        foreach ($guid in $DataSourceGuid){
        #Uri
        $URI = "$BaseUrl/api/v1/datasource/$guid"        
        Invoke-RestMethod -Uri $URI -Method "DELETE" -Headers $headers -Credential $Cred -UseBasicParsing        
        } #end foreach

    } #End process
    
    end {
    } #End end
} #End function