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.