Public/Get-BBUserSyncStatus.ps1

Function Get-BBUserSyncStatus {
    <#
    .SYNOPSIS
        Get the status of the sync to GoBright API
    .DESCRIPTION
        Get the status of the sync to 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-BBUserSyncStatus -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,

      [Parameter(Mandatory=$False)]
       [bool]$IncludeLastSyncResult,

      [Parameter(Mandatory=$False)]
       [bool]$ValidateCanStartNew
    )
    Process {
        $pathandquery = "/api/users/batch-import-csv/status";
        if ($IncludeLastSyncResult)
        {
            $pathandquery = $pathandquery +"?includelastresultdetails=true";
        }

        $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)"
        }

        # call succeeded, validate other steps if requested
        if ($ValidateCanStartNew)
        {
            if (!$jsonresponse.IsPossibleToStartNew) {
                $nextCallAllowedAt = $jsonresponse.NextCallAllowedAt

                if ($jsonresponse.IsCurrentlyProcessing) {
                    throw "Unable to start synchronisation now, processing is already busy"
                } else {
                    throw "Unable to start synchronisation now, next allowed from: $nextCallAllowedAt"
                }
            }
        }
        return $jsonresponse
    }
}