Roles.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847


<#
.SYNOPSIS
    The get role operation will retrieve a list of roles from your company.
.DESCRIPTION
    The get roles operation will retrieve a list of roles from your company. This can be either all roles, or roles filtered by name or ID.
.PARAMETER Name
    Specifies the name of an existing variable to retrieve, can be specified as an array of strings to retrieve multiple variables
.PARAMETER Name
    Specifies the name of an existing variable 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
    
.EXAMPLE
    
.INPUTS
    
.OUTPUTS
    
#>

function Get-HidRole {
    [CmdletBinding(DefaultParameterSetName = 'guid',PositionalBinding = $false)]
    [Alias()]
    [OutputType([String])]
    Param ( 
        # the name of an existing variable
        [Parameter(Mandatory = $false,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true,
        ParameterSetName = "guid")]
        [ValidateNotNullOrEmpty()]
        [guid[]]$RoleGuid,
    
        # the name of an existing variable
        [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("RoleGuid")){            
            foreach ($guid in $RoleGuid){
                $URI = "$BaseUrl/api/v1/roles/$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/roles/$item"
                $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
                $output
            }
        }        
        else {
            $URI = "$BaseUrl/api/v1/roles"
            $output = Invoke-RestMethod -Uri $URI -Method "GET" -Headers $headers -Credential $Cred -UseBasicParsing
            $output            
        }
    } #End process
    
    end {
        
    } #End end
} #End function





<#
.SYNOPSIS
    The new role operation will create a new role
.DESCRIPTION
    The new role cmdlet will create a new role in your tenant
.PARAMETER Name
    Specifies the name of the role to create
.PARAMETER Description
    Specifies the description of the role to create
.PARAMETER UserNames
    The User names to add to the role
.PARAMETER UserGuids
    The user Guids to add to the role
.PARAMETER Rights
    Specifies the Rights to assign to the role
.PARAMETER Enabled
    Role enabled true / false
.PARAMETER Default
    Role is a default role true / false
.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
    
.EXAMPLE
    
.INPUTS
    
.OUTPUTS
    
#>

function New-HidRole {
    [CmdletBinding(PositionalBinding = $false)]
    [Alias()]
    [OutputType([String])]
    Param (    
        # the name of an existing variable
        [Parameter(Mandatory = $false,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,        

        # description of the role
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$Description,

        # Usernames to add to the role
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string[]]$UserNames = @(),
        
        # UserGuids to add to the role
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [guid[]]$UserGuids = @(),
        
        # Rights to assign to the role
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [int[]]$Rights = @(),
        
        # Role enabled true / false
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [bool]$Enabled = $true,
        
        # Default role true / false
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [bool]$Default = $false,

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


        #Variables
        $JsonUserNames = ConvertTo-Json $UserNames -Depth 15
        $JsonUserGuids = ConvertTo-Json $UserGuids -Depth 15
        $JsonRights = ConvertTo-Json $Rights -Depth 15

        if (!($Description)){
            $Description = $Name
        }

        
    } #End begin
    
    process {  
        
        
        $sbBody = [System.Text.StringBuilder]::new()
        $null = $SbBody.AppendLine("{") 
        $null = $SbBody.AppendLine("`"name`": `"$Name`",")
        $null = $SbBody.AppendLine("`"Description`": `"$Description`",")
        $null = $SbBody.AppendLine("`"IsEnabled`": $(($Enabled).ToString().ToLower()),")
        $null = $SbBody.AppendLine("`"IsDefault`": $(($Default).ToString().ToLower()),")
        $null = $SbBody.AppendLine("`"UserNames`": $JsonUserNames,")
        $null = $SbBody.AppendLine("`"UserGuids`": $JsonUserGuids,")
        $null = $SbBody.AppendLine("`"Rights`": $JsonRights")
        $null = $SbBody.AppendLine("}") 
        
        Write-Debug "body is:`n $($SbBody.ToString())"

        $URI = "$BaseUrl/api/v1/roles"
        $output = Invoke-RestMethod -Uri $URI -Method "POST" -Headers $headers -Credential $Cred -UseBasicParsing -Body $sbBody.tostring()
        $output            
        
    } #End process
    
    end {
        
    } #End end
} #End function





<#
.SYNOPSIS
    The set role operation will update a role
.DESCRIPTION
    The set role cmdlet will update a role in your tenant
.PARAMETER Name
    Specifies the name of an existing variable to retrieve, can be specified as an array of strings to retrieve multiple variables
.PARAMETER Name
    Specifies the name of an existing variable 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
    
.EXAMPLE
    
.INPUTS
    
.OUTPUTS
#>

function Set-HidRole {
    [CmdletBinding(PositionalBinding = $false,DefaultParameterSetName = "guid")]
    [Alias()]
    [OutputType([String])]
    Param (

        # the guid of an existing role to update
        [Parameter(Mandatory = $false,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true,
        ParameterSetName = "guid")]
        [ValidateNotNullOrEmpty()]
        [guid]$RoleGuid,

        # the name of an existing role to update
        [Parameter(Mandatory = $false,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true,
        ParameterSetName = "name")]
        [ValidateNotNullOrEmpty()]
        [string]$Name,        

        # New name for the role
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$NewName,

        # New description of the role
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [string]$Description,        
        
        # Role enabled true / false
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [bool]$Enabled,
        
        # Default role true / false
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [bool]$Default,
        
        # Rights to assign to the role
        [Parameter(Mandatory= $false)]
        [ValidateNotNullOrEmpty()]
        [int[]]$Rights,

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


        #Variables
        $JsonRights = ConvertTo-Json $Rights -Depth 15

        if (!($Description)){
            $Description = $Name
        }

        
    } #End begin
    
    process {        
        
        $sbBody = [System.Text.StringBuilder]::new()
        $null = $SbBody.AppendLine("{") 
        if ($PSBoundParameters.ContainsKey("NewName")) {
            $null = $SbBody.AppendLine("`"name`": `"$NewName`"") 
            if ($PSBoundParameters.ContainsKey("Description") -or $PSBoundParameters.ContainsKey("Enabled") -or $PSBoundParameters.ContainsKey("Default") -or $PSBoundParameters.ContainsKey("Rights")){ $sbBody.Append(",") }
        }
        if ($PSBoundParameters.ContainsKey("Description")) { 
            $null = $SbBody.AppendLine("`"Description`": `"$Description`"") 
            if ($PSBoundParameters.ContainsKey("Enabled") -or $PSBoundParameters.ContainsKey("Default") -or $PSBoundParameters.ContainsKey("Rights")){ $sbBody.Append(",") }
        }
        if ($PSBoundParameters.ContainsKey("Enabled")) { 
            $null = $SbBody.AppendLine("`"IsEnabled`": $(($Enabled).ToString().ToLower())") 
            if ($PSBoundParameters.ContainsKey("Default") -or $PSBoundParameters.ContainsKey("Rights")){ $sbBody.Append(",") }
        }
        if ($PSBoundParameters.ContainsKey("Default")) { 
            $null = $SbBody.AppendLine("`"IsDefault`": $(($Default).ToString().ToLower())") 
            if ($PSBoundParameters.ContainsKey("Rights")){ $sbBody.Append(",") }
        }
        if ($PSBoundParameters.ContainsKey("Rights")) { $null = $SbBody.AppendLine("`"Rights`": $JsonRights") }
        $null = $SbBody.AppendLine("}") 
        
        
        Write-Debug "body is:`n $($SbBody.ToString())"

        if ($PSBoundParameters.ContainsKey("Name")){
            $URI = "$BaseUrl/api/v1/roles/$Name"
        }
        elseif ($PSBoundParameters.ContainsKey("RoleGuid")) {
            $URI = "$BaseUrl/api/v1/roles/$RoleGuid"
        }
        $output = Invoke-RestMethod -Uri $URI -Method "PUT" -Headers $headers -Credential $Cred -UseBasicParsing -Body $sbBody.tostring()
        $output            
        
    } #End process
    
    end {
        
    } #End end
} #End function







<#
.SYNOPSIS
    The assign role to user operation will assign a role to a user
.DESCRIPTION
    The assign role to user operation will assign a role to a user
.PARAMETER RoleGuid
    Specifies the Guid of an existing role to add users or groups to
.PARAMETER RoleName
    Specifies the name of an existing role to add users or groups to
.PARAMETER UserNames
    Specifies the usernames to be added to the role
.PARAMETER UserGuids
    Specifies the Guids of users to be added to the role
.PARAMETER GroupGuids
    Specifies the guids of groups to be added to the role
.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
    
.EXAMPLE
    
.INPUTS
    
.OUTPUTS
#>

function New-HidRoleAssignment {
    [CmdletBinding(PositionalBinding = $false,DefaultParameterSetName = "roleguid_users")]
    [Alias()]
    [OutputType([String])]
    Param (

        # the guid of an existing role to add users to
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true,
        ParameterSetName = "roleguid_users")]
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true,
        ParameterSetName = "roleguid_groups")]
        [ValidateNotNullOrEmpty()]
        [guid]$RoleGuid,

        # Name of the role to add users to
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $false,
        ParameterSetName = "rolename_users")]
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true,
        ParameterSetName = "rolename_groups")]
        [ValidateNotNullOrEmpty()]
        [string]$RoleName,        

        # Usernames to add to the role
        [Parameter(Mandatory= $false,ParameterSetName = "roleguid_users")]
        [Parameter(Mandatory= $false,ParameterSetName = "rolename_users")]
        [ValidateNotNullOrEmpty()]
        [string[]]$UserNames = @(),
        
        # UserGuids to add to the role
        [Parameter(Mandatory= $false,ParameterSetName = "roleguid_users")]
        [Parameter(Mandatory= $false,ParameterSetName = "rolename_users")]
        [ValidateNotNullOrEmpty()]
        [guid[]]$UserGuids = @(),

        # GroupGuids to add to the role
        [Parameter(Mandatory= $true,ParameterSetName = "roleguid_groups")]
        [Parameter(Mandatory= $true,ParameterSetName = "rolename_groups")]
        [ValidateNotNullOrEmpty()]
        [guid[]]$GroupGuids = @(),

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

        #Variables
        $JsonUserNames = ConvertTo-Json $UserNames -Depth 15
        $JsonUserGuids = ConvertTo-Json $UserGuids -Depth 15
        $JsonGroupGuids = ConvertTo-Json $GroupGuids -Depth 15

        
    } #End begin
    
    process {
        
        $sbBody = [System.Text.StringBuilder]::new()

        if($PSBoundParameters.ContainsKey("GroupGuids")){
            $UriEndpoint = "groups" #used in the POST url

            $null = $SbBody.AppendLine("{")
            $null = $SbBody.AppendLine("`"GroupGuids`": $JsonGroupGuids")
            $null = $SbBody.AppendLine("}") 
        }
        else {
            $UriEndpoint = "users" #used in the POST url

            $null = $SbBody.AppendLine("{")
            $null = $SbBody.AppendLine("`"UserNames`": $JsonUserNames,")
            $null = $SbBody.AppendLine("`"UserGuids`": $JsonUserGuids")
            $null = $SbBody.AppendLine("}") 
        }
        
        Write-Debug "body is:`n $($SbBody.ToString())"

        if ($PSBoundParameters.ContainsKey("RoleName")){
            $URI = "$BaseUrl/api/v1/roles/$RoleName/$UriEndpoint"
        }
        elseif ($PSBoundParameters.ContainsKey("RoleGuid")) {
            $URI = "$BaseUrl/api/v1/roles/$RoleGuid/$UriEndpoint"
        }
        $output = Invoke-RestMethod -Uri $URI -Method "POST" -Headers $headers -Credential $Cred -UseBasicParsing -Body $sbBody.tostring()
        $output
        
    } #End process
    
    end {
        
    } #End end
} #End function





<#
.SYNOPSIS
    The remove role to user operation will remove a role from a user
.DESCRIPTION
    The remove role to user operation will remove a role from a user
.PARAMETER RoleGuid
    Specifies the Guid of an existing role to remove users or groups from
.PARAMETER RoleName
    Specifies the name of an existing role to remove users or groups from
.PARAMETER UserNames
    Specifies the usernames to be removed from the role
.PARAMETER UserGuids
    Specifies the Guids of users to be removed from the role
.PARAMETER GroupGuids
    Specifies the guids of groups to be removed from the role
.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
    
.EXAMPLE
    
.INPUTS
    
.OUTPUTS
#>

function Remove-HidRoleAssignment {
    [CmdletBinding(PositionalBinding = $false,DefaultParameterSetName = "roleguid_users_userguid")]
    [Alias()]
    [OutputType([String])]
    Param (

        # the guid of an existing role to remove users or groups from
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true,
        ParameterSetName = "roleguid_users_username")]
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true,
        ParameterSetName = "roleguid_users_userguid")]
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true,
        ParameterSetName = "roleguid_groups")]
        [ValidateNotNullOrEmpty()]
        [guid]$RoleGuid,

        # Name of the role to remove users or groups from
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $false,
        ParameterSetName = "rolename_users_username")]
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $false,
        ParameterSetName = "rolename_users_userguid")]
        [Parameter(Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true,
        ParameterSetName = "rolename_groups")]
        [ValidateNotNullOrEmpty()]
        [string]$RoleName,

        # Usernames to remove from the role
        [Parameter(Mandatory= $true,ParameterSetName = "roleguid_users_username")]
        [Parameter(Mandatory= $true,ParameterSetName = "rolename_users_username")]
        [ValidateNotNullOrEmpty()]
        [string]$UserName,
        
        # UserGuids to remove from the role
        [Parameter(Mandatory= $true,ParameterSetName = "roleguid_users_userguid")]
        [Parameter(Mandatory= $true,ParameterSetName = "rolename_users_userguid")]
        [ValidateNotNullOrEmpty()]
        [guid]$UserGuid,

        # GroupGuids to remove from the role
        [Parameter(Mandatory= $true,ParameterSetName = "roleguid_groups")]
        [Parameter(Mandatory= $true,ParameterSetName = "rolename_groups")]
        [ValidateNotNullOrEmpty()]
        [guid[]]$GroupGuids,

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

        #Variables
        #$JsonUserNames = ConvertTo-Json $UserNames -Depth 15
        #$JsonUserGuids = ConvertTo-Json $UserGuids -Depth 15
        $JsonGroupGuids = ConvertTo-Json $GroupGuids -Depth 15

        
    } #End begin
    
    process {
        
        
        if($PSBoundParameters.ContainsKey("GroupGuids")){
            $UriEndpoint = "groups" #used in the POST url
            $sbBody = [System.Text.StringBuilder]::new()

            $null = $SbBody.AppendLine("{")
            $null = $SbBody.AppendLine("`"GroupGuids`": $JsonGroupGuids")
            $null = $SbBody.AppendLine("}") 

            Write-Debug "body is:`n $($SbBody.ToString())"

            if ($PSBoundParameters.ContainsKey("RoleName")){
                $URI = "$BaseUrl/api/v1/roles/$RoleName/$UriEndpoint"
            }
            elseif ($PSBoundParameters.ContainsKey("RoleGuid")) {
                $URI = "$BaseUrl/api/v1/roles/$RoleGuid/$UriEndpoint"
            }
            $output = Invoke-RestMethod -Uri $URI -Method "DELETE" -Headers $headers -Credential $Cred -UseBasicParsing -Body $sbBody.tostring()
            
        }
        else {
            
            if ($PSBoundParameters.ContainsKey("UserName")){
                $UsernameOrGuid = $UserName
            }
            else {
                $UsernameOrGuid = $UserGuid
            }

            if ($PSBoundParameters.ContainsKey("RoleName")){
                $URI = "$BaseUrl/api/v1/roles/$RoleName/users/$UsernameOrGuid"
            }
            elseif ($PSBoundParameters.ContainsKey("RoleGuid")) {
                $URI = "$BaseUrl/api/v1/roles/$RoleGuid/users/$UsernameOrGuid"
            }
            $output = Invoke-RestMethod -Uri $URI -Method "DELETE" -Headers $headers -Credential $Cred -UseBasicParsing
            
        }
        
        $output

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