You may have read a previous blog post of mine https://pixelrobots.co.uk/2018/06/azure-subscription-selector/ regarding Azure Subscription selection. This post builds on that and takes it to a new level. The old Azure subscription selector used out-grid for you to select a subscription. This new one presents you with a menu inside the PowerShell window and it also lets you switch Azure account.
I have created it as two functions to make it easier to use and adopt. If you have any questions or feedback please let me know.
The Code
| function Login { | |
| $needLogin = $true | |
| Try { | |
| $content = Get-AzContext | |
| if ($content) { | |
| $needLogin = ([string]::IsNullOrEmpty($content.Account)) | |
| } | |
| } | |
| Catch { | |
| if ($_ -like "*Login-AzAccount to login*") { | |
| $needLogin = $true | |
| } | |
| else { | |
| throw | |
| } | |
| } | |
| if ($needLogin) { | |
| Login-AzAccount | |
| } | |
| } | |
| Function Select-Subs { | |
| CLS | |
| $ErrorActionPreference = 'SilentlyContinue' | |
| $Menu = 0 | |
| $Subs = @(Get-AzSubscription | select Name, ID, TenantId) | |
| Write-Host "Please select the subscription you want to use:" -ForegroundColor Green; | |
| % {Write-Host ""} | |
| $Subs | % {Write-Host "[$($Menu)]" -ForegroundColor Cyan -NoNewline ; Write-host ". $($_.Name)"; $Menu++; } | |
| % {Write-Host ""} | |
| % {Write-Host "[S]" -ForegroundColor Yellow -NoNewline ; Write-host ". To switch Azure Account."} | |
| % {Write-Host ""} | |
| % {Write-Host "[Q]" -ForegroundColor Red -NoNewline ; Write-host ". To quit."} | |
| % {Write-Host ""} | |
| $selection = Read-Host "Please select the Subscription Number - Valid numbers are 0 - $($Subs.count -1), S to switch Azure Account or Q to quit" | |
| If ($selection -eq 'S') { | |
| Get-AzContext | ForEach-Object {Clear-AzContext -Scope CurrentUser -Force} | |
| Login | |
| Select-Subs | |
| } | |
| If ($selection -eq 'Q') { | |
| Clear-Host | |
| Exit | |
| } | |
| If ($Subs.item($selection) -ne $null) | |
| { Return @{name = $subs[$selection].Name; ID = $subs[$selection].ID} | |
| } | |
| } | |
| $SubscriptionSelection = Select-Subs | |
| Select-AzSubscription -SubscriptionName $SubscriptionSelection.Name -ErrorAction Stop |
You can find an always up to date version of this script and many more at this GitHub Repo.
Thanks for reading.
0 Comments