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.
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.
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.
PowerShell Script
- Param(
- [string] $VMName, [string] $ResourceGroupName)
- $Conn = Get - AutomationConnection - Name AzureRunAsConnection
- Add - AzureRMAccount - ServicePrincipal - Tenant $Conn.TenantID `
-
- -ApplicationID $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
-
- 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.
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.
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:
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.