Shane Bartholomeusz

Geek and lover of all things tech related

Azure – Scheduled VM Stop/Start

Overview

One of the most common Azure administration tasks is scheduling an auto stop & start of your Azure VM’s to save you a few $$$.

Here’s a quick post showing how you can do this.

UPDATE: Azure now provides the ability to auto start and shutdown ARM based VM’s. If you are however still using a Classic VM, you can follow the below guide.

Azure Logo

How?

There’s a number of ways you can do this however I’ve chosen to reuse a PowerShell script from the Azure script gallery. Why reinvent the wheel, right?

  1. First create an Automation Account within Azure – it will execute your start / stop script. I’ve called my account “AutomationAccount”, creative huh 🙂
Azure screenshot
Azure screenshot
  1. Open the Automation Account you’ve just created and navigate to Runbooks > Browse From Gallery.
  2. Find and import script “Scheduled Virtual Machine Shutdown/Startup”. The script should now appear in the Runbooks window.

Script documentation can be found here – https://automys.com/library/asset/scheduled-virtual-machine-shutdown-startup-microsoft-azure.

Azure Portal Screenshot

4. Edit the Runbook and then click Publish

  1. This script will shut down any Azure resources with a Tag containing the keyAutoShutdownSchedule” and a value specifying the shut down schedule. Add the tag name to any resources you want to automatically startup / shutdown. Refer to the above documentation link for more information.

In my case, I’ve added the following Tag to the Resource Group containing my VM. I want my VM to be shut down on weekends

  • Key: AutoShutdownSchedule
  • Value: Saturday, Sunday
Azure Portal Screenshot
  1. Next, create a new user in Active Directory. This user will execute your stop / start script. At the time of writing, this could only be managed through the old Azure portal.
Azure Portal Screenshot
Azure Portal Screenshot
Azure Portal Screenshot

7. Navigate to Settings > Administrators and Add register the user you just created as an Administrator of your Azure subscription.

Azure Portal Screenshot

8. Login with the new user you’ve just created. You should be prompted to enter a new password at first login.

  1. Next you’ll need to instruct the Azure Automation service to use the correct user account when executing your script. Log back into your main Azure account and go back to your Automation Account. Open Settings > Assets > Credentials. Add a new Credential. The User Name and Password will be as per the Active Directory User Account previously created.
  2. Everything should be in place now so lets test the script out. Open your Automation Account and Edit the Runbook you previously created, then open the Test Pane. This will allow you to run the script and troubleshoot any issues.
Azure Portal Screenshot
Azure Portal Screenshot
Azure Portal Screenshot
  1.  Assuming it’s running ok, schedule the Runbook to run as frequently as possible i.e. run every hour on every day of the week.

Remember: this schedule is NOT the schedule for stopping/ starting your VM’s. When the Runbook launches, it will look for any resources with the appropriate Tag and then lookup the stop / start schedule from the Tag value. This setup provides you with more flexibility i.e. if you have complex scheduling requirements.

To check whether it’s running properly, you can view the logs from the Jobs panel within the Automation Account settings blade.

Azure Portal Screenshot

Conclusion

Well that’s it. It’s quick and easy to setup.

As I mentioned before, there’s a variety of ways to do this however this is my preferred way.

Shane Bartholomeusz

1 Comment

  1. Nice walkthrough! An alternative for those who prefer a non-scripting solution is a service called VMPower (https://vmpower.io/). There is a calendar feature that makes automating shutdown, startup and resize of VMs pretty intuitive to do in <15 minutes. With the GUI, it's very accessible for non-technical team members to manage too.

    Also for the admins juggling cloud subscriptions across multiple cloud providers (Azure, AWS, & GCE) , you can manage all of your VMs in one unified dashboard.

Leave a Reply

© 2024 Shane Bartholomeusz

Theme by Anders NorenUp ↑