In this post I’ll show how to setup Azure VMs turn off & on by schedule using Azure Automation which is really useful for cost saving.
We’ll use dozen of actions to manage this task.
However, not all actions can be managed with PowerShell, so we’ll use old Azure portal a few times.
Initially I used one big script, but for this demo I decided to split it to make things simple.
In this particular example I’ll use two VMs running in the same cloud service.
Create management certificate
We’ll use Windows-based machine for all actions in this example, so we’ll use makecert utility (if you don’t have it yet, just install Windows ADK).
Upload management certificate to Azure
This step requires manual action, so let’s go to the Old portal > Setting > Management certificates and click Upload:
If you find how it can be done with PowerShell please let me know.
Create Automation account
We’ll start with Azure PowerShell now (if you don’t have it, you can use this link – https://azure.microsoft.com/en-us/documentation/articles/powershell-install-configure/)
Let’s perform login to Azure:
So far we’re ready to create Azure automation account and assign certificate:
Create Runbooks and Schedule
We’ll use two scripts to start VM, strictly speaking, first one is Azure runbook and second is for pushing runbook to Azure. With stop VM it’s exactly the same.
I don’t like to keep stuff hardcoded, so some variables will be placed in Automation account Assets and some will be suited as parameters.
Here’s another problem with PowerShell – I wasn’t able to put variable content with cyrillic symbols. It’s possible to find workaround, but let’s just save some time and use the old portal again.
Go to the Automation > automationaccount > Assets and click Add setting > Add variable (string):
Let’s take a look at Azure Start VM Runbook:
Azure Stop VM Runbook is very similar, but we don’t want to lose our IP addresses (StayProvisioned parameter is used for it):
We’ll push Start VM Runbook & set execution schedule using createStartRunbook script and here it is:
And almost the same for Stop VM:
You can execute runbook manually and see that it works:
I hope this info will be useful for you, and if you need any help feel free to use contact from on the main page.