So you have set up an Azure lab, but you are now starting to see your billing costs are higher than you anticipated, or maybe you are getting tired of logging in to the Azure portal, every morning and every evening to start and shutdown your lab/Virtual Machine(s). Unfortunately there is no UI in the Azure portal that allows you to input a start and stop time for your Virtual Machines to be powered on and/or off, however there are some clever workarounds! Below are the steps I have taken to automate this problem.
Of course you will need an Azure environment, at least one Virtual Machine and some (very) basic PowerShell knowledge.
For starters, I have already built my VM, and I have already created an account that is a member of the domain administrators.
- Log into the Azure portal and expand the Browse All icon, located on the left pane.
- Select Automation Accounts and create a new Automation Account. I called mine “MachineStartStopAutomation”.
- Next under the new account, select Assets
- Here we will assign credentials associated to this Automation account. Within Assets, select Credentials
- Once you have created the Credentials, next we will need to create the Runbook
- Go back to the Automation Account, and this time select Runbooks
- Provide some descriptive name for the Runbook. I used “Start<hostname>VM”. Also, I had some issues creating/editing the Runbook script when using the Graphical Runbook type, so I used the PowerShell Workflow. I would advise using the PowerShell Workflow option.
- Within the script, use the code similar here. Note, your workflow will be name of your Runbook name. Also, in line 5, the -Name <hostname> will be your VM you are interested in automating the PowerOn. To be safe, I specified the FQDN.
- Once complete, you can test and/or publish the Runbook. (You will need to Publish the Runbook in order to make use of it)
- Next you will need to create a schedule. Go back to the Runbook, and select Schedules
- Since I would like to start this VM daily, I set it for daily Recurrence.
You will now need to repeat all the steps above (starting at step 7) to create an automated shutdown Runbook. The PowerShell code will be almost exactly the same, but you will make use of the “Stop-VM -Name <hostname>” Cmdlets.
Once complete, your new Automation Runbook should look similar to this. Hopefully this will keep your Azure billing costs down, and hopefully no more daily/manual starting and shutting down your lab/Virtual Machine(s). =)