PowerFederatedDirectory.psm1

function Join-UriQuery { 
    <#
    .SYNOPSIS
    Provides ability to join two Url paths together including advanced querying
 
    .DESCRIPTION
    Provides ability to join two Url paths together including advanced querying which is useful for RestAPI/GraphApi calls
 
    .PARAMETER BaseUri
    Primary Url to merge
 
    .PARAMETER RelativeOrAbsoluteUri
    Additional path to merge with primary url (optional)
 
    .PARAMETER QueryParameter
    Parameters and their values in form of hashtable
 
    .EXAMPLE
    Join-UriQuery -BaseUri 'https://evotec.xyz/' -RelativeOrAbsoluteUri '/wp-json/wp/v2/posts' -QueryParameter @{
        page = 1
        per_page = 20
        search = 'SearchString'
    }
 
    .EXAMPLE
    Join-UriQuery -BaseUri 'https://evotec.xyz/wp-json/wp/v2/posts' -QueryParameter @{
        page = 1
        per_page = 20
        search = 'SearchString'
    }
 
    .EXAMPLE
    Join-UriQuery -BaseUri 'https://evotec.xyz' -RelativeOrAbsoluteUri '/wp-json/wp/v2/posts'
 
    .NOTES
    General notes
    #>

    [alias('Join-UrlQuery')]
    [CmdletBinding()]
    param ([parameter(Mandatory)][uri] $BaseUri,
        [parameter(Mandatory = $false)][uri] $RelativeOrAbsoluteUri,
        [Parameter()][System.Collections.IDictionary] $QueryParameter)
    if ($BaseUri -and $RelativeOrAbsoluteUri) { $Url = Join-Uri -BaseUri $BaseUri -RelativeOrAbsoluteUri $RelativeOrAbsoluteUri } else { $Url = $BaseUri }
    if ($QueryParameter) {
        $Collection = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
        foreach ($key in $QueryParameter.Keys) { $Collection.Add($key, $QueryParameter.$key) }
    }
    $uriRequest = [System.UriBuilder] $Url
    if ($Collection) { $uriRequest.Query = $Collection.ToString() }
    return $uriRequest.Uri.AbsoluteUri
}
function Remove-EmptyValue { 
    [alias('Remove-EmptyValues')]
    [CmdletBinding()]
    param([alias('Splat', 'IDictionary')][Parameter(Mandatory)][System.Collections.IDictionary] $Hashtable,
        [string[]] $ExcludeParameter,
        [switch] $Recursive,
        [int] $Rerun,
        [switch] $DoNotRemoveNull,
        [switch] $DoNotRemoveEmpty,
        [switch] $DoNotRemoveEmptyArray,
        [switch] $DoNotRemoveEmptyDictionary)
    foreach ($Key in [string[]] $Hashtable.Keys) { if ($Key -notin $ExcludeParameter) { if ($Recursive) { if ($Hashtable[$Key] -is [System.Collections.IDictionary]) { if ($Hashtable[$Key].Count -eq 0) { if (-not $DoNotRemoveEmptyDictionary) { $Hashtable.Remove($Key) } } else { Remove-EmptyValue -Hashtable $Hashtable[$Key] -Recursive:$Recursive } } else { if (-not $DoNotRemoveNull -and $null -eq $Hashtable[$Key]) { $Hashtable.Remove($Key) } elseif (-not $DoNotRemoveEmpty -and $Hashtable[$Key] -is [string] -and $Hashtable[$Key] -eq '') { $Hashtable.Remove($Key) } elseif (-not $DoNotRemoveEmptyArray -and $Hashtable[$Key] -is [System.Collections.IList] -and $Hashtable[$Key].Count -eq 0) { $Hashtable.Remove($Key) } } } else { if (-not $DoNotRemoveNull -and $null -eq $Hashtable[$Key]) { $Hashtable.Remove($Key) } elseif (-not $DoNotRemoveEmpty -and $Hashtable[$Key] -is [string] -and $Hashtable[$Key] -eq '') { $Hashtable.Remove($Key) } elseif (-not $DoNotRemoveEmptyArray -and $Hashtable[$Key] -is [System.Collections.IList] -and $Hashtable[$Key].Count -eq 0) { $Hashtable.Remove($Key) } } } }
    if ($Rerun) { for ($i = 0; $i -lt $Rerun; $i++) { Remove-EmptyValue -Hashtable $Hashtable -Recursive:$Recursive } }
}
function Join-Uri { 
    <#
    .SYNOPSIS
    Provides ability to join two Url paths together
 
    .DESCRIPTION
    Provides ability to join two Url paths together
 
    .PARAMETER BaseUri
    Primary Url to merge
 
    .PARAMETER RelativeOrAbsoluteUri
    Additional path to merge with primary url
 
    .EXAMPLE
    Join-Uri 'https://evotec.xyz/' '/wp-json/wp/v2/posts'
 
    .EXAMPLE
    Join-Uri 'https://evotec.xyz/' 'wp-json/wp/v2/posts'
 
    .EXAMPLE
    Join-Uri -BaseUri 'https://evotec.xyz/' -RelativeOrAbsoluteUri '/wp-json/wp/v2/posts'
 
    .EXAMPLE
    Join-Uri -BaseUri 'https://evotec.xyz/test/' -RelativeOrAbsoluteUri '/wp-json/wp/v2/posts'
 
    .NOTES
    General notes
    #>

    [alias('Join-Url')]
    [cmdletBinding()]
    param([parameter(Mandatory)][uri] $BaseUri,
        [parameter(Mandatory)][uri] $RelativeOrAbsoluteUri)
    return ($BaseUri.OriginalString.TrimEnd('/') + "/" + $RelativeOrAbsoluteUri.OriginalString.TrimStart('/'))
}
function Convert-FederatedUser {
    [cmdletBinding()]
    param(
        [Array] $Users
    )

    foreach ($FederatedUser in $Users) {
        $WorkEmail = $null
        $HomeEmail = $null
        $WorkPhone = $null
        $MobilePhone = $null
        $HomePhone = $null

        $Addresses = $FederatedUser.'addresses'
        foreach ($Address in $Addresses) {
            if ($Address.'type' -eq 'work') {
                $streetAddress = $Address.streetAddress
                $postalCode = $Address.PostalCode
                $city = $Address.Locality
                $region = $Address.region
                $country = $Address.country
            } elseif ($Address.'type' -eq 'home') {
                $HomeStreetAddress = $Address.streetAddress
                $HomePostalCode = $Address.PostalCode
                $HomeCity = $Address.Locality
                $HomeRegion = $Address.region
                $HomeCountry = $Address.country
            }
        }
        $Emails = $FederatedUser.'emails'
        foreach ($Email in $Emails) {
            if ($Email.Type -eq 'work') {
                $WorkEmail = $Email.Value
            } elseif ($Email.Type -eq 'home') {
                $HomeEmail = $Email.Value
            }
        }
        $PhoneNumbers = $FederatedUser.'phoneNumbers'
        foreach ($Phone in $PhoneNumbers) {
            if ($Phone.Type -eq 'work') {
                $WorkPhone = $Phone.Value
            } elseif ($Phone.Type -eq 'mobile') {
                $MobilePhone = $Phone.Value
            } elseif ($Phone.Type -eq 'home') {
                $HomePhone = $Phone.Value
            }
        }

        $CompanyLogoUrl = $null
        $CompanyThumbnailUrl = $null
        $CompanyLogos = $FederatedUser.'urn:ietf:params:scim:schemas:extension:fd:2.0:User:companyLogos'
        foreach ($C in $CompanyLogos) {
            if ($Type -eq 'logo') {
                $CompanyLogoUrl = $C.Value
            } elseif ($Type -eq 'thumbnail') {
                $CompanyThumbnailUrl = $C.Value
            }
        }

        $PhotoUrl = $null
        $ThumbnailUrl = $null
        $Photos = $FederatedUser.'photos'
        foreach ($C in $Photos) {
            if ($Type -eq 'photo') {
                $PhotoUrl = $C.Value
            } elseif ($Type -eq 'thumbnail') {
                $ThumbnailUrl = $C.Value
            }
        }

        [PSCustomObject] @{
            Id                  = $FederatedUser.'id'
            ExternalId          = $FederatedUser.'externalId'
            UserName            = $FederatedUser.'userName'
            GivenName           = $FederatedUser.'name'.'givenName'
            FamilyName          = $FederatedUser.'name'.'familyName'
            DisplayName         = $FederatedUser.'displayName'
            NickName            = $FederatedUser.'nickName'
            ProfileUrl          = $FederatedUser.'profileUrl'
            Title               = $FederatedUser.'title'
            UserType            = $FederatedUser.'userType'
            EmailAddress        = $WorkEmail
            EmailAddressHome    = $HomeEmail
            PhoneNumberWork     = $WorkPhone
            PhoneNumberMobile   = $MobilePhone
            PhoneNumberHome     = $HomePhone

            StreetAddress       = $streetAddress
            City                = $City
            Region              = $region
            PostalCode          = $postalCode
            Country             = $country

            StreetAddressHome   = $HomeStreetAddress
            CityHome            = $HomeCity
            RegionHome          = $HomeRegion
            PostalCodeHome      = $HomePostalCode
            CountryHome         = $HomeCountry

            PreferredLanguage   = $FederatedUser.'preferredLanguage'

            Locale              = $FederatedUser.'locale'
            TimeZone            = $FederatedUser.'timezone'
            Active              = $FederatedUser.'active'
            Groups              = $FederatedUser.'groups'
            Roles               = $FederatedUser.'roles'.value
            Organization        = $FederatedUser.'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User'.'organization'
            EmployeeNumber      = $FederatedUser.'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User'.'employeeNumber'
            CostCenter          = $FederatedUser.'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User'.'costCenter'
            Division            = $FederatedUser.'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User'.'division'
            Department          = $FederatedUser.'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User'.'department'
            Manager             = $FederatedUser.'urn:ietf:params:scim:schemas:extension:enterprise:2.0User'.'manager'
            Description         = $FederatedUser.'urn:ietf:params:scim:schemas:extension:fd:2.0:User'.'description'
            DirectoryId         = $FederatedUser.'urn:ietf:params:scim:schemas:extension:fd:2.0:User'.'directoryId'
            CompanyId           = $FederatedUser.'urn:ietf:params:scim:schemas:extension:fd:2.0:User'.'companyId'

            CompanyLogoUrl      = $CompanyLogoUrl
            CompanyThumbnailUrl = $CompanyThumbnailUrl

            PhotoUrl            = $PhotoUrl
            ThumbnailUrl        = $ThumbnailUrl

            Password            = $FederatedUser.Password
            ManagerID           = $FederatedUser.'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User'.'manager'.'value'
            #ManagerUserName = $FederatedUser.'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:manager'.'displayName'
            #ManagerReference = $FederatedUser.'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User'.'manager'.'$ref'
            ManagerDisplayName  = $FederatedUser.'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User'.'manager'.'displayName'
            Role                = $FederatedUser.'roles'.value
            Custom01            = $FederatedUser.'urn:ietf:params:scim:schemas:extension:fd:2.0:User'.'custom01'
            Custom02            = $FederatedUser.'urn:ietf:params:scim:schemas:extension:fd:2.0:User'.'custom02'
            Custom03            = $FederatedUser.'urn:ietf:params:scim:schemas:extension:fd:2.0:User'.'custom03'

            ResourceType        = $FederatedUser.Meta.ResourceType
            Created             = $FederatedUser.Meta.Created
            LastModified        = $FederatedUser.Meta.LastModified
            Location            = $FederatedUser.Meta.location
        }
    }
}
function Add-FederatedDirectoryUser {
    [alias('Add-FDUser')]
    [CmdletBinding(SupportsShouldProcess)]
    param(
        [System.Collections.IDictionary] $Authorization,
        [string] $ExternalId,
        [parameter()][string] $DirectoryID,
        [parameter(Mandatory)][string] $UserName,
        [Alias('FirstName')] $FamilyName,
        [string] $GivenName,
        [parameter(Mandatory)][string] $DisplayName,
        [string] $NickName,
        [string] $ProfileUrl,
        [string] $EmailAddress,
        [string] $EmailAddressHome,
        [string] $StreetAddress,
        [string] $City,
        [string] $Region,
        [string] $PostalCode,
        [string] $Country,
        [string] $StreetAddressHome,
        [string] $PostalCodeHome,
        [string] $CityHome,
        [string] $RegionHome,
        [string] $CountryHome,
        [string] $PhoneNumberWork,
        [string] $PhoneNumberHome,
        [string] $PhoneNumberMobile,
        [string] $PhotoUrl,
        [string] $ThumbnailUrl,
        [string] $CompanyID,
        # [string] $CompanyLogoUrl,
        # [string] $CompanyThumbnailUrl,
        [string] $PreferredLanguage,
        [string] $Locale,
        [string] $TimeZone,
        [string] $Title,
        [string] $UserType,
        [string] $Password,
        [string] $ManagerID,
        [string] $ManagerUserName,
        [string] $ManagerDisplayName,
        [switch] $Active,
        [string] $Department,
        [string] $EmployeeNumber,
        [string] $CostCenter,
        [string] $Division,
        [string] $Description,
        [ValidateSet('admin', 'user')][string] $Role = 'user',
        [alias('CustomAttribute01')][string] $Custom01,
        [alias('CustomAttribute02')][string] $Custom02,
        [alias('CustomAttribute03')][string] $Custom03,
        [switch] $Suppress,
        [switch] $BulkProcessing
    )

    if (-not $Authorization) {
        if ($Script:AuthorizationCacheFD) {
            $Authorization = $Script:AuthorizationCacheFD[0]
        }
        if (-not $Authorization) {
            if ($PSBoundParameters.ErrorAction -eq 'Stop') {
                throw "No authorization found. Please run 'Connect-FederatedDirectory' first."
            } else {
                Write-Warning -Message "Add-FederatedDirectoryUser - No authorization found. Please run 'Connect-FederatedDirectory' first."
                return
            }
        }
    }
    if ($Authorization) {
        if ($ManagerUserName) {
            $ManagerID = (Get-FederatedDirectoryUser -Authorization $Authorization -UserName $ManagerUserName).Id
        }

        $Body = [ordered] @{
            schemas                                                      = @(
                "urn:ietf:params:scim:schemas:core:2.0:User"
                "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
                "urn:ietf:params:scim:schemas:extension:fd:2.0:User"
            )
            "externalId"                                                 = $ExternalId
            "userName"                                                   = $UserName
            "name"                                                       = [ordered] @{
                "familyName" = $FamilyName
                "givenName"  = $GivenName
            }
            "displayName"                                                = $DisplayName
            "nickName"                                                   = $NickName
            "profileUrl"                                                 = $ProfileUrl
            "emails"                                                     = @(
                if ($EmailAddress) {
                    @{
                        "value"   = $EmailAddress
                        "type"    = "work"
                        "primary" = $true
                    }
                }
                if ($EmailAddressHome) {
                    @{
                        "value" = $EmailAddressHome
                        "type"  = "home"
                    }
                }
            )
            "addresses"                                                  = @(
                if ($StreetAddress -or $City -or $Region -or $PostalCode -or $Country) {
                    @{
                        "streetAddress" = $StreetAddress
                        "locality"      = $City
                        "region"        = $Region
                        "postalCode"    = $PostalCode
                        "country"       = $Country
                        "type"          = "work"
                        "primary"       = $true
                    }
                }
                if ($StreetAddressHome -or $CityHome -or $RegionHome -or $PostalCodeHome -or $CountryHome) {
                    @{
                        "streetAddress" = $StreetAddressHome
                        "locality"      = $CityHome
                        "region"        = $RegionHome
                        "postalCode"    = $PostalCodeHome
                        "country"       = $CountryHome
                        "type"          = "home"
                    }
                }
            )
            "phoneNumbers"                                               = @(
                if ($PhoneNumberWork) {
                    @{
                        "value"   = $PhoneNumberWork
                        "type"    = "work"
                        "primary" = $true
                    }
                }
                if ($PhoneNumberHome) {
                    @{
                        "value" = $PhoneNumberHome
                        "type"  = "home"
                    }
                }
                if ($PhoneNumberMobile) {
                    @{
                        "value" = $PhoneNumberMobile
                        "type"  = "mobile"
                    }
                }
            )
            "photos"                                                     = @(
                if ($PhotoUrl) {
                    @{
                        "value" = $PhotoUrl
                        "type"  = "photo"
                    }
                }
                if ($ThumbnailUrl) {
                    @{
                        "value" = $ThumbnailUrl
                        "type"  = "thumbnail"
                    }
                }
            )
            "password"                                                   = $Password
            "preferredLanguage"                                          = $PreferredLanguage
            "locale"                                                     = $Locale
            "timeZone"                                                   = $TimeZone
            "userType"                                                   = $UserType
            "title"                                                      = $Title
            "active"                                                     = if ($PSBoundParameters.Keys -contains ('Active')) { $Active.IsPresent } else { $Null }
            "roles"                                                      = @(
                @{
                    "value"   = $Role
                    "display" = $Role
                }
            )
            "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User" = [ordered] @{
                #"organization" = $Organization # read only...
                "department"     = $Department
                "employeeNumber" = $EmployeeNumber
                "costCenter"     = $CostCenter
                "division"       = $Division
                "manager"        = @{
                    "displayName" = $ManagerDisplayName
                    "value"       = $ManagerID
                    #"`$ref" = $ManagerReference # readonly
                }
            }
            "urn:ietf:params:scim:schemas:extension:fd:2.0:User"         = [ordered] @{
                "description" = $Description
                "companyId"   = $CompanyId
                # "companyLogos" = @(
                # if ($CompanyLogoUrl) {
                # @{
                # "value" = $CompanyLogoUrl
                # "type" = "logo"
                # }
                # }
                # if ($CompanyThumbnailUrl) {
                # @{
                # "value" = $CompanyThumbnailUrl
                # "type" = "thumbnail"
                # }
                # }
                # )
                #'directoryId' = $DirectoryID
                'custom01'    = $Custom01
                'custom02'    = $Custom02
                'custom03'    = $Custom03
            }
        }

        Try {
            Remove-EmptyValue -Hashtable $Body -Recursive -Rerun 2

            if ($BulkProcessing) {
                # Return body is used for using Invoke-FederatedDirectory to add/set/remove users in bulk
                return [ordered] @{
                    data   = $Body
                    method = 'POST'
                    bulkid = $Body.userName
                }
            }
            $invokeRestMethodSplat = [ordered] @{
                Method      = 'POST'
                Uri         = 'https://api.federated.directory/v2/Users'
                Headers     = [ordered]  @{
                    'Content-Type'  = 'application/json; charset=utf-8'
                    'Authorization' = $Authorization.Authorization
                    'Cache-Control' = 'no-cache'
                }
                Body        = $Body | ConvertTo-Json -Depth 10
                ErrorAction = 'Stop'
            }
            if ($DirectoryID) {
                $invokeRestMethodSplat['Headers']['directoryId'] = $DirectoryID
            }
            # for troubleshooting
            if ($VerbosePreference -eq 'Continue') {
                $Body | ConvertTo-Json -Depth 10 | Write-Verbose
            }
            if ($PSCmdlet.ShouldProcess("username $UserName, displayname $DisplayName", "Adding user")) {
                $ReturnData = Invoke-RestMethod @invokeRestMethodSplat -Verbose:$false
                # don't return data as we trust it's been created
                if (-not $Suppress) {
                    $ReturnData
                }
            }

            # # for troubleshooting
            # if ($VerbosePreference -eq 'Continue') {
            # $invokeRestMethodSplat.Remove('body')
            # $invokeRestMethodSplat | ConvertTo-Json -Depth 10 | Write-Verbose
            # }
        } catch {
            if ($PSBoundParameters.ErrorAction -eq 'Stop') {
                throw
            } else {
                $ErrorDetails = $_.ErrorDetails.Message | ConvertFrom-Json -ErrorAction SilentlyContinue
                if ($ErrorDetails.Detail -like '*already exists*directory*') {
                    Write-Warning -Message "Add-FederatedDirectoryUser - $($ErrorDetails.Detail) [UserName: $UserName / DisplayName: $DisplayName]"
                } else {
                    Write-Warning -Message "Add-FederatedDirectoryUser - Error $($_.Exception.Message), $($ErrorDetails.Detail)"
                }
            }
        }
    } else {
        Write-Warning -Message 'Add-FederatedDirectoryUser - No authorization found. Please make sure to use Connect-FederatedDirectory first.'
    }
}
function Connect-FederatedDirectory {
    <#
    .SYNOPSIS
    Connects to a federated directory.
 
    .DESCRIPTION
    Connects to a federated directory.
 
    .PARAMETER Token
    The token to use for authentication to the federated directory from New-JWT command. This is the default.
 
    .PARAMETER TokenEncrypted
    The encrypted token to use for authentication to the federated directory from New-JWT command.
 
    .PARAMETER ExpiresTimeout
    The number of seconds before the token expires.
 
    .PARAMETER ForceRefresh
    Forces a refresh of the authentication
 
    .PARAMETER Suppress
    Suppresses the output of the command. By default the command will output the connection information.
 
    .EXAMPLE
    $Token = 'TokenInformation'
    Connect-FederatedDirectory -Token $Token -Suppress
 
    .NOTES
    General notes
    #>

    [alias('Connect-FD')]
    [cmdletbinding(DefaultParameterSetName = 'ClearText')]
    param(
        [Parameter(Mandatory, ParameterSetName = 'ClearText')]
        [alias('ApplicationSecret', 'ApplicationKey')]
        [string] $Token,

        [Parameter(Mandatory, ParameterSetName = 'Encrypted')]
        [alias('ApplicationSecretEncrypted', 'ApplicationKeyEncrypted')]
        [string] $TokenEncrypted,

        [int] $ExpiresTimeout = 30,
        [switch] $ForceRefresh,
        [switch] $Suppress
    )
    if (-not $Script:AuthorizationCacheFD) {
        $Script:AuthorizationCacheFD = [ordered] @{}
    }

    if ($TokenEncrypted) {
        try {
            $ApplicationKeyTemp = $ClientSecretEncrypted | ConvertTo-SecureString -ErrorAction Stop
        } catch {
            if ($PSBoundParameters.ErrorAction -eq 'Stop') {
                throw
            } else {
                $ErrorMessage = $_.Exception.Message -replace "`n", " " -replace "`r", " "
                Write-Warning -Message "Connect-ILM - Error: $ErrorMessage"
                return
            }
        }
        $ApplicationKey = [System.Net.NetworkCredential]::new([string]::Empty, $ApplicationKeyTemp).Password
    } else {
        $ApplicationKey = $Token
    }

    $ShortKey = $ApplicationKey.Trim(15)

    $RestSplat = @{
        ErrorAction = 'Stop'
        Method      = 'POST'
        Body        = @{
            "grant_type" = "urn:ietf:params:oauth:grant-type:jwt-bearer"
            "assertion"  = $ApplicationKey
        }
        Uri         = 'https://api.federated.directory/v2/Login/Oauth2/Token'
    }

    if ($Script:AuthorizationCacheFD[$ShortKey] -and -not $ForceRefesh) {
        if ($Script:AuthorizationCacheFD[$ShortKey].ExpiresOn -gt [datetime]::UtcNow) {
            Write-Verbose "Connect-FederatedDirectory - Using cache for $ShortKey..."
            if (-not $Suppress) {
                return $Script:AuthorizationCacheFD[$ShortKey]
            }
        }
    }

    try {
        $Authorization = Invoke-RestMethod @RestSplat
        $Key = [ordered] @{
            'Authorization' = "$($Authorization.token_type) $($Authorization.access_token)"
            'Extended'      = $Authorization
            'Error'         = ''
            'ExpiresOn'     = ([datetime]::UtcNow).AddSeconds($Authorization.expires_in - $ExpiresTimeout)
            'Splat'         = [ordered] @{
                Token = $RestSplat['Body']['assertion']
            }
            'Platform'      = $Platform
        }
        $Script:AuthorizationCacheFD[$ShortKey] = $Key
    } catch {
        if ($PSBoundParameters.ErrorAction -eq 'Stop') {
            throw
        } else {
            $ErrorMessage = $_.Exception.Message -replace "`n", " " -replace "`r", " "
            Write-Warning -Message "Connect-ILM - Error: $ErrorMessage"
            $Key = [ordered] @{
                'Authorization' = $Null
                'Extended'      = $Null
                'Error'         = $ErrorMessage
                'ExpiresOn'     = $null
                'Splat'         = [ordered] @{
                    Token = $RestSplat['Body']['assertion']
                }
                'Platform'      = $Platform
            }
        }
    }
    if (-not $Suppress) {
        $Key
    }
}
function Get-FederatedDirectorySchema {
    <#
    .SYNOPSIS
    Get the schema of a federated directory.
 
    .DESCRIPTION
    Get the schema of a federated directory.
 
    .EXAMPLE
    $Schema = Get-FederatedDirectorySchema
    $Schema | Where-Object { $_.Name -eq 'User' } | Select-Object -ExpandProperty Attributes | Format-Table
    $Schema | Where-Object { $_.Name -eq 'EnterpriseUser' } | Select-Object -ExpandProperty Attributes | Format-Table
    $Schema | Where-Object { $_.Name -eq 'FederatedDirectoryUser' } | Select-Object -ExpandProperty Attributes | Format-Table
 
    .NOTES
    General notes
    #>

    [alias('Get-FDSchema')]
    [cmdletbinding()]
    param()

    $BaseUri = "https://api.federated.directory/v2/Schemas"

    Write-Verbose -Message "Get-FederatedDirectorySchema - Using query: $BaseUri"

    $Headers = @{
        'Content-Type' = 'application/json'
    }

    Try {
        $BatchObjects = Invoke-RestMethod -Method Get -Uri $BaseUri -Headers $Headers -ErrorAction Stop
        $BatchObjects.Resources
    } catch {
        if ($PSBoundParameters.ErrorAction -eq 'Stop') {
            throw
        } else {
            $ErrorDetails = $_.ErrorDetails.Message | ConvertFrom-Json -ErrorAction SilentlyContinue
            Write-Warning -Message "Get-FederatedDirectorySchema - Error $($_.Exception.Message), $($ErrorDetails.Detail)"
        }
    }
}
function Get-FederatedDirectoryUser {
    [alias('Get-FDUser')]
    [cmdletbinding()]
    param(
        [System.Collections.IDictionary] $Authorization,
        [string] $Id,
        [Alias('SearchUserName')][string] $UserName,
        [string] $DirectoryID,
        [int] $MaxResults,
        [int] $StartIndex = 1,
        [int] $Count = 50,
        [string] $Filter,
        [ValidateSet(
            'id',
            'externalId',
            'userName',
            'givenName',
            'familyName',
            'displayName',
            'nickName',
            'profileUrl',
            'title',
            'userType',
            'emails',
            'phoneNumbers',
            'addresses',
            'preferredLanguage',
            'locale',
            'timezone',
            'active',
            'groups',
            'roles',
            'meta',
            'organization',
            'employeeNumber',
            'costCenter',
            'division',
            'department',
            'manager',
            'description',
            'directoryId',
            'companyId',
            'companyLogos',
            'custom01',
            'custom02',
            'custom03'
        )]
        [string] $SortBy,
        [ValidateSet('ascending', 'descending')][string] $SortOrder,
        [Alias('Property')]
        [ValidateSet(
            'id',
            'externalId',
            'userName',
            'givenName',
            'familyName',
            'displayName',
            'nickName',
            'profileUrl',
            'title',
            'userType',
            'emails',
            'phoneNumbers',
            'addresses',
            'preferredLanguage',
            'locale',
            'timezone',
            'active',
            'groups',
            'roles',
            'meta',
            'organization',
            'employeeNumber',
            'costCenter',
            'division',
            'department',
            'manager',
            'description',
            'directoryId',
            'companyId',
            'companyLogos',
            'custom01',
            'custom02',
            'custom03'
        )]
        [string[]] $Attributes,
        [switch] $Native
    )

    $ConvertAttributes = @{
        'id'                = 'id'
        'externalId'        = 'externalId'
        'userName'          = 'userName'
        'givenName'         = 'name.givenName'
        'familyName'        = 'name.familyName'
        'displayName'       = 'displayName'
        'nickName'          = 'nickName'
        'profileUrl'        = 'profileUrl'
        'title'             = 'title'
        'userType'          = 'userType'
        'emails'            = 'emails'
        'phoneNumbers'      = 'phoneNumbers'
        'addresses'         = 'addresses'
        'preferredLanguage' = 'preferredLanguage'
        'locale'            = 'locale'
        'timezone'          = 'timezone'
        'active'            = 'active'
        'groups'            = 'groups'
        'roles'             = 'roles'
        'meta'              = 'meta'
        'organization'      = 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:organization'
        'employeeNumber'    = 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber'
        'costCenter'        = 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:costCenter'
        'division'          = 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:division'
        'department'        = 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:department'
        'manager'           = 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:manager'
        'description'       = 'urn:ietf:params:scim:schemas:extension:fd:2.0:User:description'
        'directoryId'       = 'urn:ietf:params:scim:schemas:extension:fd:2.0:User:directoryId'
        'companyId'         = 'urn:ietf:params:scim:schemas:extension:fd:2.0:User:companyId'
        'companyLogos'      = 'urn:ietf:params:scim:schemas:extension:fd:2.0:User:companyLogos'
        'custom01'          = 'urn:ietf:params:scim:schemas:extension:fd:2.0:User:custom01'
        'custom02'          = 'urn:ietf:params:scim:schemas:extension:fd:2.0:User:custom02'
        'custom03'          = 'urn:ietf:params:scim:schemas:extension:fd:2.0:User:custom03'
    }

    $AttributesConverted = foreach ($Attribute in $Attributes) {
        if ($ConvertAttributes[$Attribute]) {
            $ConvertAttributes[$Attribute]
        }
    }
    if ($SortBy) {
        $SortByConverted = $ConvertAttributes[$SortBy]
    }

    if (-not $Authorization) {
        if ($Script:AuthorizationCacheFD) {
            $Authorization = $Script:AuthorizationCacheFD[0]
        }
        if (-not $Authorization) {
            if ($PSBoundParameters.ErrorAction -eq 'Stop') {
                throw "No authorization found. Please run 'Connect-FederatedDirectory' first."
            } else {
                Write-Warning -Message "Get-FederatedDirectory - No authorization found. Please run 'Connect-FederatedDirectory' first."
                return
            }
        }
    }
    if ($Authorization) {
        if ($ID) {
            $BaseUri = "https://api.federated.directory/v2/Users/$ID"
        } else {
            $BaseUri = "https://api.federated.directory/v2/Users"
        }
        # Lets build up query
        $QueryParameter = [ordered] @{
            count      = if ($Count) { $Count } else { $null }
            startIndex = if ($StartIndex) { $StartIndex } else { $null }
            filter     = if ($UserName) {
                # keep in mind regardless of used operator it will always revert back to co as per API (weird)
                "userName co $UserName"
            } else {
                $Filter
            }
            sortBy     = $SortByConverted
            sortOrder  = $SortOrder
            attributes = $AttributesConverted -join ","
        }
        # lets remove empty values to remove whatever user hasn't requested
        Remove-EmptyValue -Hashtable $QueryParameter

        # Lets build our url
        $Uri = Join-UriQuery -BaseUri $BaseUri -QueryParameter $QueryParameter
        Write-Verbose -Message "Get-FederatedDirectoryUser - Using query: $Uri"

        $Headers = @{
            'Content-Type'  = 'application/json; charset=utf-8'
            'Authorization' = $Authorization.Authorization
            'directoryID'   = $DirectoryID
        }
        Remove-EmptyValue -Hashtable $Headers
        Try {
            $BatchObjects = Invoke-RestMethod -Method Get -Uri $Uri -Headers $Headers -ErrorAction Stop #{id}?attributes={attributes}'
        } catch {
            if ($PSBoundParameters.ErrorAction -eq 'Stop') {
                throw
            } else {
                $ErrorDetails = $_.ErrorDetails.Message | ConvertFrom-Json -ErrorAction SilentlyContinue
                if ($ErrorDetails.Detail -like '*already exists*directory*') {
                    Write-Warning -Message "Get-FederatedDirectoryUser - $($ErrorDetails.Detail) [UserName: $UserName / ID: $ID]"
                    return
                } else {
                    Write-Warning -Message "Get-FederatedDirectoryUser - Error $($_.Exception.Message), $($ErrorDetails.Detail)"
                    return
                }
            }
        }
        if ($BatchObjects.Resources) {
            Write-Verbose -Message "Get-FederatedDirectoryUser - Got $($BatchObjects.Resources.Count) users (StartIndex: $StartIndex, Count: $Count). Starting to process them."

            if ($MaxResults -gt 0 -and $BatchObjects.Resources.Count -ge $MaxResults) {
                # return users if amount of users available is more than we wanted
                if ($Native) {
                    $BatchObjects.Resources | Select-Object -First $MaxResults
                } else {
                    Convert-FederatedUser -Users ($BatchObjects.Resources | Select-Object -First $MaxResults)
                }
                $LimitReached = $true
            } else {
                # return all users that were given in a batch
                if ($Native) {
                    $BatchObjects.Resources
                } else {
                    Convert-FederatedUser -Users $BatchObjects.Resources
                }
            }
        } elseif ($BatchObjects.Schemas -and $BatchObjects.id) {
            if ($Native) {
                $BatchObjects
            } else {
                Convert-FederatedUser -Users $BatchObjects
            }
        } else {
            Write-Verbose "Get-FederatedDirectoryUser - No users found"
            return
        }
        if (-not $Count -and -not $StartIndex) {
            # paging is disabled, we don't do anything
        } elseif (-not $LimitReached -and $BatchObjects.TotalResults -gt $BatchObjects.StartIndex + $Count) {
            # lets get more users because there's more to get and user wanted more
            $MaxResults = $MaxResults - $BatchObjects.Resources.Count
            Write-Verbose "Get-FederatedDirectoryUser - Processing more pages (StartIndex: $StartIndex, Count: $Count)."
            $getFederatedDirectoryUserSplat = @{
                Authorization = $Authorization
                StartIndex    = $($BatchObjects.StartIndex + $Count)
                Count         = $Count
                MaxResults    = $MaxResults
                Filter        = $Filter
                SortBy        = $SortBy
                SortOrder     = $SortOrder
                Attributes    = $Attributes
                DirectoryID   = $DirectoryID
            }
            Remove-EmptyValue -Hashtable $getFederatedDirectoryUserSplat
            Get-FederatedDirectoryUser @getFederatedDirectoryUserSplat
        }
    } else {
        Write-Warning -Message 'Get-FederatedDirectoryUser - No authorization found. Please make sure to use Connect-FederatedDirectory first.'
    }
}

# $Script:AttributesList = @(
# 'id',
# 'externalId',
# 'userName',
# 'givenName',
# 'familyName',
# 'displayName',
# 'nickName',
# 'profileUrl',
# 'title',
# 'userType',
# 'emails',
# 'phoneNumbers',
# 'addresses',
# 'preferredLanguage',
# 'locale',
# 'timezone',
# 'active',
# 'groups',
# 'roles',
# 'meta',
# 'organization',
# 'employeeNumber',
# 'costCenter',
# 'division',
# 'department',
# 'manager',
# 'description',
# 'directoryId',
# 'companyId',
# 'companyLogos'
# 'custom01',
# 'custom02',
# 'custom03'
# )

# $Script:ScriptBlockAttributes = {
# param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
# $Script:AttributesList | Where-Object { $_ -like "*$wordToComplete*" }
# }

# Register-ArgumentCompleter -CommandName Get-FederatedDirectoryUser -ParameterName Attributes -ScriptBlock $Script:ScriptBlockAttributes
# Register-ArgumentCompleter -CommandName Get-FederatedDirectoryUser -ParameterName SortBy -ScriptBlock $Script:ScriptBlockAttributes
function Invoke-FederatedDirectory {
    [cmdletbinding(SupportsShouldProcess)]
    param(
        [System.Collections.IDictionary] $Authorization,
        [Array] $Operations
    )
    if (-not $Authorization) {
        if ($Script:AuthorizationCacheFD) {
            $Authorization = $Script:AuthorizationCacheFD[0]
        }
        if (-not $Authorization) {
            if ($PSBoundParameters.ErrorAction -eq 'Stop') {
                throw "No authorization found. Please run 'Connect-FederatedDirectory' first."
            } else {
                Write-Warning -Message "Invoke-FederatedDirectory - No authorization found. Please run 'Connect-FederatedDirectory' first."
                return
            }
        }
    }
    if ($Authorization) {
        $Body = [ordered] @{
            schemas    = @('urn:ietf:params:scim:api:messages:2.0:BulkRequest')
            Operations = $Operations
        }
        Remove-EmptyValue -Hashtable $Body -Recursive -Rerun 2
        Try {
            $invokeRestMethodSplat = [ordered] @{
                Method      = 'POST'
                Uri         = 'https://api.federated.directory/v2/Bulk'
                Headers     = [ordered]  @{
                    'Content-Type'  = 'application/json; charset=utf-8'
                    'Authorization' = $Authorization.Authorization
                    'Cache-Control' = 'no-cache'
                }
                Body        = $Body | ConvertTo-Json -Depth 10
                ErrorAction = 'Stop'
            }
            if ($DirectoryID) {
                $invokeRestMethodSplat['Headers']['directoryId'] = $DirectoryID
            }
            # for troubleshooting
            if ($VerbosePreference -eq 'Continue') {
                $Body | ConvertTo-Json -Depth 10 | Write-Verbose
            }
            if ($PSCmdlet.ShouldProcess("Federated Directory", "Bulk sending $($Operations.Count) operations")) {
                $ReturnData = Invoke-RestMethod @invokeRestMethodSplat -Verbose:$false
                # don't return data as we trust it's been created
                if (-not $Suppress) {
                    $ReturnData.Operations.Response
                }
            }
            # # for troubleshooting
            # if ($VerbosePreference -eq 'Continue') {
            # $invokeRestMethodSplat.Remove('body')
            # $invokeRestMethodSplat | ConvertTo-Json -Depth 10 | Write-Verbose
            # }
        } catch {
            if ($PSBoundParameters.ErrorAction -eq 'Stop') {
                throw
            } else {
                $ErrorDetails = $_.ErrorDetails.Message | ConvertFrom-Json -ErrorAction SilentlyContinue
                Write-Warning -Message "Invoke-FederatedDirectory - Error $($_.Exception.Message), $($ErrorDetails.Detail)"
            }
        }
    } else {
        Write-Warning -Message 'Invoke-FederatedDirectory - No authorization found. Please make sure to use Connect-FederatedDirectory first.'
    }
}
function Remove-FederatedDirectoryUser {
    <#
    .SYNOPSIS
    Remove a user from a federated directory.
 
    .DESCRIPTION
    Remove a user from a federated directory.
 
    .PARAMETER Authorization
    The authorization identity to use for the request from Connect-FederatedDirectory. If not specified, the default authorization identity will be used.
 
    .PARAMETER User
    The user to remove from the federated directory.
 
    .PARAMETER Id
    The id of the user to remove from the federated directory.
 
    .PARAMETER SearchUserName
    The user name of the user to remove from the federated directory.
 
    .PARAMETER DirectoryID
    The id of the directory to remove the user from. If not specified, the default directory will be used.
 
    .EXAMPLE
     # remove specific user id
    Remove-FederatedDirectoryUser -Id '171a8cd0-2382-11ed-9dd1-b13400d703b6' -Verbose
 
    .EXAMPLE
    # get all ther users that contain name test user and delete them
    Remove-FederatedDirectoryUser -UserName 'testuser' -Verbose
 
    .EXAMPLE
     # get all ther users that contain name test user and delete them
    Get-FederatedDirectoryUser -UserName 'testuser' | Remove-FederatedDirectoryUser -Verbose
 
    .NOTES
    General notes
    #>

    [alias('Remove-FDUser')]
    [CmdletBinding(DefaultParameterSetName = 'Id', SupportsShouldProcess)]
    param(
        [System.Collections.IDictionary] $Authorization,
        [parameter(Position = 0, ValueFromPipeline, Mandatory, ParameterSetName = 'User')][PSCustomObject[]] $User,
        [parameter(Mandatory, ParameterSetName = 'Id')][string[]] $Id,
        [parameter(Mandatory, ParameterSetName = 'UserName')][string[]] $SearchUserName,
        [parameter()][string] $DirectoryID,
        [switch] $BulkProcessing
    )
    Begin {
        if (-not $Authorization) {
            if ($Script:AuthorizationCacheFD) {
                $Authorization = $Script:AuthorizationCacheFD[0]
            }
            if (-not $Authorization) {
                if ($PSBoundParameters.ErrorAction -eq 'Stop') {
                    throw "No authorization found. Please run 'Connect-FederatedDirectory' first."
                } else {
                    Write-Warning -Message "Get-FederatedDirectory - No authorization found. Please run 'Connect-FederatedDirectory' first."
                    return
                }
            }
        }
    }
    Process {
        if ($Authorization) {
            if ($Id) {
                $RemoveID = $Id
            } elseif ($User) {
                $RemoveID = $User.Id
            } elseif ($SearchUserName) {
                $RemoveID = Foreach ($U in $SearchUserName) {
                    (Get-FederatedDirectoryUser -Authorization $Authorization -UserName $U).Id
                }
            } else {
                return
            }
            foreach ($I in $RemoveID) {
                Try {
                    if ($BulkProcessing) {
                        # Return body is used for using Invoke-FederatedDirectory to add/set/remove users in bulk
                        return [ordered] @{
                            data   = @{
                                schemas = @("urn:ietf:params:scim:schemas:core:2.0:User")
                                id      = $I
                            }
                            method = 'DELETE'
                            bulkid = $I
                        }
                    }
                    $invokeRestMethodSplat = [ordered] @{
                        Method      = 'DELETE'
                        Uri         = "https://api.federated.directory/v2/Users/$I"
                        Headers     = [ordered]  @{
                            'Content-Type'  = 'application/json'
                            'Authorization' = $Authorization.Authorization
                            'Cache-Control' = 'no-cache'
                            'directoryId'   = $DirectoryID
                        }
                        ErrorAction = 'Stop'
                    }
                    Remove-EmptyValue -Hashtable $invokeRestMethodSplat -Recursive

                    if ($VerbosePreference -eq 'Continue') {
                        $invokeRestMethodSplat | ConvertTo-Json -Depth 10 | Write-Verbose
                    }
                    if ($PSCmdlet.ShouldProcess($I, "Removing user")) {
                        $ReturnData = Invoke-RestMethod @invokeRestMethodSplat
                        $ReturnData
                    }
                } catch {
                    $ErrorDetails = $_.ErrorDetails.Message | ConvertFrom-Json -ErrorAction SilentlyContinue
                    if ($ErrorDetails.Detail -like "*not found*") {
                        Write-Warning -Message "Remove-FederatedDirectoryUser - $($ErrorDetails.Detail)."
                    } else {
                        Write-Warning -Message "Remove-FederatedDirectoryUser - Error $($_.Exception.Message), $($ErrorDetails.Detail)"
                    }
                }
            }
        } else {
            Write-Warning -Message 'Remove-FederatedDirectoryUser - No authorization found. Please make sure to use Connect-FederatedDirectory first.'
        }
    }
}
function Set-FederatedDirectoryUser {
    [alias('Set-FDUser')]
    [CmdletBinding(SupportsShouldProcess)]
    param(
        [System.Collections.IDictionary] $Authorization,
        [string] $SearchUserName,
        [string] $Id,
        [string] $ExternalId,
        [parameter()][string] $DirectoryID,
        [parameter()][string] $UserName,
        [Alias('FirstName')] $FamilyName,
        [string] $GivenName,
        [parameter()][string] $DisplayName,
        [string] $NickName,
        [string] $ProfileUrl,
        [string] $EmailAddress,
        [string] $EmailAddressHome,
        [string] $StreetAddress,
        [string] $City,
        [string] $Region,
        [string] $PostalCode,
        [string] $Country,
        [string] $StreetAddressHome,
        [string] $PostalCodeHome,
        [string] $CityHome,
        [string] $RegionHome,
        [string] $CountryHome,
        [string] $PhoneNumberWork,
        [string] $PhoneNumberHome,
        [string] $PhoneNumberMobile,
        [string] $PhotoUrl,
        [string] $ThumbnailUrl,
        [string] $CompanyID,
        #[string] $CompanyLogoUrl,
        #[string] $CompanyThumbnailUrl,
        [string] $PreferredLanguage,
        [string] $Locale,
        [string] $TimeZone,
        [string] $Title,
        [string] $UserType,
        [string] $Password,
        [string] $ManagerID,
        [string] $ManagerUserName,
        [string] $ManagerReference,
        [string] $ManagerDisplayName,
        [bool] $Active,
        #[string] $Organization,
        [string] $Department,
        [string] $EmployeeNumber,
        [string] $CostCenter,
        [string] $Division,
        [string] $Description,
        [ValidateSet('admin', 'user')][string] $Role,
        [alias('CustomAttribute01')][string] $Custom01,
        [alias('CustomAttribute02')][string] $Custom02,
        [alias('CustomAttribute03')][string] $Custom03,
        [switch] $Suppress,
        [ValidateSet('Overwrite', 'Update')][string] $Action = 'Update',
        [System.Collections.IDictionary] $ActionPerProperty = @{},
        [switch] $BulkProcessing
    )
    if (-not $Authorization) {
        if ($Script:AuthorizationCacheFD) {
            $Authorization = $Script:AuthorizationCacheFD[0]
        }
        if (-not $Authorization) {
            if ($PSBoundParameters.ErrorAction -eq 'Stop') {
                throw "No authorization found. Please run 'Connect-FederatedDirectory' first."
            } else {
                Write-Warning -Message "Set-FederatedDirectoryUser - No authorization found. Please run 'Connect-FederatedDirectory' first."
                return
            }
        }
    }
    if ($Authorization) {
        if ($Id) {
            $SetID = $Id
        } elseif ($User) {
            $SetID = $User.Id
        } elseif ($SearchUserName) {
            $SetID = Foreach ($U in $SearchUserName) {
                (Get-FederatedDirectoryUser -Authorization $Authorization -UserName $U).Id
            }
        } else {
            return
        }
        if ($Action -eq 'Update') {
            $TranslatePath = @{
                UserName           = "userName"
                ExternalId         = "externalId"
                FamilyName         = "name.familyName"
                Password           = "password" # not used yet
                # not used yet
                Role               = "roles.value" # "admin" or "user"
                GivenName          = 'name.givenName'
                DisplayName        = 'displayName'
                NickName           = 'nickName'
                ProfileUrl         = 'profileUrl'
                EmailAddress       = 'emails[type eq "work"].value'
                EmailAddressHome   = 'emails[type eq "home"].value'
                StreetAddress      = 'addresses[type eq "work"].streetAddress'
                City               = 'addresses[type eq "work"].locality'
                Region             = 'addresses[type eq "work"].region'
                PostalCode         = 'addresses[type eq "work"].postalCode'
                Country            = 'addresses[type eq "work"].country'
                StreetAddressHome  = 'addresses[type eq "home"].streetAddress'
                PostalCodeHome     = 'addresses[type eq "home"].postalCode'
                CityHome           = 'addresses[type eq "home"].locality'
                RegionHome         = 'addresses[type eq "home"].region'
                CountryHome        = 'addresses[type eq "home"].country'
                PhoneNumberWork    = 'phoneNumbers[type eq "work"].value'
                PhoneNumberHome    = 'phoneNumbers[type eq "home"].value'
                PhoneNumberMobile  = 'phoneNumbers[type eq "mobile"].value'
                PhotoUrl           = 'photos[type eq "photo"].value'
                ThumbnailUrl       = 'photos[type eq "thumbnail"].value'
                Title              = 'title'
                UserType           = 'userType'
                Active             = 'active'
                TimeZone           = 'timezone'
                Locale             = 'locale'
                PreferredLanguage  = 'preferredLanguage'
                EmployeeNumber     = 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber'
                CostCenter         = 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:costCenter'
                Division           = 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:division'
                Department         = 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:department'
                Organization       = 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:organization'
                ManagerID          = 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:manager.value'
                ManagerUserName    = 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:manager.value'
                ManagerDisplayName = 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:manager.displayName'
                Description        = "urn:ietf:params:scim:schemas:extension:fd:2.0:User:description"
                Custom01           = "urn:ietf:params:scim:schemas:extension:fd:2.0:User:custom01"
                Custom02           = "urn:ietf:params:scim:schemas:extension:fd:2.0:User:custom02"
                Custom03           = "urn:ietf:params:scim:schemas:extension:fd:2.0:User:custom03"
                CompanyID          = "urn:ietf:params:scim:schemas:extension:fd:2.0:User:companyId"
                #CompanyLogoUrl = 'urn:ietf:params:scim:schemas:extension:fd:2.0:User:companyLogos[type eq "logo"].value'
                #CompanyThumbnailUrl = 'urn:ietf:params:scim:schemas:extension:fd:2.0:User:companyLogos[type eq "thumbnail"].value'
            }

            if ($ManagerUserName) {
                $ManagerID = (Get-FederatedDirectoryUser -Authorization $Authorization -UserName $ManagerUserName).Id
            }

            $Body = [ordered] @{
                schemas    = @(
                    "urn:ietf:params:scim:api:messages:2.0:PatchOp"
                )
                Operations = @(
                    foreach ($Key in $PSBoundParameters.Keys) {
                        if ($Key -in $TranslatePath.Keys) {
                            if ($TranslatePath[$Key]) {
                                $Path = $TranslatePath[$Key]
                            } else {
                                $Path = $Key
                            }
                            if ($PSBoundParameters[$Key]) {
                                $Value = $PSBoundParameters[$Key]
                            } else {
                                $Value = $null
                            }
                            if ($ActionPerProperty) {
                                if ($ActionPerProperty[$Key]) {
                                    $ActionProperty = $ActionPerProperty[$Key]
                                } else {
                                    $ActionProperty = 'replace'
                                }
                            } else {
                                $ActionProperty = 'replace'
                            }
                            [ordered] @{
                                op    = $ActionProperty
                                path  = $Path
                                value = $Value
                            }
                        }
                    }
                )
            }
        } else {

            $Body = [ordered] @{
                schemas                                                      = @(
                    "urn:ietf:params:scim:schemas:core:2.0:User"
                    "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
                    "urn:ietf:params:scim:schemas:extension:fd:2.0:User"
                )
                # Mandatory
                "id"                                                         = $Id
                "externalId"                                                 = $ExternalId
                "userName"                                                   = $UserName
                "name"                                                       = [ordered] @{
                    "familyName" = $FamilyName
                    "givenName"  = $GivenName
                }
                "displayName"                                                = $DisplayName
                "nickName"                                                   = $NickName
                "profileUrl"                                                 = $ProfileUrl
                "emails"                                                     = @(
                    if ($EmailAddress) {
                        @{
                            "value"   = $EmailAddress
                            "type"    = "work"
                            "primary" = $true
                        }
                    }
                    if ($EmailAddressHome) {
                        @{
                            "value" = $EmailAddressHome
                            "type"  = "home"
                        }
                    }
                )
                "addresses"                                                  = @(
                    if ($StreetAddress -or $Locality -or $Region -or $PostalCode -or $Country) {
                        @{
                            "streetAddress" = $StreetAddress
                            "locality"      = $Locality
                            "region"        = $Region
                            "postalCode"    = $PostalCode
                            "country"       = $Country
                            "type"          = "work"
                            "primary"       = $true
                        }
                    }
                    if ($StreetAddressHome -or $LocalityHome -or $RegionHome -or $PostalCodeHome -or $CountryHome) {
                        @{
                            "streetAddress" = $StreetAddressHome
                            "locality"      = $LocalityHome
                            "region"        = $RegionHome
                            "postalCode"    = $PostalCodeHome
                            "country"       = $CountryHome
                            "type"          = "home"
                        }
                    }
                )
                "phoneNumbers"                                               = @(
                    if ($PhoneNumberWork) {
                        @{
                            "value"   = $PhoneNumberWork
                            "type"    = "work"
                            "primary" = $true
                        }
                    }
                    if ($PhoneNumberHome) {
                        @{
                            "value" = $PhoneNumberHome
                            "type"  = "home"
                        }
                    }
                    if ($PhoneNumberMobile) {
                        @{
                            "value" = $PhoneNumberMobile
                            "type"  = "mobile"
                        }
                    }
                )
                "photos"                                                     = @(
                    if ($PhotoUrl) {
                        @{
                            "value" = $PhotoUrl
                            "type"  = "photo"
                        }
                    }
                    if ($ThumbnailUrl) {
                        @{
                            "value" = $ThumbnailUrl
                            "type"  = "thumbnail"
                        }
                    }
                )
                "password"                                                   = $Password
                "preferredLanguage"                                          = $PreferredLanguage
                "locale"                                                     = $Locale
                "timeZone"                                                   = $TimeZone
                "userType"                                                   = $UserType
                "title"                                                      = $Title
                "active"                                                     = if ($PSBoundParameters.Keys -contains ('Active')) { $Active.IsPresent } else { $Null }
                "roles"                                                      = @(
                    @{
                        "value"   = $Role
                        "display" = $Role
                    }
                )
                "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User" = [ordered] @{
                    #"organization" = $Organization # read only...
                    "department"     = $Department
                    "employeeNumber" = $EmployeeNumber
                    "costCenter"     = $CostCenter
                    "division"       = $Division
                    "manager"        = @{
                        "displayName" = $ManagerDisplayName
                        "value"       = $ManagerID
                    }
                }
                "urn:ietf:params:scim:schemas:extension:fd:2.0:User"         = [ordered] @{
                    "description" = $Description
                    "companyId"   = $CompanyId
                    # "companyLogos" = @(
                    # if ($CompanyLogoUrl) {
                    # @{
                    # "value" = $CompanyLogoUrl
                    # "type" = "logo"
                    # }
                    # }
                    # if ($CompanyThumbnailUrl) {
                    # @{
                    # "value" = $CompanyThumbnailUrl
                    # "type" = "thumbnail"
                    # }
                    # }
                    # )
                    #'directoryId' = $DirectoryID
                    'custom01'    = $Custom01
                    'custom02'    = $Custom02
                    'custom03'    = $Custom03
                }
            }
        }
        Try {
            Remove-EmptyValue -Hashtable $Body -Recursive -Rerun 2

            $MethodChosen = if ($Action -eq 'Update') { 'PATCH' } else { 'PUT' }
            if ($BulkProcessing) {
                # Return body is used for using Invoke-FederatedDirectory to add/set/remove users in bulk
                return [ordered] @{
                    data   = $Body
                    method = $MethodChosen
                    bulkid = $Body.id
                }
            }
            $invokeRestMethodSplat = [ordered] @{
                Method      = $MethodChosen
                Uri         = "https://api.federated.directory/v2/Users/$SetID"
                Headers     = [ordered]  @{
                    'Content-Type'  = 'application/json'
                    'Authorization' = $Authorization.Authorization
                    'Cache-Control' = 'no-cache'
                }
                Body        = $Body | ConvertTo-Json -Depth 10
                ErrorAction = 'Stop'
            }
            if ($DirectoryID) {
                $invokeRestMethodSplat['Headers']['directoryId'] = $DirectoryID
            }
            # for troubleshooting
            if ($VerbosePreference -eq 'Continue') {
                $Body | ConvertTo-Json -Depth 10 | Write-Verbose
            }
            if ($PSCmdlet.ShouldProcess($SetID, "Updating user using $Action method")) {
                $ReturnData = Invoke-RestMethod @invokeRestMethodSplat
                # don't return data as we trust it's been updated
                if (-not $Suppress) {
                    $ReturnData
                }
            }
            # # for troubleshooting
            # if ($VerbosePreference -eq 'Continue') {
            # $invokeRestMethodSplat.Remove('body')
            # $invokeRestMethodSplat | ConvertTo-Json -Depth 10 | Write-Verbose
            # }
        } catch {
            if ($PSBoundParameters.ErrorAction -eq 'Stop') {
                throw
            } else {
                $ErrorDetails = $_.ErrorDetails.Message | ConvertFrom-Json -ErrorAction SilentlyContinue
                if ($ErrorDetails.Detail -like '*userName is mandatory*') {
                    Write-Warning -Message "Set-FederatedDirectoryUser - $($ErrorDetails.Detail) [Id: $SetID]"
                } else {
                    Write-Warning -Message "Set-FederatedDirectoryUser - Error $($_.Exception.Message), $($ErrorDetails.Detail) [Id: $SetID]"
                }
            }
        }
    } else {
        Write-Warning -Message 'Set-FederatedDirectoryUser - No authorization found. Please make sure to use Connect-FederatedDirectory first.'
    }
}



# Export functions and aliases as required
Export-ModuleMember -Function @('Add-FederatedDirectoryUser', 'Connect-FederatedDirectory', 'Get-FederatedDirectorySchema', 'Get-FederatedDirectoryUser', 'Invoke-FederatedDirectory', 'Remove-FederatedDirectoryUser', 'Set-FederatedDirectoryUser') -Alias @('Add-FDUser', 'Connect-FD', 'Get-FDSchema', 'Get-FDUser', 'Remove-FDUser', 'Set-FDUser')
# SIG # Begin signature block
# MIInPgYJKoZIhvcNAQcCoIInLzCCJysCAQExDzANBglghkgBZQMEAgEFADB5Bgor
# BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG
# KX7zUQIBAAIBAAIBAAIBAA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