Last Update: Sep 04, 2024 | Published: Jun 10, 2015
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.
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.
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:
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.
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:
Give it a spin, and let me know your thoughts in the article comments below.