Geeks With Blogs
Thorvald Bøe
I love Azure!

As a developer, Azure is simply the greatest thing that has happened since intellisense. Today you are rarely building an individual program to be run at a single computer. Most apps today have some kind of consciousness to the world around it. Before Azure, this used to be a pain in the butt, because budgets rarely allowed for creating realistic development environments. Hence, you would typically set up dev environment in a single pc with everything running with admin privileges, and when the time came to deploy to production, nothing would work because the environment suddenly became more complex.

With Azure, this has changed completely. Need a test server? Just set it up. No need to fight for a thousand dollars budget for hardware, just fire up a VM and pay for when you use it. For a few bucks a month, you can have a small instance running 24/7, and most of the time we don't require 24/7 either, so the bill becomes even smaller.

That is, of course, if you remember to switch off your dev environment. Not everything in Azure is cheap either, and the larger the instance, the higher the cost. The currently largest VM instance (G5 - The "Godzilla" instance) costs almost $10 per hour. If you fire up one of these and forget to switch it off before going on vacation, you might have a $7k explanation problem for your boss a month later.

I don't use G5 instances, but I have a couple of servers set up in a network, that I use for testing stuff related to SharePoint OnPremise. It doesnt cost thousands of dollars for a month, but it is expensive enough that I want to switch it off when I don't use it. The problem is of course that other people are also using my environment, and even if I can remember to switch it off after use, they can still forget it. What we need is a simple, reliable way to automatically turn a VM or a set of VMs off when it is no longer used.

To make a long story short, I ended up with a solution that runs every day at midnight. So if I am working after midnight,  I have to switch my environment on again. It is rarely a problem. And it fixes the problem of leaving the environment on for weeks without remembering it.

To accomplish this, I did a few simple (and free) steps. 
1.You need a running VM instance to be used as a "gatekeeper". For this, I use the cheapest VM instance (A0) at around $14 per month. This one is running 24/7. I could of course switch this one off too, but then I need to remember to switch it on along with the rest of my environment, which kind of defeats the purpose
2.Install Azure powershell from here:
This will be the toolkit that we will use to do the job
3.Open a azure powershell window and type "Add-AzureAccount" to bring up the login dialog
4.Run Get-AzurePublishSettingsFile and store the settings file somewhere
5.Run Import-AzurePublishSettingsFile [full path of the import file you just downloaded]
This will set the sucscription in the publish settings file as the default subscription 
6.You can now do a manual test, by typing:
Stop-AzureVM -ServiceName [myVmHere] -Name [myVmHere] -Force
This should shut down the specified VM. If this is working, go to next step
7.Now you open scheduled tasks and add a new task that runs whenever you want, to shut down whatever VMs you want. 
In the action dialog, press the browse button to locate the directory of powershell.exe, usually found here: C:\Windows\System32\WindowsPowerShell\v1.0\
Specify powershell.exe as the executable. 
In the "program/script" field, and enter the "stop-azureVM..." command that you executed earlier in the options textbox. Remember to test your scheduled task when done to make sure it runs as expected.

Now you are done, and hopefully you will have no surprises waiting in your monthly Azure bill!

Posted on Friday, July 17, 2015 12:07 PM azure | Back to top

Comments on this post: Save money by automatically switch off your azure test environment

# re: Save money by automatically switch off your azure test environment
Requesting Gravatar...
Good article and very smart solution. We are working on something similar right now. Here is a website: It allows to simplify usage of AzureVM for testing and development purposes. You get a list of VMs. You may start, connect and then stop your VM.
Left by Denys on Nov 13, 2015 8:04 PM

# re: Save money by automatically switch off your azure test environment
Requesting Gravatar...
When the load test engine checks for the file, it will be redirected to the blob which has the right content
Left by directed on Jul 29, 2016 10:01 AM

# re: Save money by automatically switch off your azure test environment
Requesting Gravatar...
Thank you very much for the explanation. Will follow the latest article from you.
Engine Services
Left by Cara Cruz on Aug 11, 2016 8:07 AM

Your comment:
 (will show your gravatar)

Copyright © Thorvald Bøe | Powered by: