PowerShell Snippet 13 - Skip Logging Into Azure Everytime With This One Simple Trick

Hello!

I have been working an awful lot with ARM Templates and publishing resources using PowerShell. As part ofhte process we’re using a Service Endpoint to publish resoruces to Azure. So the login and subscription context are set by the endpoint itself.

However, when running the scripts locally, it is necessary to login first. so you have two choices - login before running the script, which isn’t always possible, or include Login-AzureRmAccount in every script, and then obviously forget to remove it prior to checking in and wondering why releases takes forever…

So here is a fancy dan piece of PowerShell you can include in the scripts and have in the build/deploy pipeline.

if ([string]::IsNullOrEmpty($(Get-AzureRmContext).Account)) {
    Login-AzureRmAccount
    Select-AzureSubscription -Current -SubscriptionName ContosoEngineering
}