Azure IaaS: How to Use Custom Script on Virtual Machine

PowerShell is an amazing tool for every IT Admins and we now this rule. A script is not only a great help to reduce execution time of same tasks, like AD Users’ creation, but also for automation.


Maybe this could be strange but also into the Cloud it’s important automate some activities: for example if we want enable role or features into a Virtual Machine; these activities requires few minutes that can grow when we have many VMs.


The solution to this problem is PowerShell and this article will show you how to inject a script into a VM, to automate task, on Microsoft Azure.


First of all start the wizard of a new VM but hold to create it (go ahead until the Virtual machine configuration form). Select the checkbox Custom Script, as showed in figure 1. Script must be as .ps1 and can be located on your computer or into a Azure Storage and this is a good idea to create a central repository.


Figure 1 – Custom Script Enabled


For this example I will use a script with cmdlet to enable IIS role: Install-WindowsFeature Web-Server -IncludeManagementTools -IncludeAllSubFeature


After script’s selecting, start VM’s creation and wait; it’s also possible check tasks status as showed in figure 2.


Figure 2 – Tasks Status


When the wizard will finish, the VM will have IIS installed, as showed in figure 3.


Figure 3 – IIS Installed


Q – No script are executed
A- Check if the VM Agent checkbox is enable

Q – VM Agent is selected but roles/features are not presents
A – Some roles/features require reboot and if you haven’t write the right cmdlet, you must do manually from console

Q – VM Agent is selected, script is right but roles/features are not presents
A – Before deploy a script on Azure, test it on a local machine!

Q – I receive a red-error into tasks status
A – Sometimes the VM Agent crashes during the wizard so it’s necessary restart the VM from Azure console. This should reset all the issues and restart the entire activities.