Easier Azure VM Deployment with the Custom Script Extension

Posted on June 10, 2015 by Aidan Finn in Microsoft Azure with 0 Comments

Microsoft provides us with several different virtual machine templates that lets us quickly deploy virtual machines in Azure. However, it’s rare that you need a standard virtual machine; maybe you need some roles or features added, install some software, or force some other kind of change. If you can script what you need, then the good news is that you can make it happen thanks to the Custom Script extension.

An Overview of the Custom Script Extension

One of the options when you deploy a new Azure virtual machine is to install extensions. One of these extensions is called the Custom Script extension. As Microsoft explains, this extension can load a script into a virtual machine and execute it for you using system privileges after the virtual machine has been deployed.

This means that if you can script a change by using PowerShell, then you can automate that change automatically after the virtual machine has been deployed by using the Custom Script extension.

Preparing Deployment

The first step is to write a script that will make the required change. Author your script and save it somewhere safe. If this is a one-off script, then you can keep it on your PC. If you are going to reuse the script, then upload it to a storage account in Azure. In my example, I have uploaded a script to a container called scripts in a storage account named petridemo.

Verifying that the file was uploaded to the Azure Storage Account (Image credit: Aidan Finn)

Verifying that the file was uploaded to the Azure Storage Account (Image credit: Aidan Finn)

Deploying the Virtual Machine

If you are using the management portal, then go through the normal wizard to create a virtual machine.

Creating a new Azure virtual machine (Image credit: Aidan Finn)

Creating a new Azure virtual machine (Image credit: Aidan Finn)

You might be used to just skipping through the Virtual Machine Configuration screen. Let’s hold on a second because this is where we can enable extensions. Check the box to enable Custom Script.

Enabling the Custom Script extension in the virtual machine configuration wizard. (Image credit: Aidan Finn)

Enabling the Custom Script extension in the virtual machine configuration wizard. (Image Credit: Aidan Finn)

You now have two options:

  • From Local: Use a script that is stored on your PC. This is great if you’re the only administrator or you have a one-off script.
  • From Storage: You are sharing administrator duties or you are using scripts over and over. Storing scripts in a storage account allows safe-shared storage with easy access.
Sponsored

I have already uploaded a script to Azure, so I clicked From Storage. A new dialog appears; expand the required storage account, browse into the necessary container, select the script that you want to use, and click Open.

Select a script from an Azure Storage Account (Image Credit: Aidan Finn)

Select a script from an Azure Storage Account (Image Credit: Aidan Finn)

Now the Custom Script extension is configured to run a script from the Azure storage account. This ensures that my desired changes are implemented in the virtual machine when the deployment is completed.

A configured Custom Script extension [Image credit: Aidan Finn]

A configured Custom Script extension (Image Credit: Aidan Finn)

Sponsored

Getting More from the Custom Script Extension

If PowerShell and automation are things that appeal to you, then the Custom Script extension offers a lot of promise, as you might realize if you read the excellent article, Understanding Azure Custom Script Extension, by Ravikanth Chaganti (MVP) on PowerShell Magazine. Some interesting options include:

  • Drive the entire VM creation and customization process using PowerShell
  • Passing parameters to the script to change its actions
  • Retrieving return values using Azure PowerShell cmdlets
  • Executing scripts in the guest OS of a virtual machine using Azure Automation

Give it a spin, and let me know your thoughts in the article comments below.

Sponsored

Tagged with ,