Skip to content

Saving Azure Credentials

When using Powershell

2016-10-13

Rinse / Repeat sb_floatWhen working with Azure Powershell you have to authenticate your cmdlet calls in order to actually get any work done.

This is pretty simple with Add-AzureRmAccount but having to do that for every script run gets tiresome fairly quick. To solve that you can save your credentials to a json file for a period of time with Save-AzureRmProfile and then you can load it with Select-AzureRmProfile

Automate the Automation

To wrap this I've been using this snippet:

$profileFileName = "AzureRmProfile.json"
$ProfileFilePath = (Join-Path $env:Temp $profileFileName)

# Sign In / Save Profile
if((Test-Path $profileFilePath) -eq $false) {
    Write-Host "Logging in..." -ForegroundColor Yellow
    Login-AzureRmAccount
    Write-Host "Saving profile" -ForegroundColor Green
    Save-AzureRmProfile -Path $profileFilePath
}
Select-AzureRmProfile -Path $profileFilePath | Out-Null
A few things to note. This sample is storing the profile output to a temp location. You'll probably want to get this into a Powershell Module where you can handle flows where the file name get overwritten or what happens when the file expires.