I am trying to execute a powershell script in azure automation account runbook test pane. In my powershell script, I am importing the modules, connecting automation account using managed identity and then trying to get recovery services vault properties of a particular subscription using cmdlet "Get-AzRecoveryServicesVault"
Powershell Script:
Import-Module Az.Accounts
Import-Module Az.Resources
Import-Module Az.Compute
Import-Module Az.Automation
Import-Module Az.Storage
Import-Module Az.KeyVault
Import-Module Az.RecoveryServices
Connect-AzAccount -Identity
$context = Get-AzContext -ListAvailable
write-output "context" $context
Set-AzContext -Subscription "<<subscriptionID>>"
$vaultName = ""
$vaultResourcegroup = ""
$containerCtx = Get-AzRecoveryServicesVault -Name $vaultName -ResourceGroupName $vaultResourcegroup
write-output "containerCtx" $containerCtx
According to the above code, this should show the recovery service vault properties as I need a vaultId to get containerslist. Below is the error I am getting.
error:
One or more errors occurred.
Not sure if this is related to permissions on managed Identity? azurepowershellpowershell-cmdlet azure-automation
Even I have got same error at first:
Then given the Back Operator role to managed identity of auto automation account to the Recovery Services Vault:
Later I used version also in scripts and it worked for me like below(modified yours script a bit):
Import-Module -Name Az.RecoveryServices -RequiredVersion 7.2
Connect-AzAccount -Identity
$vaultName = "stackdemo"
$vaultResourcegroup = "rgname"
$containerCtx = Get-AzRecoveryServicesVault -Name $vaultName -ResourceGroupName $vaultResourcegroup
Write-output "containerCtx" $containerCtx
Output: