Public/Send-ADUsersToBB.ps1

Function Send-ADUsersToBB {
    <#
    .SYNOPSIS
        Get the user information from the pipeline that is already converted to a GoBright BrightBooking object, and send it to the GoBright BrightBooking system
    .DESCRIPTION
        The pipeline should contain the converted user information
    .PARAMETER BrightBookingApiUrl
        Address of the GoBright BrightBooking API, e.g.: https://eu1.api.brightbooking.eu/
    .PARAMETER BrightBookingApiKey
        API key of the user to use to process the import
    .PARAMETER BrightBookingIntegrationName
        Name of the integration to link the users to
    .EXAMPLE
        Get-ADUsersForBB -Filter * | Convert-ADUsersToBBUserExport | Send-ADUsersToBB -BrightBookingApiUrl "https://eu1.api.brightbooking.eu/" -BrightBookingApiKey "[your api key]" -BrightBookingIntegrationName "Office 365"
        # Get all users in the Active Directory and let GoBright BrightBooking process it directly
    .EXAMPLE
        Get-ADUsersForBB -Filter * -SearchBase "OU=Office,DC=Company,DC=com" | Convert-ADUsersToBBUserExport | Send-ADUsersToBB -BrightBookingApiUrl "https://eu1.api.brightbooking.eu/" -BrightBookingApiKey "[your api key]" -BrightBookingIntegrationName "Office 365"
        # Get the users in the Active Directory, which are member of the given group and let GoBright BrightBooking process it directly
    .EXAMPLE
        Get-ADUsersForBB -Filter { memberOf -RecursiveMatch "CN=Administrators,DC=Company,DC=com" } -SearchBase "OU=Office,DC=Company,DC=com" | Convert-ADUsersToBBUserExport -ADUserPincodePropertyName PersonnelNumber | Send-ADUsersToBB -BrightBookingApiUrl "https://eu1.api.brightbooking.eu/" -BrightBookingApiKey "[your api key]" -BrightBookingIntegrationName "Office 365"
        # Get the users in the Active Directory, which in the specified SearchBase path, and use the custom property 'PersonnelNumber' as pincode and let GoBright BrightBooking process it directly
    .LINK
        https://support.gobright.com/
    #>


    [CmdletBinding()]
    Param(
      [Parameter(Mandatory=$True,ValueFromPipeline=$True)]
       [System.Object[]]$pipelineConvertedADUsers,

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

      [Parameter(Mandatory=$True)]
       [string]$BrightBookingApiKey,

      [Parameter(Mandatory=$True)]
       [string]$BrightBookingIntegrationName
    )

    Process {
        Write-Output "Loading..."
        
        # generate the csv file contents, generate to string
        $csvtext = $pipelineConvertedADUsers | ConvertTo-Csv -NoTypeInformation | Out-String
        
        # convert the text so the encoding is correct to handle special characters
        $csvtext = [System.Text.Encoding]::UTF8.GetBytes($csvtext)        
        
        Write-Output "Connecting to the GoBright BrightBooking API..."
        
        # post to the api
        $access_token = Get-BBAccessToken -BrightBookingApiUrl $BrightBookingApiUrl -BrightBookingApiKey $BrightBookingApiKey

        $resturi = [System.Uri]::new([System.Uri]::new($BrightBookingApiUrl), "/api/users/synchronize-direct-csv")                

        $QueryParameters = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
        $QueryParameters['integrationname'] = $BrightBookingIntegrationName
                
        $Request  = [System.UriBuilder]($resturi)
        $Request.Query = $QueryParameters.ToString()

        $body = $csvtext

        $hdrs = @{}
        $hdrs.Add("Authorization", "Bearer "+ $access_token)
        
        Try
        {
            Write-Output "Processing users in GoBright BrightBooking (this might take a while)..."    
            Write-Output ""
            
            $response = Invoke-WebRequest -Uri $Request.Uri -Method Post -Body $body -ContentType 'application/text' -Headers $hdrs
        
            If ($response.StatusCode -eq 200 -or $response.StatusCode -eq 201)
            {
                Write-Output "Finished synchronizing users to GoBright BrightBooking successfully"
            }
            Else
            {
                Write-Output "Finished synchronizing users to GoBright BrightBooking with unexpected statuscode $response.StatusCode"
            }
        } 
        Catch 
        {
            $statusCode = $_.Exception.Response.StatusCode.Value__
            $responseText = $_.ErrorDetails.Message
            
            Try
            {
                If ($responseText)
                {
                    $jsonresponse = $responseText | ConvertFrom-Json
                    If ($jsonresponse.SyncRoot)
                    {
                        $statusMessage = $jsonresponse.SyncRoot | Select-Object * | Format-List | Out-String
                    }
                    Else
                    {
                        $statusMessage = $responseText
                    }
                }
            } 
            Catch 
            {
                $statusMessage = $responseText
            }
            
            Write-Output "Synchronizing users to GoBright BrightBooking failed, please check the user data you have selected from your AD"
            Write-Output "Statuscode: $statusCode"
            If ($statusMessage)
            {
                Write-Output "Statusdetails:"
                $statusMessage = $statusMessage.Trim()
                Write-Output $statusMessage
            }
            
            throw "Synchronizing users failed"
        }
    }
}