Home > Blockchain >  Azure Connect-AzAccount in runbook with managed identity fails
Azure Connect-AzAccount in runbook with managed identity fails

Time:10-13

I am trying to execute a runbook in an automation account within azure.

I have set a managed identity following the instructions here, then i issue the following in my runbook:

Connect-AzAccount -Identity
Set-AzContext -Subscription Subscription1

As instructed here

But i get the following error:

Set-AzContext : Please provide a valid tenant or a valid subscription.
 At line:134 char:1
   Set-AzContext -Tenant $tenantId -Subscription $subscriptionId

I pass the tenantId and subscriptionId through as parameters, and have written them out to confirm they are correct.

Can anyone see where I am going wrong?

CodePudding user response:

Make sure you have up-to-date modules for Az.Accounts (2.10.2), Az.Resources (6.3.0), Az.Automation (1.8.0).

https://learn.microsoft.com/en-us/azure/automation/automation-update-azure-modules

CodePudding user response:

Please find the sample code below. Hope this helps!

$subscription = "000000-0000-0000-0000-000000000"
$identity = "000000-0000-0000-0000-000000000"

$null = Disable-AzContextAutosave -Scope Process # Ensures you do not inherit an AzContext in your runbook

$AzureContext = (Connect-AzAccount -Identity -AccountId $identity).context  # Connect to Azure with user-assigned managed identity

$connectionResult = Set-AzContext -Subscription $subscription -DefaultProfile $AzureContext
  • Related