Public/Get-BBUserRole.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
Function Get-BBUserRole {
    <#
    .SYNOPSIS
        Get the list of userroles as they are known in the GoBright API
    .DESCRIPTION
        Get the list of userroles as they are known in the GoBright API
    .PARAMETER BrightBookingApiUrl
        Address of the GoBright API, e.g.: https://t1b.gobright.cloud/ (please get this from the 'General Settings' page of the portal)
    .PARAMETER BrightBookingApiToken
        A valid token to access the api
    .EXAMPLE
        Get-BBUserRole -BrightBookingApiUrl "https://t1b.gobright.cloud/" -BrightBookingApiToken "[already retreived api token]"
    .LINK
        https://support.gobright.com/
    #>


    [CmdletBinding()]
    Param(
      [Parameter(Mandatory=$True)]
       [string]$BrightBookingApiUrl,

      [Parameter(Mandatory=$True)]
       [string]$BrightBookingApiToken
    )
    Process {
        $pathandquery = "/api/user-roles/sync";

        $resturi = [System.Uri]::new([System.Uri]::new($BrightBookingApiUrl), $pathandquery)
        
        $hdrs = @{}
        $hdrs.Add("Authorization", "Bearer "+ $BrightBookingApiToken)

        Try
        {
            $response = Invoke-WebRequest -TimeoutSec 1200 -Uri $resturi -Method Get -Headers $hdrs -UseBasicParsing
        
            If ($response.StatusCode -eq 200 -or $response.StatusCode -eq 201)
            {
                $jsonresponse = $response.Content | ConvertFrom-Json
            } else {
                throw "Failed to get user sync status from the API. (statuscode: $response.StatusCode)"
            }
        }
        Catch 
        {
            $statusCode = $_.Exception.Response.StatusCode.Value__
            $responseText = $_
                        
            Try
            {
                $jsonresponse = $responseText | ConvertFrom-Json
                If ($jsonresponse.SyncRoot)
                {
                    $statusMessage = $jsonresponse.SyncRoot
                }
                Else
                {
                    $statusMessage = $responseText
                }
            } 
            Catch 
            {
                $statusMessage = $responseText
            }
            
            throw "Failed to get user sync status from the API. (statuscode: $statusCode, message: $statusMessage)"
        }
        
        return $jsonresponse
    }
}