Here in PFE, we LOVE our labs. That should be apparent by all the posts we have on the subject. Labs are the best way to dig in and discover how things really work before going into production, or if you are just curious about how something looks or functions. We’ve had several posts on Azure VMs, and while it is easy to get them up and going, every minute a VM is running it is costing money. While we love our labs, we also want to get as much as we can out of the lab budget!
Below I’ve outlined the steps on how to automate the starting and stopping of a VM.
Here are the pre-requisites for my demo:
· I already have a VM created called “Test-VM-Blog” in the Central US region. This VM is the only VM in a Service also called “Test-VM-Blog”. The VM is currently in a “Stopped (Deallocated)” state.
· I have created a Co-administrator Account in Settings/Administrators section of the portal called “azure-admin@<my tenant name>” to be used for this type of administration task.
Ok, let’s get started:
1. In Azure, click on the Automation icon on the left hand side.
2. Click the “Create an Automation Account” option. Enter a name and choose a region. Note: The region does not have to be the same as the VM Region. My VM is in the Central US Region, but I’ve selected “East US 2” for the automation account region.
3. Once it is done Creating, click on the Automation Account name you just created. In a few more steps we will create scripts that will start and stop the VM. In order for that to work, we need to supply an Azure account for the script to run as. To do this, go to the Assets tab at the top.
4. Click “Add Settings” at the bottom, and choose “Add Credential”.
5. For the “Credential Type”, choose “Windows PowerShell Credential”. Give it a name and description.
6. Supply the Co-Administrator account the script will be run as, along with the password.
7. Click on the Runbooks Tab.
8. To create a new Runbook, click on “New” at the bottom, then select “Automation”, “Runbook”, then “Quick Create”. Give the Runbook a name and description, then click “Create”.
9. Click on the Runbook you just created.
10. Click the “Author” tab.
11. Below is a simple script that will start the specified VM. First you need to supply the automation credential specified above, then login using that account. The last line is supplying the Service and VM name to start.
12. At the bottom, click “Save”, then “Test” and choose “Yes”.
13. Be patient and give the test a few minutes to run. You should then see some output like below, and your VM will be started.
14. Once you have verified the script ran successfully, then you can click “Publish” at the bottom.
15. Now click on “Schedule” at the top, and then choose “Link to a New Schedule”.
16. Give the Schedule a name and click the arrow.
17. If you want this to be a daily task, then choose “Daily” and specify a start date and time. This time is in your local computer’s time zone.
18. You can now go back and create a Stop task and script, using the same steps starting with step 8. The same script can be used as before, with a small change on the last line. Below is an example. The “-force” option is needed to suppress the confirmation question of the stop command.
I hope you enjoy experimenting with these simple automation tasks, and can expand your labs while staying within budget.
David “The Lab Rat” Hail