Git Product home page Git Product logo

login-azcontext's Introduction

Login-AzContext

Synopis

Save your Azure context with account and subscription information to a file

Description

Sometimes life is about the little things, and one little thing that has been bothering me is logging on to Azure in Powershell using Add-AzureRmAccount or Add-AzAccount. Every time you start Powershell, you need to log on again and that gets tired quickly, especially with accounts having mandatory 2FA.

It gets even more complicated if you have multiple accounts to manage, for instance, one for testing and another for production. To top it off, you can start over when it turns out that your context has expired, which you will only discover after you actually executed some AzureRM or Az commands.

The standard trick to make this easier is to save your Azure context with account and subscription information to a file (Save-AzureRmContext or Save-AzContext), and to import this file whenever you need (Import-AzureRmContext or Import-AzContext). But we can do a little bit better than that.

Parameters

ParentFolder (Required)

A String value for the path to store the Context File

AccountName (Required)

A String value for the name of account

TenantId (Required)

A String value for the guid of a Tenant (Required)

SubscriptionId

A String value for the guid of a subscription

Example

Use a PowerShell profile to define a function doing the work.

A profile gets loaded whenever you start PowerShell. There are multiple profiles, but the one we want is for CurrentUser - Allhosts.

The function will load the Azure context from a file. If there is no such file, it should prompt me to log on.

After logging on, the context should be tested for validity because the token may have expired. If the token is expired, prompt for logon again.

If needed, save the new context to a file. To make this work, add this function to the powershell profile: from the Powershell ISE, type ise $profile.CurrentUserAllHosts or VSCode, type code $profile.CurrentUserAllHosts to edit the profile and copy/paste the function definition.

Suppose I have two Azure accounts that I want to use here, called 'personal' and 'work'. For that I would add the following function definitions to the profile:

#
# specific azure logons. Context file is deliberately in a non-synced folder for security reasons.
#
    
function azure-personal { Login-AzureContext -ParentFolder "$env:LOCALAPPDATA\Windows Azure PowerShell" -AccountName "personal" -TenantId "9d2426e9-b74a-428e-9065-80f29e416c3e" }
function azure-work { Login-AzureContext -ParentFolder "$env:LOCALAPPDATA\Windows Azure PowerShell" -AccountName "work" -TenantId "9d2426e9-b74a-428e-9065-80f29e416c3e" -SubscriptionId "9877a694-1b15-4cdc-91d2-7bbfde6bf348"}

To log on to 'personal', you simply execute azure-personal. If this is a first logon, I get the usual Azure logon dialog and the resulting context gets saved. The next time, the existing file is loaded and the context tested for validity. From that point on you can switch between accounts whenever you need.

Notes

Version: 1.3.0
Author: Willem Kasdorp (original)
Modified: Paul Towler
Creation Date: 29/10/2018 16:00
Required Modules: Az
Dependencies: PowerShell 5.1 or PowerShell Core
Limitations: None
Supported Platforms * Windows, macOS
*Currently not tested against other platforms
Version History: [29/10/2018 - 0.01 - Paul Towler]: Initial script. Add fixes as discussed here
[21/02/2019 - 0.02 - Paul Towler]: Added Check for PowerShell Core
[21/02/2019 - 1.0.0 - Paul Towler]: Full Release - Updated PowerShell Core to enable AzureRm Alias - New function to get Access Token from here
[26/08/2019 - 1.1.0 - Paul Towler]: Added TenantId parameter to cater for accounts that have access to many Tenants
[12/08/2020 - 1.2.0 - Paul Towler]: Removed AzureRm (Time to move on)
[12/08/2021 - 1.3.0 - Paul Towler]: BUGFIX: Issue with multiple Tenants and the same account name. Also issue using same Subscription Names. Changed to SubscriptionId.

login-azcontext's People

Contributors

mrptsai avatar paul-towler avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.