Stopping specific VMs every night with Azure Automation

As many companies start to use Windows Azure as a development and test environment, the need for cost saving in the subscriptions start to become a real issue. I run into the same issue when trying to move at least part of my lab into Azure. I wanted it to be available, but I wanted some automated task to bring down everything that was non essential to the environment (my domain controller, for example).

So, I have decided to leverage a recently made public Azure feature called automation. Automation leverages PowerShell Workflow to perform automation tasks against your Azure environment.

Below you will find the steps on how to configure it.

Initial Setup

In order to run the workflow against your Azure VMs, you will need an Azure credential (AD User) that is an administrator of your subscription. So I went ahead and created a new AzureAdmin in my default directory and assigned subscription administration rights.

If you don’t know how to do that, you can check this post.

Now that you have your user, you can go ahead and create your automation account. For that go to the Automation section in the portal:

 

image

You should see something like this, if you never configured an account.

image

Create and account and choose a region.

image

Documentation doesn’t say specifically, but I assume the region, among other things, will define the timezone for the scheduling feature.

image

The last step before you actually create your runbook is to create a credential asset. For that you must go to your automation account->assets option:

image

Click on Add Setting:

image

Add a credential:

image and image

 

Finally, let’s create the Runbook!

image

I’m using a runbook available in the gallery and then I will do some customizations. Follow the steps below:

image

image

image

image

image

image

As you probably noticed, the Runbook will stop ALL VMs,so, that doesn’t work for me. So, I have customized the script lightly to allow for an exclusion list, in form of an Array:

$exceptionlist = @(“fehsedc02”)

I have then customized the PS line to exclude the vms in the list:

Get-AzureVM | where{$_.status -ne ‘StoppedDeallocated’ -and $_.Name -notin $exceptionlist} | Stop-AzureVM –force

image

I have added an extra line, just to show the VMs that remained started after the procedure.

Let’s give it a spin:

image

image

 

image

image

image

The cherry on the top of the cake is the schedule. What I wanted is the VMs to stop at 8:00 PM every night. So, here it goes:

In the Runbook section, click on Schedule:

image

Name it

image

Not a lot of options, but enough for my purposes:

image

IMPORTANT: Initially, I assumed it would respect the time zone of the Automation account, but in fact, it seems it will respect the time zone by checking what is set in the browser  (or the computer). Namely, where you are. If you need it to be different, it seems you’ll need to change you time zone or calculate the hours manually.

Also note the scheduler won’t respect daylight saving time.

A few hours later:

image

image

image

There you go. You can go to sleep safely without the fear of spending a lot of money on your subscription!

 

Hope this helps