Hi @Dimitris Krallis ,
Welcome to Microsoft Q&A! Thanks for posting the question.
The steps below are specific to PowerShell type runbook. For other type of runbooks, the steps might vary depending on the available modules.
1. Ensure that System assigned OR User assigned identity is enabled. You can verify it by going to the "Account Settings --> Identity" blade in Azure Automation account. This identity will be used for authenticating with Azure Resource Manager for querying the required values. Ref: Quickstart - Enable managed identities for your Automation account using the Azure portal
2. Add the required permission for the Azure Automation identity. Perform the following operation for it:
> a. Go to "Access control (IAM)" option in Azure Automation account --> "Role assignments".
> b. Click on "+ Add", select "Contributor" --> "Next". Select the "Managed Identity" option and the respective identity of your Automation Account as shown below:
> c. Click on "Select" --> "Review + assign"
3. After performing the steps above, you can get the values stored in Azure Automation Account Variables using the Get-AutomationVariable
. You can defined your own set of variables with name and values. For the following example, I stored the Resource Group name and Azure Automation Account name in 2 variables. The resulting PowerShell runbook is as below:
# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process | Out-Null
# Connect using a Managed Service Identity
try {
$AzureContext = (Connect-AzAccount -Identity).context
}
catch{
Write-Output "There is no system-assigned user identity. Aborting.";
exit
}
# set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
#read variables from the Azure Automation Account Variable available under "Shared Resource" --> "Variables"
$RG_name = Get-AutomationVariable -Name ResourceGroup_Name
$AutomationAccount_Name = Get-AutomationVariable -Name AutomationAccountName
write-output "RG_name: $RG_name"
write-output "AutomationAccount_name: $AutomationAccount_Name"
You may also create PowerShell runbook with parameters as shown in this article.
Please let me know if you have any questions.
---
Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.