You can create Azure Automation custom runbook to control stop and start of your Azure Virtual Machines. You need to have a Azure subscription and Automation account (click here for steps to create one) to hold the runbook and authenticate to Azure resources. This account must have permission to start and stop the virtual machine. Also have virtual machines ready to test the runbook.

Benefits of this custom runbook script provided in this article are as below-

  1. The runbook find the VMs and starts and stop then in same run. Different runbook calls are not made. This same running time and is cost effective.
  2. The result of jobs run is shown in one place consolidated.

Scenarios covered by this runbook

  1. Provide the list of VMs in variable and it will start & stop them.
  2. If VM list is not provided and the variable “VMstoStartStop” is empty, then it finds all VMs in resource group names provided in External_Start_ResourceGroupNames and External_Stop_ResourceGroupNames
  3. If resource group parameters are empty then it finds all VMs in the subscription and execute start & stop on them.
  4. Under same resource group different VMs can be started and stopped at different time.
  5. If resource group is provided but not all VMs need to be started/stopped in that group then add those VMs in variable External_ExcludeVMNames to exclude for the start/stop logic. Rest of the VMs will be stated/stopped as per schedule.

Steps:

>>Login to Azure portal and open your Automation account

>>Under Process Automation, click Runbooks to open the list of runbooks

>>Click on + Create a runbook button to create a new runbook

>> Provide and Name and Runbook Type as PowerShell for the new runbook and then click Create button.

>>Your runbook is empty. You can download the script code from here and paste in it. Then Save and Publish it. [Note: this script is created by using the scripts provided by Microsoft Azure Automation].

>>Now in Automation Account, under Shared Resources click Variables and add these variables [Note: while creating the variables you have to provide some Value, later you can delete the value if required]

>>Explanation of these variables-

-  External_Start_ResourceGroupNames    :  ResourceGroup that contains VMs to be started. Must be in the same subscription that the Azure Automation Run-As account has permission to manage.
-  External_Stop_ResourceGroupNames     :  ResourceGroup that contains VMs to be stopped. Must be in the same subscription that the Azure Automation Run-As account has permission to manage.
-  External_ExcludeVMNames              :  VM names to be excluded from being started.
The first 3 variables should be of exactly same name but the last variable "VMstoStartStop" can be on any name. You will provide the list of the VMs to be started or stopped.

>> Now we will create schedule for the start & stop of VMs. Under Shared Resources, click on Schedules and then click + Add a schedule and provide a Name and time and recurrence frequency for the schedule and Click Create

>>Similarly create schedule for Stop_VM

>>Now we will attach these schedules with the runbook. Go to Runbook and then open the runbook, click schedules and then + Add a schedule

>>Now link Start_VM and then provide parameter values. ACTION can have values like start or stop. In VMLIST provide the variable name “VMstoStartStop” which contains the VM names. Click create

>> Similarly attach the Stop_VM and provide the values as shown below

>>Now your runbook will execute the start of VM and stop of VM as per the schedule attached. The results of runbook execution can be seen under Process Automation –> Jobs.