Azure Automation using Custom Script Extension – Part 2

Azure Automation using Custom Script Extension – Part 2

Azure Automation using Custom Script Extension – Part 2

In the first part of the series – article Create Custom Image in Microsoft Azure, we saw how to create Custom Image in Microsoft Azure. In this article, I will walk you through using Custom Script Extension feature of Microsoft Azure, that lets you execute PowerShell scripts once the Operating System is deployed.

In this example, you will deployment of Operating System on Azure, install Windows Role/Feature and silently deploy an application Firefox.

Below is what is required for this:

  1. Azure Account
  2. Azure Blob Storage – for storing Custom Script Extension
  3. Azure File Services – for storing installers (Firefox)
  4. Custom Image – that was created in first part of this article


Preparing Azure Blob Storage & Container

Create Azure Blob Storage. Login to Azure Account & give a unique name. In the example below you see little “red” color escalation mark. This was because I didn’t take screen shot earlier. There needs to be “green” color tick mark before you continue with successfully creating Storage account



Now create Container named “scripts” and with access type as  “Public Container” . This will be used to store the PowerShell script used by Custom Script Extension.






Preparing Azure File Services for storing Software Installers

Login to New Azure Portal –, go to Storage Account that was created above, click on Files and create a File Share named installers. This will be used to store the installers like Firefox in this example.




Quick recap, we have now created a Azure Storage Account named goel, created a page blob named scripts and have a File Share named installers, in North Europe region.

Page Blob – Scripts –

File Share – Installers\\\installers

Connect to File Share and upload Installers

In order to connect to Azure File Share, you need a Virtual Machine on Azure to access via SMB method (I guess this is because Azure might have locked the source IPs to Azure regions, I have not explored why I can’t to connect to Azure File Share from my home computer. Leaving this for investigation for later). We need a Azure Storage Primary Key to connect to Azure share.

Connect to Azure Subscription by executing




Once you add your Azure Account to PowerShell, execute the command shown below to get your Primary and Secondary Keys. Copy the Primary key.

Get-AzureStorageKey -StorageAccountName <your-account-name>



Now login to one of the Azure VM, connect to Azure File share and copy the Firefox installer

net use u: <azure-page-blob>\scripts /u:<azure-storage-name> <azure-storage-primary-key>




Almost done,  Create PowerShell file to execute by Custom Script Extension

Save the below code, as start.ps1 file. Replace the <azure-storage-key> with your key, and <azure-storage-name> with your Azure Storage Account Name. In my case, the storage name was goel

Add-WindowsFeature Failover-Clustering -IncludeAllSubFeature -IncludeManagementTools

net use u: \\ /u:<azure-storage-name> <azure-storage-key>
u:\Firefox.exe -ms

Upload the start.ps1 to Azure Blob Container

To upload the PowerShell script to Azure Container, you need Azure Storage Explorer. Download it from below location and install it.

Download Azure Storage Explorer

Once downloaded and installed, launch Azure Storage Explorer, add your Azure Account by providing your Storage account name and Key


Once successfully logged-in, upload the start.ps1 to your scripts container, as shown below.




We are now ready to deploy the VM. Let us start

First we need to set the Default Storage Account Name on the Azure subscription

Set-AzureSubscription -SubscriptionId (Get-AzureSubscription).SubscriptionId -CurrentStorageAccountName "goel"

Next,  we set the Azure VM Image Name that we created in Part-1 of this article series (Custom VM Image), and set the Name of the Virtual Machine


Next, we start creating VM Configuration for New VM with Instance Size as Small, this can be changed to any valid Azure VM size.

$vm = New-AzureVMConfig -Name $Name -InstanceSize Small -ImageName $imagename

Next, we set the Azure VM login ID and Password

Add-AzureProvisioningConfig -VM $vm -Windows -AdminUsername "sarvesh" -Password "Pas123456"


Next, we add the Azure VM Custom Script Extension to the VM Configuration, and link the start.ps1 file from the scripts container

$vm=Set-AzureVMCustomScriptExtension -VM $vm -ContainerName "scripts" -FileName 'start.ps1'

Next, we create a new VM and deploy it in new or existing Cloud Service Customtest1. 

Please note that Location of the Virtual Machine should be same of your Custom Image that we created in Part-1. If you use default Microsoft VM Image then could use any supported Azure region for the OS Image.
New-AzureVM -ServiceName "Customtest1" -Location "North Europe" -VMs $vm


Azure would take approximately 5-6 minutes to create the VM, and once created you will see VM created that would install Failover-Cluster feature and Firefox installed as we mentioned in start.ps1


We have now successfully deployed the Firefox application using Custom Script Extension

Let us see the logs now

Script got downloaded to local machine



Below log shows that start.ps1 was downloaded and executed successfully.


About The Author

Related posts


  1. Sarvesh Goel

    Start.ps1 content is below – Save the text in a .ps1 file and upload it to blob storage

    Add-WindowsFeature Failover-Clustering -IncludeAllSubFeature -IncludeManagementTools
    net use u: \ /u:
    u:Firefox.exe -ms

  2. Ramit Srivastava

    Great article! Is there a way to trigger the custom scripts extension on demand from outside the VM (say from Automation rubook)?

Comments are closed.