Schedule to shutdown (deallocate) azure vm during night time

Published November 12, 2014 12:51 pm

Recently we have started the service trial. VM is unused past 11PM and should be shutdown to save dollars. so, I set out to figure out how to do it.

On net search – I hit across azure automation services which let you schedule your powershell script for process automation. It was not straight forward to get going with it without spending day or two to understand it and its billing. I was simply looking for a quick solution without any learning curve. I know shutdown /s /t 0 shuts down a windows computer. Can I schedule it? I found that schedule task mmc is available in windows azure vm. Hence, I created a basic task and scheduled it for 1 am to shutdown.

In morning, I did find the VM in stopped state. but I have typically seen the VM in stopped(deallocated) state when I shutdown using azure management portal. I looked around and found that stopped VM is still billed. stopped(deallocated) VM is not billed. I have to now figure out how can I de-allocate it in midnight? azure powershell and cross platform CLI both provide commands to shutdown VM (including deallocation). Download of azure powershell is ~120mb whereas azure-cli (cross platform) is available through npm (nodejs). I have nodejs on my devbox and it is our server platform too. npm install azure-cli -g command installed the command line in less than a minute. command line is also very intuitive and simple to follow.

azure account download 
-- launch the browser, login, download & save the azure account publisher settings.
azure account import <path to publisher settings file> 
-- imports setting for the subscription.
azure vm shutdown -v <vm name> 
-- shutdown vm including deallocation.

Now, I need to figure out the solution to trigger this command at midnight. My laptop is also sleeping at that time including me. Windows task scheduler also have option to wake computer & ensure network connection before launching a task. It requires changing advanced power settings on the computer – to enable wake up alerts. I tried to schedule it (using windows task scheduler) in the VM itself which did not require all of this and it did work to de-allocate itself!

ps/ I have set few additional settings in the task. 1) run with highest privileges 2) run whether user is logged on or not. It asks the admin password to schedule the task. Without the password – it can’t have network access which is required for the command line to issue the command.

Leave a comment