Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#region Connect to Correct Tenant and Subscription
$CurrentContext = Get-AzContext
#If there is no AzContext found connect to desired Subscription and Tenant
if (!$CurrentContext) {
Connect-AzAccount -Tenant $TenantId -Subscription $SubscriptionId -UseDeviceAuthentication
$CurrentContext = Get-AzContext
#If subscription ID doesnt match, call the Set-AzContext with
#SubscriptionId and TenantId to allow switch between tenants as well.
if ($CurrentContext.Subscription.Id -ne $SubscriptionId) {
$CurrentContext = Set-AzContext -Subscription $SubscriptionId -Tenant $TenantId
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment