Abstract
Honestly, I did not know. This error has created so much
of the frustration in the developers; who wishes to use Azure PowerShell and
Azure Automation. This blog post is dedicated to solving the error “Run
Login-AzureRmAccount to login”.
Background
Any Azure RM [a.k.a Azure Resource Manager] PowerShell
command execution first requires authentication done against your Azure
subscription. So if you fire any command without Login-AzureRMAccount; above sweet
error comes.
Reproducing the error
Let’s first reproduce this error.
I am assuming you already Azure PowerShell module
installed. If not refer
here for installation steps. Now open PowerShell and run the command to retrieve
all Azure VMs present in the Azure subscription –
Get-AzureRmVM
Error appears – “Run Login-AzureRmAccount to login”.
Solution is simple, run the command “Login-AzureRmAccount”
and it opens up a pop up. Enter the credentials. After this run the command of
retriving VMs again and everything works.
So, locally it’s easy to get rid of this error. How do
we solve the error in Azure Automation account? Let’s first reproduce the same
in Azure Automation account. I already have one Azure Automation account
created as per the earlier
blog post here. Refer section “Provision Azure Automation Account” in the
blog post.
Click on Runbooks -> Add a Runbook. Give the name of
your choice, select the type as “PowerShell”, and provide description of your
choice. Then click on Create. After runbook is created on the Azure Portal, Open
it by clicking on Edit option. Type the command as “Get-AzureRmVM”. Then to
test the command click on “Test Pane” as highlighted below –
Click on Start button in Test Pane window to start the
execution. There you receive the error again – “Run Login-AzureAccount to login”.
Now here is the catch. Automation account runbooks runs in the background hence
they can’t throw a pop up wherein you can put up your credentials. So how do we
resolve it?
Solution is – Use Azure AD Service Principal
Service principal means you are treating an application
as a user and giving full access to it so that it can perform any action
against your azure subscription. As Azure subscription is always present in the
Azure Active Directory tenant; we must add the information of our application
in Azure AD tenant and this is nothing but the service principal.
So how do we create a Service principal? Well you don’t
have to create because it already exists if you have an Azure ARM automation
account created.
Open Assets -> Connections -> AzureRunAsConection.
This shows type as Azure service principal and there are many Ids present as
highlighted below –
Application Id is the one by which your Automation account
is identified as Service principal in Azure AD. Tenant id is nothing but Azure
AD id under which your subscription exists. Subscription Id is the actual Azure
subscription Id.
Let’s verify this exist in your Azure AD as well. For
the same, on Azure portal open Azure Active Directory -> App Registrations.
You will see an Application Id same as what we have observed under automation
account connection.
This means AzureRunAsConection of automation account is
acting as Service principal. Hence it can be used for authentication against
the subscription and also to perform operations against our azure subscription.
With this let’s write some PowerShell code to perform authentication using
service principal.
Authenticating using Service principal
Code for authenticating Azure Automation account runbook
using Automation connection as Service principal is shown below –
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection
"
$servicePrincipalConnection = Get-AutomationConnection
-Name $connectionName
"Logging in to Azure..."
$account =
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage =
"Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message
$_.Exception
throw $_.Exception
}
}
Write-Output $account
Add above code segment in any runbook you wish to in
Azure Automation account and you will never receive error of “Run
Login-AzureRMAccount to login”.
I did the same in my sample runbook and VM list
received. Below is the output –
That’s all folks.
logins, but when doing :
ReplyDeleteFind-AzureRmResource i get: Run Login-AzureRmAccount to login.
this one blog post was more useful than dozens of official Microsoft docs. Thank you!
ReplyDeleteHelped me out, thank you!
ReplyDelete^exactly!
ReplyDeleteThank you, you set me into the right direction. I was leveraging your code block to do some stop/start azure database, and it seems I need to use Connect-AzAccount instead of AddAzureRMAccount. If interested, it is documented here: https://stackoverflow.com/questions/55909242/how-to-use-connect-azaccount-in-a-powershell-runbook-with-an-automationconnectio#
ReplyDeleteSincere Regards. Searched for last 2 days, no clue. This was a life saver, indeed. Thanks a lot again.
ReplyDeleteGood one to understand!!!
ReplyDeleteHi, Thanks for the code snippet. When I run always I am getting Connection Name not found. How to get the value of connectionName
ReplyDelete