Powershell Authentication to Azure Resource Manager

Few days ago I have showed you how to authenticate to your Classic Azure resources with Powershell. Today, let’s check our authentication options to Azure Resource Manager (ARM) portal.

You can simply run the following cmdlet and will be prompted to login to your Azure account.

Add-AzureRmAccount

However, every single time you start your Powershell session you will have to provide username and password if you need to work with Azure resources.

Let’s check how else we can authenticate to our Azure Resource Manager via Powershell. First, you need to assign values to your username and password parameters

$Username = "yourusername@company.com"
$Password = Read-Host "Enter Password" -AsSecureString

In this case we just simple assign username as is and read the value to $Password parameter which is stored as a secure string. Now we need to store that string into the text file for the future use.

$Password | ConvertFrom-SecureString | Out-File D:\PS\Login.txt

By doing this step we are storing our password without displaying it as a plain text. After creating the text file with the password in it, we can use the following script block for future use of Powershell sessions when we will need to access our Azure Resource Manager portal resources.

$Username = "yourusername@company.com"
$Get = Get-Content D:\PS\Login.txt| ConvertTo-SecureString
$MyLogin = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $Get
Add-AzureRmAccount -Credential $MyLogin

Importan thing to mention, that -Credential paramereter will not work with Microsoft accounts. It will work only with Azure AD credentials.

Do you know how else you can authenticate to Azure? Please share in the comments!

Leave a Reply

Your email address will not be published.