How to create an Azure Function App to Simultaneously Start|Stop all Virtual Mac ...

6th of January, 2017 / Darren Robinson / No Comments

Just on a year ago I wrotethisblog post that detailed a method to “Simultaneously Start|Stop all Azure Resource Manager Virtual Machines in a Resource Group”. It’s a simple script that I use quite a lot and I’ve received a lot of positive feedback on it.

One year on though and there are a few enhancements I’ve been wanting to make to it. Namely;

host the script in an environment that is a known state. Often I’m authenticated to different Azure Subscriptions, my personal, my employers and my customers. prioritize the order the virtual machines startup|shutdown allow for a delay between starting each VM (to account for environments where the VM’s have roles that have cross dependencies; e.g A Domain Controller, an SQL Server, Application Servers). You want the DC to be up and running before the SQL Server, and so forth and if I do all those the most important; secure itso not just anyone can start|stop my environments at their whim Overview

This blog post is the first that executes the first part of implementing the script in an environment that is a known state aka implementing it as an Azure Function App. This won’t be a perfect implementation as you will see, but will set the foundation for the other enhancements. Subsequent posts (as I make time to develop the enhancements) will add the new functionality. This post covers;

Creating the Azure Function App Creating the foundation for automating management of Virtual Machines in Azure using Azure Function Apps Starting | Stopping all Virtual Machines in an Azure Resource Group Create a New Azure Function App

First up we are going to need a Function App. Through your Azure Resource Manager Portal create a new Function App.


How to create an Azure Function App to Simultaneously Start|Stop all Virtual Mac ...

For mine I’ve created a new Resource Group and a new Storage Account as this solution will flesh out over time and I’d like to keep everything organised.


How to create an Azure Function App to Simultaneously Start|Stop all Virtual Mac ...

Now that we have the Azure App Plan setup, create a New PowerShell HTTP Trigger Function App.


How to create an Azure Function App to Simultaneously Start|Stop all Virtual Mac ...

Give it a name and hit Create.


How to create an Azure Function App to Simultaneously Start|Stop all Virtual Mac ...
Create Deployment Credentials

In order to get some of the dependencies into the Azure Function we need to create deployment credentials so we can upload them. Head to the Function App Settings and choose Go to App Service Settings.


How to create an Azure Function App to Simultaneously Start|Stop all Virtual Mac ...

Create a login and give it a password. Record the FTP/Deployment username and the FTP hostname along with your password as you’ll need this in the next step.


How to create an Azure Function App to Simultaneously Start|Stop all Virtual Mac ...
Upload our PowerShell Modules and Dependencies

Just as my original PowerShell script did I’m using the brilliant Invoke Parallel Powershell Script from Rambling Cookie Monster . Download it from that linkand save it to your local machine.

Connect to your Azure Function App using your favourite FTP Client using the credentials you created earlier. I’m using WinSCP. Create a new sub-directory under /site/wwwroot/ named “bin” as shown below.


How to create an Azure Function App to Simultaneously Start|Stop all Virtual Mac ...

Upload the Invoke-Parallel.ps1 file from wherever you extracted it to on your local machine to the bin folder you just created in the Function App.


How to create an Azure Function App to Simultaneously Start|Stop all Virtual Mac ...

We are also going to need the AzureRM Powershell Modules . Download those via Powershell to your local machine (eg.Save-Module -Name AzureRM -Path c:\temp\azurerm). There are a lot of modules obviously and you’re not going to need them all. At a minimum for this solution you’ll need;

AzureRM AzureRM.profile AzureRM.Compute

Upload them under the bin directory also as shown below.


How to create an Azure Function App to Simultaneously Start|Stop all Virtual Mac ...
Test that our script dependencies are accessible

Now that we have our dependent modules uploaded let’s test that we can load and utilise them. Below is commands to load the Invoke-Parallel script and test that it has loaded by getting the Help.

# Load the Invoke-Parallel Powershell Script . "D:\home\site\wwwroot\RG-Start-Stop-VirtualMachines\bin\Invoke-Parallel.ps1" # See if it is loaded by getting some output Get-Help Invoke-Parallel -Full

Put those lines into the code section, hit Save and Run and select Logs to see the output. If successful you’ll see the help. If you don’t you probably have a problem with the path to where you put the Invoke-Parallel script. You can use the Kudu Console from the Function App Settings to get a command line and verify your path.


How to create an Azure Function App to Simultaneously Start|Stop all Virtual Mac ...

Mineworked successfully.Now to test our AzureRM Module Loads. Update the Function to load the AzureRM Profile PSM as per below and test you have your path correct.

# Import the AzureRM Powershell Module import-module 'D:\home\site\wwwroot\RG-Start-Stop-VirtualMachines\bin\AzureRM.profile\2.4.0\AzureRM.Profile.psm1' Get-Help AzureRM
How to create an Azure Function App to Simultaneously Start|Stop all Virtual Mac ...

Success. Fantastic.

Create an Azure Service Principal

In order to automate the access and control of the Azure Virtual Machines we are going to need to connect to Azure using a Service Principal with the necessary permissions to manage the Virtual Machines.

The following script does just that. You only need to run this as part of the setup for the Azure Function so we have an account we can use for our automation tasks. Update line 6 for your naming and the password you want to use. I’m assigning the Service Principal the “DevTest Labs User” Azure Role (Line 17) as that allows the ability to manage the Virtual Machines. You can find a list of the available roles here .

Take note of the key outputs from this script. You will need to note the;

ApplicationID TenantID

I’m also securing the credential that has the permissions to Start|Stop the Virtual Machines using the example detailed here in Tao’s post .

For reference here is an example to generate the keyfile. Update your path in line 5 if required and make sure the password you supply in line 18 matches the password you supplied for the line

本文系统(windows)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

主题: PowerShellSQLSQL ServerHead
tags: Azure,Function,App,AzureRM,your,script
分页:12
转载请注明
本文标题:How to create an Azure Function App to Simultaneously Start|Stop all Virtual Mac ...
本站链接:http://www.codesec.net/view/522466.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(windows) | 评论(0) | 阅读(100)