Custom Script Extension For Windows

The Custom Script Extension is used for post-deployment configuration, software installation, or any other configuration/management task by executing the scripts on Azure virtual machines. These scripts can be downloaded from Azure Storage or GitHub, or provided to the Azure portal at extension runtime. The Custom Script extension integrates with Azure Resource Manager templates, and can also be run using the Azure CLI, PowerShell, Azure portal, or the Azure Virtual Machine REST API.

Here, I am going to create a new text file on my C drive of a virtual machine by uploading an extension to the portal during runtime. For this, I have already deployed a Windows VM in my previous article.

Go to the Extensions section on your dashboard of VM in the Azure portal.

As we haven’t added any extensions, the list is empty and there is an option to add. Click on Add. There, you will get a list of extensions supported and here, we are going to select Custom Script Extension and click on Create.

On creating, it will ask for the script file and there is an option to pass any arguments to the scripts in the file. You can just use double quotes around the parameters with space.

For this demo, I am writing a simple script to create a text file with a content on c drive.

New-Item -ItemType file -Path C:\ -Name test.txt -Value "This file is created by Custom Script Extension"

Save this script as CustomScriptDemo.ps1 and upload to the extensions and click OK. After a few seconds, on completion of the deployment, you can see the extension listed in the portal with status as provisioning succeeded.

Now, if you RDP to the server and check the C drive, you can see the file there which was not during initial deployment.

It is not required for you to upload the extensions. It is also possible for you to install these extensions programmatically via PowerShell or Azure Resource Templates. That is, you can add this script to the end of the PowerShell script to deploy a VM. So, once the server gets built, it will install the extension and then executes whatever script you want. So, you can literally use this script extension like a Bootstrapping type of scenario, or like a startup script when you provision your virtual machines.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now