IMPORTANT ANNOUNCEMENT FOR OUR READERS!
AskPFEPlat is in the process of a transformation to the new Core Infrastructure and Security TechCommunity, and will be moving by the end of March 2019 to our new home at https://aka.ms/CISTechComm (hosted at https://techcommunity.microsoft.com). Please bear with us while we are still under construction!
We will continue bringing you the same great content, from the same great contributors, on our new platform. Until then, you can access our new content on either https://aka.ms/askpfeplat as you do today, or at our new site https://aka.ms/CISTechComm. Please feel free to update your bookmarks accordingly!
Why are we doing this? Simple really; we are looking to expand our team internally in order to provide you even more great content, as well as take on a more proactive role in the future with our readers (more to come on that later)! Since our team encompasses many more roles than Premier Field Engineers these days, we felt it was also time we reflected that initial expansion.
If you have never visited the TechCommunity site, it can be found at https://techcommunity.microsoft.com. On the TechCommunity site, you will find numerous technical communities across many topics, which include discussion areas, along with blog content.
NOTE: In addition to the AskPFEPlat-to-Core Infrastructure and Security transformation, Premier Field Engineers from all technology areas will be working together to expand the TechCommunity site even further, joining together in the technology agnostic Premier Field Engineering TechCommunity (along with Core Infrastructure and Security), which can be found at https://aka.ms/PFETechComm!
As always, thank you for continuing to read the Core Infrastructure and Security (AskPFEPlat) blog, and we look forward to providing you more great content well into the future!
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