public/Select-MyCorpSubscription.ps1
|
function Select-MyCorpSubscription { <# .SYNOPSIS Allows the user to select an Azure subscription for MyCorp to run tests against. .DESCRIPTION This command reads the subscription list stored during Connect-MyCorp and allows interactive selection. After selection, it sets the Azure context and stores the selection in $__MyCorpSession.SelectedSubscription. #> if (-not $__MyCorpSession.Subscriptions -or $__MyCorpSession.Subscriptions.Count -eq 0) { Write-Error "No Azure subscriptions available. Run Connect-MyCorp -Service Azure first." return } $subs = $__MyCorpSession.Subscriptions Write-Host "`nAvailable Azure Subscriptions:" -ForegroundColor Yellow $i = 0 foreach ($s in $subs) { $i++ Write-Host ("[{0}] {1} ({2})" -f $i, $s.Name, $s.Id) } $choice = Read-Host "Enter subscription number" if ($choice -notmatch '^\d+$') { Write-Error "Invalid choice. Please enter a number." return } $index = [int]$choice if ($index -lt 1 -or $index -gt $subs.Count) { Write-Error "Invalid selection. Choose a value between 1 and $($subs.Count)." return } $selected = $subs[$index - 1] Write-Host "Selected Subscription: $($selected.Name)" -ForegroundColor Green try { Set-AzContext -SubscriptionId $selected.Id -ErrorAction Stop $__MyCorpSession.SelectedSubscription = $selected Write-Host "Azure context updated to subscription: $($selected.Name)" -ForegroundColor Cyan } catch { Write-Error "Failed to set Azure subscription context: $($_.Exception.Message)" } } |