Welcome to the navigation

Lorem magna anim dolore cillum fugiat officia nostrud dolor reprehenderit et enim consectetur voluptate aute ipsum consequat, adipisicing cupidatat incididunt ut ullamco sint deserunt veniam. Cupidatat consectetur dolor eiusmod aute exercitation in deserunt est mollit qui consequat, ut ut ex minim sit do velit eu proident, non cillum dolore ad

Yeah, this will be replaced... But please enjoy the search!

How to stop (deallocate) an Azure VM at a given time using PowerShell

When using Azure you can either stop or stop a Virtual Machine. When stopping a VM the OS in the VM is stopped and the VM services are unavailable, but the VM continues to reserve the compute and network resources that Azure provisioned. However, when you stop (deallocate) a VM you not only stop the VM’s OS, you also free up the hardware and network resources Azure previously provisioned for it (a process called deallocation).

First up. There are several ways to automate this is, I prefer the option where I manually start my VM and decentralize the shutdown process using the Windows Task Scheduler. If I need a bit more powerful option I use the runbook Assert-AutoShutdownSchedule. This guide will however only cover the basics when using PowerShell and Task Scheduler.

So you've shut the VM down?

I've seen this quite a few times when people simply shut down the VM using the menu or a PowerShell command like Stop-Computer. This will result in a notification looking like this 

This virtual machine is still incurring compute charges. To avoid these charges, use the portal to stop the virtual machine.

If I have a look at the VM using PowerShell this is what I see

$vmname = 'myvm'
$vm = Get-AzureVM | Where-Object { $_.Name -eq $vmname }
Write-Host $vm.OperationStatus $vm.PowerState $vm.Status

OK Stopped StoppedVM

The command $vm.Status is what we are looking for, the result above tells us StoppedVM which means the machine is still provisioned and it will cost you the computing charges to keep it in this state. The result we are looking for is $vm.Status to be StoppedDeallocated, the Azure Portal should report the VM like this

To fix this set up your PowerShell for Azure

You'll need to install this on the VM but I recommend you to install it on your dev/work-box as well.

Follow this guide to install the Azure commandlets for your environment, https://azure.microsoft.com/en-us/documentation/articles/powershell-install-configure/

Download your Azure publish settings file

The Azure publish settings file will help you to automate connections to your Azure services. You'll get it by with this command in PowerShell

Get-AzurePublishSettingsFile

This should take you to an url like https://manage.windowsazure.com/publishsettings/index?client=powershell, if you aren't logged in you'll need to logon to a account with access to the VM.

Set up the script

Place the .publishsettings file on the VM (and your dev/work-box). Begin by importing the publishsetting file 

$publishsettings = 'C:\Automation\Azure.publishsettings'
Import-AzurePublishSettingsFile $publishsettings

This command will tell you which subscriptions your account can use, select the one that is suitable and follow up with this command

Select-AzureSubscription -SubscriptionId "1gf3f71d-c0g7-412f-aghr-6af042bb1231"

$vmname = 'myvm'
$vm = Get-AzureVM | Where-Object { $_.Name -eq $vmname }
Write-Host $vm.OperationStatus $vm.PowerState $vm.Status

If the VM is running and all went well you should get the the status OK Started ReadyRole

Stop the VM

Run the following command to stop the VM

Stop-AzureVM -ServiceName $vm.ServiceName -Name $vm.Name -Force

Please note that this is a pretty harsh way to turn off the VM but at the moment it is the only legit way. 

When checking the VM from your local box the status should now be OK Stopped StoppedDeallocated

Automate the shutdown

Begin by wrapping the PowerShell script in a file looking like this, I saved it in the folder c:\Automation\shutdown.ps1

Import-AzurePublishSettingsFile 'C:\Automation\Azure.publishsettings'
Select-AzureSubscription -SubscriptionId "1gf3f71d-c0g7-412f-aghr-6af042bb1231"
$vmname = 'myvm'
$vm = Get-AzureVM | Where-Object { $_.Name -eq $vmname }
Stop-AzureVM -ServiceName $vm.ServiceName -Name $vm.Name -Force

In order to automate this script I use the built-in Task Scheduler in Windows.

  1. Create a basic task and name it Shutdown or similiar, press next
  2. Select when the task shall start, I use daily, press next
  3. Set the time you want to shut down the machine everyday, recur every 1 day, press next
  4. Select Start a program, press next
  5. Enter powershell in the Program/script box and the file and its path as the argument
  6. Finish and open the properties for fine tuning

The most important part when fine tuning the shutdown is in the Conditions tab, to prevent the computer from shutting down if you are working on it configure the idle options as you prefer.

Here's the rest of my scheduler config

 

Cheers!

Please note that this portal is a BETA site , I will continuously improve all functionality and performance during the coming weeks / the author