Azure Runbook – Powering off and on a VM Automatically
Keep in mind, runbooks are just powershell, don’t let anything here scare you. This is basically a glorified scheduled task running a powershell script that can take some inputs via parameter calls.
Since this codebase is going to get a bit long and there’s much more I want to do to it, I’ve dropped it on GitHub. Feel free to submit modifications to it.
I currently have two scripts. Startup and Shutdown. I have desires to combine it to one, make it a bit more modular and add functionality such as ordering the shutdown/startup sequence. All of these are pretty simple to do with the code.
What does the script do, exactly? Start-AzureRMVM and Stop-AzureRMVM, in a more complex way. Well there are 3 parameters (variables) currently:
VMName – the specific name of the vm to shutdown
ResourceName – The specific resource group to shutdown (yes- all vm’s in a resource group). Useful if you have a resource group full of test vm’s.
PowerStatusTag – Power cycle by tag. I create a tag called “PowerStatus” with a value “AzureAutomatedShutdownStartup” to do this. It will look for any VM in the subscription with that tag value to down. Lots of info on tags here: https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-using-tags
One big note: If you don’t provide any parameters, this script will down all VM’s in your subscription. Useful if your entire subscription is a dev/test environment.
The parameters are set in the schedule. So, don’t worry too much about what to do with these until you click schedule – then worry about them.
So, lets get started. First thing first. If you don’t have an Azure Automation account – create one. The defaults are fine. Micorosft has a good guide on creating as well. https://docs.microsoft.com/en-us/azure/automation/automation-create-standalone-account
Now that the Automation account is created, lets go in and add our runbooks. In the automation account, select Runbook under process automation, then hit the plus sign to add a runbook.
Do this for both the Start and Stop scripts.
We’ll do most our work from within the runbook.
Inside the runbook, are a few key commands. Edit – to edit the powershell runbook. Note when you’re done editing you need to save it and publish it so that its usable. Also, within Edit is the option to test. Test will run the runbook, allowing you to feed it the parameters. Note – test really runs it, its not just a test.
Here’s a sample of what the test looks like:
Schedule does exactly that – to schedule the runbook to run when you want it to.
Let’s go into schedule – we’re going to create two. One to power off nightly and one to start up every morning.
In the automation account, go into schedules, click the plus sign to add a schedule.
Configure the schedules to look like as follows:
Now, lets go back in the runbook and click that schedule button we talked about.
Select the schedule (one of the ones we just created). I kept it simple, for the start script, i’m using start. for stop i’m using stop schedule.
Select the parameters. See note from above. You need to set parameters at schedule time. The runbook pulls the parameters that you set in the schedule, no where else.
You can always inspect what is going on by drilling in
If you scroll down you can see the set parameters.
You can also disable the schedule by selecting the Enabled (yes/no) option.
Lastly, keep an eye on what the automation is doing.
Click the pie chart in the automation account and drill into the details.
Here’s a few sample events.
Good luck 🙂