internal/Initialize-MtSession.ps1

<#
.SYNOPSIS
    Initializes the MtSession / MyCorp session object.
 
.DESCRIPTION
    Called automatically by Invoke-MyCorp.
    Sets default session values including portal URLs and subscription filters.
#>


function Initialize-MtSession {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Module variables used in other functions.')]
    param()

    #
    # 1. Initialize environment metadata
    #
    $environment = (Get-MgContext).Environment
    $__MtSession.AdminPortalUrl = Get-MtAdminPortalUrl -Environment $environment


    #
    # 2. Load subscription selection (if Connect-MyCorp stored it)
    #
    if ($script:__MyCorpSession -and $script:__MyCorpSession.SelectedSubscriptionIds) {

        # Export to global session variable for tests
        $__MtSession.SelectedSubscriptions = $script:__MyCorpSession.SelectedSubscriptionIds

        # Also expose via environment variable (used by Pester tests)
        Set-Item Env:\MYCORP_SELECTED_SUBSCRIPTIONS `
            -Value ($script:__MyCorpSession.SelectedSubscriptionIds -join ",") -Force

        Write-Verbose "Subscription filter applied: $($env:MYCORP_SELECTED_SUBSCRIPTIONS)"
    }
    else {
        $__MtSession.SelectedSubscriptions = @()
        Remove-Item Env:\MYCORP_SELECTED_SUBSCRIPTIONS -ErrorAction SilentlyContinue
        Write-Verbose "No subscription filter found. All subscriptions will be tested (if Azure tests are used)."
    }
}