Easier Azure VM Deployment with the Custom Script Extension
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.
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.
Deploying the Virtual Machine
If you are using the management portal, then go through the normal wizard to create a virtual machine.
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.
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.
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.
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.
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.
More in Microsoft Azure
Microsoft's New Azure Fluid Relay Service Lets Developers Build Collaborative Apps
Aug 5, 2022 | Rabia Noureen
Securing IoT with Azure Sphere
Jul 25, 2022 | John Lunn
Oracle Teams Up with Microsoft to Launch New Database Service for Azure
Jul 21, 2022 | Rabia Noureen
Microsoft Releases Update Management Center in Public Preview
Jul 20, 2022 | Rabia Noureen
How to Select Hardware for Azure Stack HCI
Jul 5, 2022 | Flo Fox
How to Install Azure Stack HCI Single-Node Clusters
Jun 3, 2022 | Flo Fox
Most popular on petri