functions/azure/Test-BcAdminSession.ps1

function Test-BcAdminSession {
    <#
    .SYNOPSIS
        Checks if all required values for Connect-ToAzure are present in the session.
 
    .DESCRIPTION
        This function verifies whether the session variable $BcAdminSession contains all necessary values (TenantId and ServicePrincipalName)
        required to execute Connect-ToAzure. This ensures that Connect-ToAzure can use credentials stored in the SecretStore.
 
    .OUTPUTS
        Returns a boolean value indicating whether all required values are present.
 
    .EXAMPLE
        PS> Test-BcAdminSession
        True
 
    .EXAMPLE
        PS> Test-BcAdminSession -ErrorAction Stop
        True
 
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false)]
        [guid]$TenantId
    )

    if ($null -eq $BcAdminSession) {
        Write-Error "No session found. Please initialize the session."
        return $false
    }

    $missingProperties = @()
    if ([guid]::Empty -eq $BcAdminSession.AzureTenantId) {
        $missingProperties += "TenantId"
    } elseif ([guid]::Empty -ne $TenantId -and $TenantId -ne $BcAdminSession.AzureTenantId) {
        Write-Error ("Provided TenantId '{0}' does not match the session TenantId '{1}'." -f $TenantId, $BcAdminSession.AzureTenantId)
        return $false
    }
    
    if ([string]::IsNullOrWhiteSpace($BcAdminSession.AzureServicePrincipalName)) {
        $missingProperties += "ServicePrincipalName"
    }
    if ($missingProperties.Count -gt 0) {
        Write-Error ("Required value(s) missing in the session: {0}." -f ($missingProperties -join ", "))
        return $false
    }

    return $true
}