Tutorial: Deploy virtual machine extensions with Azure Resource Manager templates

Learn how to use Azure virtual machine extensions to perform post-deployment configuration and automation tasks on Azure VMs. Many different VM extensions are available for use with Azure VMs. In this tutorial, you deploy a Custom Script extension from a Resource Manager template to run a PowerShell script on a Windows VM. The script installs Web Server on the VM.

This tutorial covers the following tasks:

  • Prepare a PowerShell script
  • Open a QuickStart template
  • Edit the template
  • Deploy the template
  • Verify the deployment

If you don't have an Azure subscription, create a free account before you begin.

Prerequisites

To complete this article, you need:

Prepare a PowerShell script

A PowerShell script with the following content is shared from an Azure Storage account with the public access:

Install-WindowsFeature -name Web-Server -IncludeManagementTools

If you choose to publish the file to your own location, you must update the [fileUri] element in the template later in the tutorial.

Open a Quickstart template

Azure QuickStart Templates is a repository for Resource Manager templates. Instead of creating a template from scratch, you can find a sample template and customize it. The template used in this tutorial is called Deploy a simple Windows VM.

  1. From Visual Studio Code, select File>Open File.
  2. In File name, paste the following URL:

    https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-vm-simple-windows/azuredeploy.json
    
  3. Select Open to open the file.
  4. There are five resources defined by the template:

  5. Select File>Save As to save a copy of the file to your local computer with the name azuredeploy.json.

Edit the template

Add a virtual machine extension resource to the existing template with the following content:

{
    "apiVersion": "2018-06-01",
    "type": "Microsoft.Compute/virtualMachines/extensions",
    "name": "[concat(variables('vmName'),'/', 'InstallWebServer')]",
    "location": "[parameters('location')]",
    "dependsOn": [
        "[concat('Microsoft.Compute/virtualMachines/',variables('vmName'))]"
    ],
    "properties": {
        "publisher": "Microsoft.Compute",
        "type": "CustomScriptExtension",
        "typeHandlerVersion": "1.7",
        "autoUpgradeMinorVersion":true,
        "settings": {
            "fileUris": [
                "https://armtutorials.blob.core.windows.net/usescriptextensions/installWebServer.ps1"
            ],
            "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File installWebServer.ps1"
        }
    }
}

See the extension reference if you need more information about this resource definition. The following are some important elements:

  • name: Because the extension resource is a child resource of the virtual machine object, the name must have the virtual machine name prefix. See Child resources.
  • dependsOn: The extension resource must be created after the virtual machine has been created.
  • fileUris: These are the locations where the script files are stored. If you choose not to use the one provided, you need to update the values.
  • commandToExecute: This is the command to invoke the script.

Deploy the template

Refer to the Deploy the template section for the deployment procedure. It is recommended to use a generated password for the virtual machine administrator account. See Prerequisites.

Verify the deployment

In the portal, select the VM and in the overview of the VM, use Click to copy to the right of the IP address to copy it and paste it into a browser tab. The default IIS welcome page opens, and should look like this:

Azure Resource Manager deploys vm extensions customer script IIS web server

Clean up resources

When the Azure resources are no longer needed, clean up the resources you deployed by deleting the resource group.

  1. From the Azure portal, select Resource group from the left menu.
  2. Enter the resource group name in the Filter by name field.
  3. Select the resource group name. You shall see a total of six resources in the resource group.
  4. Select Delete resource group from the top menu.

Next steps

In this tutorial, you deployed a virtual machine and a virtual machine extension. The extension installed the IIS web server on the virtual machine. To learn how to use the SQL Database extension to import a BACPAC file, see: