Non-Interactive Azure Resource Manager (ARM) login

Written on June 25, 2016

Azure Resource Manager allows you to deploy, manage, and monitor all of the resources for your solution as a group, rather than handling these resources individually.

TL;DR from the docs:

With Resource Manager, you can create a simple template (in JSON format) that defines deployment and configuration of your application. This template is known as a Resource Manager template and provides a declarative way to define deployment. By using a template, you can repeatedly deploy your application throughout the app lifecycle and have confidence your resources are deployed in a consistent state.

Before you can deploy a resource group using PowerShell, you have to login using the Login-AzureRmAccount command.

By default, this opens a dialog where you can enter your Azure credentials:

This is ok if you are working with ARM on your local machine, but you don't want this behavior on your CI-Server for example.

Luckily, the Login-AzureRmAccount command also accepts an -Credential parameter which is an object consisting of your username and password.

The following PowerShell commands show how to create this object:

$accountName ="your account name"
$password = ConvertTo-SecureString "your password" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($accountName, $password)

Now you can login using the Login-AzureRmAccount command like this:

Login-AzureRmAccount -Credential $credential

If you're providing username and password using environment variables for example, you can set $accountName and password using these commands:

$accountName = (get-item env:"ACCOUNTNAME").Value
$password = (get-item env:"PASSWORD").Value

Happy deployment!