Centric connect.engage.succeed

PowerShell your DevOps - part 3

Geschreven door Randolph Widjaja - 25 oktober 2019

Randolph Widjaja
Deploy ARM templates to Azure and install with Azure Custom Script Extension a in-house created PowerShell script to deploy roles, features and 3rd party applications to the Azure VM.

In my other blogposts I wrote about using scripts to install Microsoft roles, features and 3rd party software during deployment of a Windows Server VM. In this blog, I’ll explain how to integrate the PowerShell commands with ARM templates deployments.

What you need is:

  • Visual Studio Code
  • PowerShell AZ CLI module
  • ARM template, I used the 201 Custom Script VM template on GitHub in this case
  • ARM Custom Script Extension For Windows

In order to connect to the Azure Command Line module, you need to install the new Microsoft Azure CLI module. Follow the instructions in the link if you haven’t done this yet: https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-2.7.0

I’m creating PowerShell scripts in Microsoft Visual Studio Code, because it is light and fast for creating scripts. There are lot of free extensions available to use with Microsoft Visual Studio Code. For example, you can install the ARM template viewer for visualisation of the ARM template.

So now we can start coding. We are going to use a predefined ARM template with Microsoft Azure Custom Script Extension feature. There are many more Azure quick-start templates available for other purposes. They can help you and also speed up the coding process. In this case, I used the ARM template that originally came from “Azure-quickstart-templates / 201-vm-custom-script-windows”: https://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-custom-script-windows

To use a PowerShell script that you have created in-house with an ARM template, you need to use the Azure Custom Script Extension. The Azure Custom Script Extension will copy the script from a repository like GitHub or Azure Storage and execute it locally during the deployment of the ARM template. For more details, please go to: https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows

This ARM template copies a Copy-FileFromAzure.ps1 script and executes it locally on the deployed ARM template. But what if you want to install Microsoft server roles, features or third-party software on the template? We can add some code to the Copy-FileFromAzure.ps1 and use the same methods I described in the first part of my PowerShell your DevOps series: https://www.centric.eu/NL/Default/Craft/Blogs/2018/10/08/Powershell-your-DevOps-environment---Part-1

We’re going to automate the installation for Microsoft serverfeature DotNet framework and install a 3rd party application Notepad++. The first part is copying files like the Copy-FileFromAzure.ps1 and software installation resources to the VM for execution on the VM with the Invoke-WebRequest command.

Also, we are going to create a task in the task scheduler to restart the Copy-FileFromAzure.ps1 and add the -SkiptoPhaseTwo parameter. Don’t forget to delete the task after the reboot, otherwise the script will rerun after the next reboot. But first you need to know where the Azure Custom Script Extension is copying the Copy-FileFromAzure.ps1. In this case, it is copying to "C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.9.5\Downloads\0\”. You can use this location in the TaskScheduler task.

Now the script is finished, we need to deploy the ARM template and place the files in the cloud. The scripts and ARM template will be placed on GitHub, but the installation files are placed on Azure storage.

How do we connect and start the deployment? First we connect to the Azure CLI with Connect-AzAccount.

# PowerShell Azure 

# Connect to Microsoft Azure
#vervangen door  Connect-AzAccount 
Connect-AzAccount

Second we going to create a separate Azure Resourcegroup for the deployment of the ARM template.

# Variables
$ResourceGroup              = "***RG01"
$Location                   = "westeurope"

# Azure ResourceGroup
New-AzResourceGroup -Name $ResourceGroup -Location $Location

Finally we can start our deployment with the following PowerShell command:

#Deployment ARM Template to Azure
New-AzResourceGroupDeployment -ResourceGroupName  $ResourceGroup -TemplateUri https://raw.githubusercontent.com/***/master/azuredeploy.json


If your Azure VM already exists, all you need to do is execute a PowerShell script on the Azure VM using the Set-AzVMCustomScriptExtension command. The only requirement is that the script must be located on an Azure Storage Account instead of GitHub.

#Execute CustomScriptExtension 
Set-AzVMCustomScriptExtension -ResourceGroupName $ResourceGroup  -Location $Location -VMName "***" -ContainerName  "***" -StorageAccountName "***" -FileName " Copy-FileFromAzure.ps1" -Name "CustomScriptExtension" 

For this blog, I automated the installation and download of the DotNet 3.5 and Notepad++ resources using a task that executes the same Copy-FileFromAzure.ps1 PowerShell script after the reboot, with logging enabled.

If you're interested in the coding, I have added the coding of the Copy-FileFromAzure.ps1 PowerShell script on my GitHub page. Download link:

https://raw.githubusercontent.com/randolphwidjaja/PowerShell_Scripts/master/Copy-FileFromAzure.ps1

Resources

  1. https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows
  2. https://adamtheautomator.com/azure-custom-script-extension-windows/
  3. https://github.com/Azure/azure-quickstart-templates
  4. https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-2.7.0
  5. https://docs.microsoft.com/en-us/powershell/module/az.compute/set-azvmcustomscriptextension?view=azps-2.7.0
  6. https://code.visualstudio.com/

About Randolph

Craft Expert Randolph Widjaja is part of the Cloud team within Craft, the development programme for IT professionals (powered by Centric). If you would like to follow his blog, sign up for Craft updates

Want to know more about Craft, the development programme for IT professionals? Check out the website!

Tags:Cloud

     
Schrijf een reactie
  • Captcha image
  • Verzenden