How to Autostart a VM on Azure with a Schedule

Tecsun Yeep
4 min readJul 26, 2020

I find it kind of awkward that Azure only allow configuring of auto-shutdown under VM’s Operations menu, but no auto-startup.

There is a tutorial about “Enable Start/Stop VMs during off-hours” in Azure Product Documentation (link at the end of this article), but I don’t like creating extra resources (Log Analytics and Azure Monitor) to achieve this simple function, plus the additional cost that might incurs. I’m gonna share how you could accomplish the same by using a Runbook.

With a good planning, you could achieve very decent costs saving over long run by doing so.

Let’s Start

First of all, an Automation account is required. Let’s create that by going to Menu and click on the Create a Resource.

Search for Automation and click Create.

In this step, you will need to select the corresponding Subscription, Resource Group and most importantly the Location where you VM located. If you select a wrong location, additional cost may be incurred. And make sure Create Azure Run As account is set to YES.

Once done, go to the newly created Automation Account, go to Left Menu > Shared Resources > Modules Gallery and search for “Az.”.

First, import Az.Accounts. Once it is done (and it must be done), then continue to import Az.Automation and Az.Compute. One thing to take note here, the import process might takes about 2 minutes or more. Even though the notification center shows it is completed, you might still encountering error message when you trying to import Az.Automation. In such case, check the Modules tab to verify the import status before continue. While waiting, don’t forget to grab this chance to do a 2-mins workout exercise (yeah, you could accomplish a 6-mins workout in total!).

At the time of writing, the version for all threes modules are:
Az.Accounts = 1.9.1
Az.Automation = 1.3.7
Az.Compute = 4.2.1

After that, create a Runbook.

Use PowerShell under Runbook type.

You will be landed in Edit Runbook page. Insert the PowerShell script below, make sure to change the Name and ResourceGroupName corresponding to your own setup. Then, run it under Test pane > Start.

$Conn = Get-AutomationConnection -Name AzureRunAsConnection

$connectionResult = Connect-AzAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint

Start-AzVM -Name ‘autostart-test’ -ResourceGroupName ‘tecsun-testlab

If you have done everything correctly, the output will show Completed. If you encounter an error, read the Troubleshooting section at the later part of this tutorial.

Now, close the Test Pane and click Publish.

Next, we are going to configure a schedule to this Runbook. Go to Link to Schedule > Link a schedule to your runbook > Create a new schedule.

Be sure to set Recurring and Recur every “day”. Expiration set to NO of course, unless you want it to expire.

Once created, be sure to verify that it is linked to your autostart-VM Runbook. You can do a check by running it manually. If it runs successfully, your VM will be started and you will see the automation job shows a Completed status.

We’re done now.

What’s More

You can modify the PowerShell script to include several VMs, or any post-actions you wish alongside with the auto-startup.

Troubleshooting

Runbook completed with error shown below? You might not have install the required Az modules.

Further reading:

--

--