This post discusses a relatively simple piece of code used to start or stop one or more Azure virtual machines within a defined time window.
Why am I doing this?
So, I have come up with this code to solve an issue with one of our customers wanting to de-allocate their VMs during non-business hours using PowerShell; the alternative would be to login to the Azure portal and selecting start…
…or shutdown everyday; not going to happen.
How does it work?
So this post references an earlier article I wrote around authenticating against Azure when scripting – see here.
The script reads in data from a csv file called AzureIaaSVMs.csv; the script looks for this file within the directory the script is being run from.
Once the data has been imported it works out what day it is and then checks whether that particular Azure VM should be running or shutdown and does the necessary.
I have this script scheduled to run every hour using Task Scheduler. The command for example would be c:\…\powershell.exe -file c:\…\deallocateAzureVMs.ps1
So the this script is relatively simple at the minute but does a job. The script could be extended to…
…report failures using email notification
…include multiple subscriptions
…be more granular regarding start or stop time and improved time logic
The current version of the script can be downloaded from GitHub here – https://github.com/heathen1878/AzureVMControl