functions/Set-BBRESTSession.ps1

<#
.Synopsis
   Sets the session for the current enviroinment from the Blackboard RESTAPI. This will be used for any Invoke-BBRESTMethod Calls that are made.
   It uses the values stored in Get-BBResconfig. These values can be updated using Set-BBRestConfig.
.DESCRIPTION
   Sets the session for the current enviroinment from the Blackboard RESTAPI. This will be used for any Invoke-BBRESTMethod Calls that are made.
   It uses the values stored in Get-BBResconfig. These values can be updated using Set-BBRestConfig.
.EXAMPLE
   Set-BBRESTSession -verbose
#>

function Set-BBRESTSession
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([int])]
    Param
    (
      [string]$Environment = 'Production'
    )

    Begin
    {
    }
    Process
    {
        $securePwd = ConvertTo-SecureString "$($BBRESTVariables.ClientSecret)" -AsPlainText -Force
        $credential = New-Object System.Management.Automation.PSCredential ($($BBRESTVariables.ClientID), $securePwd)

        if ($Environment -eq 'Production'){
         $Uri = "$($BBRESTVariables.ProductionClientURL)/learn/api/public/v1/oauth2/token?&redirect_uri=" + [System.Web.HttpUtility]::UrlEncode("https://localhost")
         $Authorization =
            Invoke-RestMethod   -Method Post `
                                -ContentType application/x-www-form-urlencoded `
                                -Uri $Uri `
                                -Body "grant_type=client_credentials" `
                                -Credential $credential `
                                -Authentication Basic

        $BBRESTVariables.ProductionBearerToken = $Authorization.access_token;
        $BBRESTVariables.ProductionBearerTokenRefreshTime = (Get-Date).AddSeconds($Authorization.expires_in)
        Write-Verbose "Created PROD Token $($BBRESTVariables.ProductionBearerToken)"
        }
        if ($Environment -eq 'Test'){
         $Uri = "$($BBRESTVariables.TestClientURL)/learn/api/public/v1/oauth2/token?&redirect_uri=" + [System.Web.HttpUtility]::UrlEncode("https://localhost")
         $Authorization =
         Invoke-RestMethod   -Method Post `
                             -ContentType application/x-www-form-urlencoded `
                             -Uri $Uri `
                             -Body "grant_type=client_credentials" `
                             -Credential $credential `
                             -Authentication Basic

         $BBRESTVariables.TestBearerToken = $Authorization.access_token;
         $BBRESTVariables.TestBearerTokenRefreshTime = (Get-Date).AddSeconds($Authorization.expires_in)
         Write-Verbose "Created TEST Token $($BBRESTVariables.TestBearerToken)"
        }





    }
    End
    {
    }
}