Microsoft Azure Automation With RunBook Using PowerShell

In this article I will show you how to create Microsoft Azure Automation Runbook using PowerShell. Create Automation service in Azure Portal and Click on Runbook as shown in the below image. Then click on RunBooks and Add a RunBook.

Azure

Click on Quick create and Create a new runbook, provide name of runbook and select RunBook Type – PowerShell and provide description of this runbook and click on create.

Azure

After creation of second runbook the editor will open in the left side CMDLETS, RUNBOOKS and ASSETS are available and right side PowerShell Script editor is available. We need to write a Powershell script in that, and click on Save Button shown in below snap.

Azure

PowerShell Script

  1. Param(  
  2.     [string] $VMName, [string] $ResourceGroupName)  
  3. $Conn = Get - AutomationConnection - Name AzureRunAsConnection  
  4. Add - AzureRMAccount - ServicePrincipal - Tenant $Conn.TenantID `  
  5.   
  6. -ApplicationID $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint  
  7.   
  8. Start-AzureRmVM -Name $VMName -ResourceGroupName $ResourceGroupName   

After inserting above code click on save button and click on test pane for testing and run the script. In this above PowerShell script we need to enter parameters from user regarding which VM and VM Resource Group of that VM we would like to start using. Start-AzureRmVM command. First, as you can see in the below image, my VM is stopped and deallocated.

Azure

Now after clicking on the Test pane of my runbook, there is a column called parameters on the left side in which we need to enter our VM name and VM Resource Group name of which we want to start that VM using runbook. After providing the parameter's name click on Start button. It will take 3-4 minutes and will show you output as shown below.

Azure

If everything is ok than you will get Completed status in green color and True – OK Ok status in test pane. After that you can check VM Status:
Azure

Now if you want to auto schedule this runbook you need to publish it and after publishing it,  you will find an option called schedule on the top, click on that and you can schedule this runbook once or with recurrence with Date and Time. You can also use the webhook option for the same using HTTP POST to a URL.