Skip to content

Instantly share code, notes, and snippets.

@PixelRobots
Created March 20, 2019 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PixelRobots/da65c2289667570a1f6e4c8dc9d49e9e to your computer and use it in GitHub Desktop.
Save PixelRobots/da65c2289667570a1f6e4c8dc9d49e9e to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment