Getting values from a Runbook dynamicaly and not hardcoded.

Dimitris Krallis 66 Reputation points
2022-02-03T16:32:05.667+00:00

I would like to know how I can get the RG and Automation Account Name as values dynamically when an Azure runbook is executed.
But if that is not possible, I would rather have the automation account and resource group be parameters instead of hardcoding the values in the runbook.

How can I accomplish this?

Thank you very much for your time.

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,368 questions
0 comments No comments
{count} votes

Accepted answer
  1. AnuragSingh-MSFT 21,546 Reputation points Moderator
    2022-02-04T11:44:28.637+00:00

    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:
    171402-image.png

    > 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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.