Azure Resource Manager – Automation

Azure Resource Manager – Automation

Azure Resource Manager – Automation

Azure Automation is very effective as it lets you run powershell commands or workflows inside Microsoft Azure and execute the required set of actions at scheduled time. We will see step-by-step action in this post.

Login to Azure Portal – and create Automation account

Azure Automation has two major components –

  1. Runbooks
  2. Assets – Assets contain the following
    1. Certificates – Allows to save Azure Certificate for Connections
    2. Connections – Provides Azure Connection with certificates and Subscription ID
    3. Schedule – Contains the schedule for RunBook to run at specific time
    4. Credentials – Saves the credential
    5. Modules – Placeholder for Azure Powershell modules to import and use in Runbooks
    6. Variables – Contains Powershell variables

We will now create a Credential, Runbook and Schedule that will Stop all Virtual Machines at a specific time interval.

Create Credential – Go to your Azure Automation Account -> Assets -> Credential -> Select Add a Credential to add valid credential that has access to Azure Resource that you want to manage.



Once Credential is added, now it is time to create a new RunBook. Again, go to your Azure Automation Accounts -> Runbooks -> Add Runbook -> Type Name and Select PowerShell Workflows runbook to create

Azure Automation has 3 type of Runbooks

  1. PowerShell – A normal PowerShell where you can import modules and execute PowerShell commands as you would normally do
  2. PowerShell workflows – Workflows provide modern features to PowerShell like running commands in Parallel, we will see this below
  3. Graphical – This adds additional Runbooks or Assets and you can create a checkpoint or condition for Runbook to continuously run until condition is satisfied (Not explained in this blog)


Once the Runbook is created, select the Runbook and click on Edit. Then type the code shown below to login to Azure Account, Select Subscription and Stop the VMs simultaneously.



workflow Stop-MyVMs
#Get the credential with the above name from the Automation Asset store
$Cred = Get-AutomationPSCredential -Name 'MyLoginID'
if(!$Cred) {
Throw "Could not find an Automation Credential Asset named '${CredentialAssetName}'. Make sure you have created one in this Automation Account."
#Connect to your Azure Account
$Account = Add-AzureRmAccount -Credential $Cred
if(!$Account) {
Throw "Could not authenticate to Azure using the credential asset '${CredentialAssetName}'. Make sure the user name and password are correct."
#Select your Subscription
Select-AzureRMSubscription -subscriptionId <your-Subscription>
$VMs = Get-AzureRmVM -ResourceGroupName KalingaLabResGrp #Get all the VMs you have in your Azure subscription
Write-Output "No VMs were found in your subscription."
} else
Foreach -parallel ($VM in $VMs)
Stop-AzureRmVM -ResourceGroupName KalingaLabResGrp -Name $VM.Name -Force -ErrorAction SilentlyContinue

Test the Runbook

Once the code is ready, save the Runbook and click on Test Pane to test run the code and ensure its working.


Once Runbook is successfully tested then Publish the Runbook.


Schedule the Runbook

Once published, now schedule the Runbook to run at specific time.

Go to your Azure Automation account, and select Runbook. Now click on Schedules

Note that you will not be able to create Schedule for Runbook that is not PUBLISHED.

You can only add already created Schedules. If not, already created then you need to create one, as shown later.


Create a Schedule

Go to your Azure Automation Account, and then go to Assets to create a schedule.



This completes the Azure Automation.

About The Author

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *