If you’re working with Microsoft Azure and you’re performing tasks through PowerShell you know that before performing any operation you must authenticate first. To do this you can download the publish settings file (Get-AzurePublishSettingsFile) or use Add-AzureAccount. If what you need is to automate tasks, you must use an authentication system that does not require interaction with the user. In this post I’ll show how to authenticate with Add-AzureAccount using your Azure Active Directory associated with your subscription.
The first thing you should do is create a new user in the associated Azure Active Directory account.
Once created, you must add the user as co-administrator in paragraph SETTINGS > ADMINISTRATORS of your subscription.
Finally, we authenticate with this new user as follows:
$userName = “firstname.lastname@example.org” $securePassword = ConvertTo-SecureString -String "My_Password" -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential($userName, $securePassword) Add-AzureAccount -Credential $cred Get-AzureSubscription
The difference in this case is that I use New-Object System.Management.Automation.PSCredential($userName, $securePassword) to create an object of type PSCredential and pass it as a parameter in the called Add-AzureAccount – Credential $cred, allowing me access to my resources, without a prompt that requires interaction in an automated process.