Set the LCS configuration details
        Set the LCS configuration details and save them into the configuration store
    .PARAMETER ProjectId
        The project id for the Dynamics 365 for Finance & Operations project inside LCS
    .PARAMETER ClientId
        The Azure Registered Application Id / Client Id obtained while creating a Registered App inside the Azure Portal
    .PARAMETER BearerToken
        The token you want to use when working against the LCS api
    .PARAMETER ActiveTokenExpiresOn
        The point in time where the current bearer token will expire
        The time is measured in Unix Time, total seconds since 1970-01-01
    .PARAMETER RefreshToken
        The Refresh Token that you want to use for the authentication process
    .PARAMETER LcsApiUri
        URI / URL to the LCS API you want to use
        Depending on whether your LCS project is located in europe or not, there is 2 valid URI's / URL's
        Valid options:
    .PARAMETER Temporary
        Instruct the cmdlet to only temporarily override the persisted settings in the configuration storage
        PS C:\> Set-D365LcsApiConfig -ProjectId 123456789 -ClientId "9b4f4503-b970-4ade-abc6-2c086e4c4929" -BearerToken "JldjfafLJdfjlfsalfd..." -ActiveTokenExpiresOn 1556909205 -RefreshToken "Tsdljfasfe2j32324" -LcsApiUri ""
        This will set the LCS API configuration.
        The ProjectId 123456789 will be saved as the default ProjectId for all cmdlets that will interact with LCS, if they require a ProjectId.
        The ClientId "9b4f4503-b970-4ade-abc6-2c086e4c4929" will be saved as the default ClientId for all cmdlets that will interact with LCS, if they require a ClientId.
        The BearerToken "JldjfafLJdfjlfsalfd..." will be saved as the default BearerToken. Remember the BearerToken will expire, so you should fill in the ActiveTokenExpiresOn and RefreshToken parameters also.
        The ActiveTokenExpiresOn 1556909205 will be saved to assist the module in determine whether the BearerToken is still valid or not.
        The RefreshToken "Tsdljfasfe2j32324" will be saved as the default RefreshToken for all cmdlets that will interact with tokens.
        The LcsApiUri "" will be saved as the default LCS HTTP endpoint for all cmdlets that will interact with LCS.
        PS C:\> Get-D365LcsApiToken -Username "" -Password "TopSecretPassword" | Set-D365LcsApiConfig
        This will obtain a valid OAuth 2.0 access token from Azure Active Directory and save the needed details.
        The Username "" and Password "TopSecretPassword" is used in the OAuth 2.0 Grant Flow, to approved that the application should impersonate like "".
        The output object received from Get-D365LcsApiToken is piped directly to Set-D365LcsApiConfig.
        Set-D365LcsApiConfig will save the access_token(BearerToken), refresh_token(RefreshToken) and expires_on(ActiveTokenExpiresOn).
        These values will then be available as default values for all LCS cmdlets across the module.
        You can validate the current default values by calling Get-D365LcsApiConfig.
        Tags: Environment, Url, Config, Configuration, LCS, Upload, ClientId
        Author: Mötz Jensen (@Splaxi)

function Set-D365LcsApiConfig {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
        [int] $ProjectId,

        [string] $ClientId,

        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [string] $BearerToken,

        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [long] $ActiveTokenExpiresOn,

        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [string] $RefreshToken,

        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [string] $LcsApiUri = "",
        [switch] $Temporary


    process {
        #The ':keys' label is used to have a continue inside the switch statement itself
        :keys foreach ($key in $PSBoundParameters.Keys) {
            $configurationValue = $PSBoundParameters.Item($key)
            $configurationName = $key.ToLower()
            $fullConfigName = ""

            Write-PSFMessage -Level Verbose -Message "Working on $key with $configurationValue" -Target $configurationValue
            switch ($key) {
                "Temporary" {
                    continue keys

                Default {
                    $fullConfigName = "$configurationName"

            Write-PSFMessage -Level Verbose -Message "Setting $fullConfigName to $configurationValue" -Target $configurationValue
            Set-PSFConfig -FullName $fullConfigName -Value $configurationValue
            if (-not $Temporary) { Register-PSFConfig -FullName $fullConfigName -Scope UserDefault }