Azure virtual machines cost money especially when they run. Therefore, in development and test environments, it is often undesirable for the associated virtual servers to run day and night. Manual shutdown via the Azure portal is of course an option. But this is often forgotten (at least in my case) and either leads to unwanted costs or reduces the Azure budget to zero within a few days.

With Azure Automation this problem can be solved surprisingly easily in a few minutes.

Step 1: Create new automation account

Click on the green plus sign in the new Azure portal and search for “Automation”.

Give a name and – if desired – create a new resource group.

Of particular interest is the item “Create Executing Azure Account”. This does a lot of work for us, because accounts for Classic and ARM mode are created directly, which can be used later to run the runbooks.

Step 2: Import Runbook from catalog

Click on “Runbook catalogue” to open a large selection of ready-made Runbooks.
Search for the following entry (“Stop Azure V2 VMS”) and import it.

Then edit the runbook and publish it without any adjustments. Only after the publication it is possible to create a schedule or a webhook.

Step 3: Create schedule

Now that we have the Runbook, we need to decide when and how often to run it. Click on “Schedule” and create a new schedule.

Since we created the executing accounts when we created the automation account and our Runbook uses these accounts as default, it is not necessary to adjust the parameters.
With the configuration shown above, ALL VM’s in the subscription will now be shut down every evening at the specified time.

The Runbook offers additional parameters that can be filled in optionally. This can be used, for example, to shut down individual VMs or the VMs of a specific resource group.

Note: This runbook only affects second generation VMs, not “classic” VMs. However, there are separate runbooks for classic VMs.

Optional step: Set up webhook

Webhooks are a great thing: With a simple HTTP post I can run the Runbook from any application (no matter if Mobile App, Powershell etc.).

It is important to note that the generated URL is equivalent to a password. The security token is embedded directly into the URL. You should be accordingly careful when passing on the URL.

To create a new webhook, simply click on “Webhook” in the Runbook overview.

Please note: The URL will be generated automatically and displayed only once. As with the schedule, the parameters can all be left blank if ALL VM’s are to be shut down.
Once the webhook is created, a simple HTTP POST to the URL can be used to start the runbook.
I implemented this in my Powershell environment as a simple function, which is loaded automatically when starting Powershell. So I can start my SharePoint environment in Azure by calling “start-sp2016” and shut down all servers of the environment with “Stop-Sp2016”.