2

I'm trying to use Azure Automation Account to run some jobs, but I'm struggling with the AzureRM Module installed. I manually installed some Az Modules but PowerShell ISE still using the AzureRM, like in trying to establish RunAs connectio below,

$RunAsConnection = Get-AutomationConnection -Name AzureRunAsConnection;try {$Login=Add-AzureRmAccount -ServicePrincipal
-TenantId $RunAsConnection.TenantId -ApplicationId $RunAsConnection.ApplicationId -CertificateThumbprint $RunAsConnection.CertificateThumbprint -ErrorAction Stop}catch{Sleep 10;$Login=Add-AzureRmAccount -ServicePrincipal -TenantId $RunAsConnection.TenantId -ApplicationId $RunAsConnection.ApplicationId -CertificateThumbprint $RunAsConnection.CertificateThumbprint};Set-AzureRmContext
-SubscriptionId $RunAsConnection.SubscriptionID

Then, I have to modify it manually to below code and it works.

$RunAsConnection = Get-AutomationConnection -Name AzureRunAsConnection;try {$Login=Add-AzureRmAccount -ServicePrincipal -TenantId $RunAsConnection.TenantId -ApplicationId $RunAsConnection.ApplicationId -CertificateThumbprint $RunAsConnection.CertificateThumbprint -ErrorAction Stop}catch{Sleep 10;$Login=Add-AzAccount -ServicePrincipal -TenantId $RunAsConnection.TenantId -ApplicationId $RunAsConnection.ApplicationId -CertificateThumbprint $RunAsConnection.CertificateThumbprint};Set-AzContext -SubscriptionId $RunAsConnection.SubscriptionID

How to fix this and how to force the automation account to use Az instead of Azure RM ?

Thanks!

Nurhun
  • 475
  • 1
  • 9
  • 21

1 Answers1

3

After import Az module, you can use Connect-AzAccount for authentication, then use other Az cmdlet directly.

An example is using Get-AzWebApp(need to import Az.Websites modules as mentioned before) to get azure web app details, in automation runbook:

$connection = Get-AutomationConnection -Name AzureRunAsConnection
$connectionResult = Connect-AzAccount -ServicePrincipal -Tenant $connection.TenantID -ApplicationId $connection.ApplicationID -CertificateThumbprint $connection.CertificateThumbprint

#the cmdlet to get azure web app details
Get-AzWebApp -ResourceGroupName xxx
Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60