functions/public/New-GroupAuthorization.ps1

function New-GroupAuthorization {
    param (
        [Parameter(Mandatory = $true)] [string] $authUrl,
        [Parameter(Mandatory = $true)] $body,
        [Parameter(Mandatory = $true)] [string] $accessToken
    )

    $url = "$($authUrl.TrimEnd("/"))/groups"

    if (!($body -is [string])) {
        $clientObject = $body
        $body = $body | ConvertTo-Json
    }
    else {
        $clientObject = ConvertFrom-Json -InputObject $body
    }

    $headers = @{"Accept" = "application/json"}
    if ($accessToken) {
        $headers.Add("Authorization", "Bearer $accessToken")
    }

    # attempt to add
    try {
        $response = Invoke-RestMethod -Method Post -Uri ([System.Uri]::EscapeUriString($url)) -Body $body -ContentType "application/json" -Headers $headers
        Write-DosMessage -Level "Information" -Message """$($clientObject.groupName)"" has been added as a new group"
        return $response
    }
    catch {
        $exception = $_.Exception
        if ((Assert-WebExceptionType -exception $exception -typeCode 409)) {
            $url = "$($url)/$($clientObject.groupName)?"
            if ($clientObject.identityProvider) {
                $url = "$($url)identityProvider=$($clientObject.identityProvider)&"
            }
            if ($clientObject.tenantId) {
                $url = "$($url)tenantId=$($clientObject.tenantId)&"
            }
            $url = "$($url.TrimEnd("?").TrimEnd("&"))"

            $response = Invoke-RestMethod -Method Get -Uri ([System.Uri]::EscapeUriString($url)) -ContentType "application/json" -Headers $headers
            Write-DosMessage -Level "Information" -Message """$($clientObject.groupName)"" group has already been created"
            return $response
        }
        else {
            $error = "Unknown error attempting to post"
            $exception = $_.Exception
            if ($null -ne $exception -and $null -ne $exception.Response) {
                $error = Get-ErrorFromResponse -response $exception.Response
            }
            throw ( New-Object -TypeName "System.Net.WebException" "There was an error creating group ""$($clientObject.groupName)"": $error, halting installation.", $exception)
        }
    }
}