您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:使用 Azure 资源管理器模板部署虚拟机扩展Tutorial: Deploy virtual machine extensions with Azure Resource Manager templates

了解如何使用 Azure 虚拟机扩展在 Azure VM 上执行部署后配置和自动化任务。Learn how to use Azure virtual machine extensions to perform post-deployment configuration and automation tasks on Azure VMs. 有许多不同的 VM 扩展可与 Azure VM 配合使用。Many different VM extensions are available for use with Azure VMs. 在本教程中,你将从 Azure 资源管理器模板部署自定义脚本扩展,以便在 Windows VM 上运行 PowerShell 脚本。In this tutorial, you deploy a Custom Script extension from an Azure Resource Manager template to run a PowerShell script on a Windows VM. 此脚本在 VM 上安装 Web 服务器。The script installs Web Server on the VM.

本教程涵盖以下任务:This tutorial covers the following tasks:

  • 准备 PowerShell 脚本Prepare a PowerShell script
  • 打开快速入门模板Open a quickstart template
  • 编辑模板Edit the template
  • 部署模板Deploy the template
  • 验证部署Verify the deployment

如果还没有 Azure 订阅,可以在开始前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

先决条件Prerequisites

若要完成本文,需要做好以下准备:To complete this article, you need:

准备 PowerShell 脚本Prepare a PowerShell script

包含以下内容的 PowerShell 脚本在可以公开访问的 Azure 存储帐户上共享:A PowerShell script with the following content is shared from an Azure storage account with public access:

Install-WindowsFeature -name Web-Server -IncludeManagementTools

如果选择将文件发布到你自己的位置,则必须在教程的后面部分更新模板中的 fileUri 元素。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 快速入门模板是资源管理器模板的存储库。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. 本教程中使用的模板称为部署简单的 Windows VMThe template used in this tutorial is called Deploy a simple Windows VM.

  1. 在 Visual Studio Code 中,选择“文件” > “打开文件”。 In Visual Studio Code, select File > Open File.

  2. 在“文件名”框中粘贴以下 URL: https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-vm-simple-windows/azuredeploy.json In the File name box, paste the following URL: https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-vm-simple-windows/azuredeploy.json

  3. 若要打开该文件,请选择“打开”。 To open the file, select Open.
    该模板定义五个资源:The template defines five resources:

    • Microsoft.Storage/storageAccountsMicrosoft.Storage/storageAccounts. 请参阅模板参考See the template reference.

    • Microsoft.Network/publicIPAddressesMicrosoft.Network/publicIPAddresses. 请参阅模板参考See the template reference.

    • Microsoft.Network/virtualNetworksMicrosoft.Network/virtualNetworks. 请参阅模板参考See the template reference.

    • Microsoft.Network/networkInterfacesMicrosoft.Network/networkInterfaces. 请参阅模板参考See the template reference.

    • Microsoft.Compute/virtualMachinesMicrosoft.Compute/virtualMachines. 请参阅模板参考See the template reference.

      在自定义模板之前,不妨对其进行一些基本的了解。It's helpful to get some basic understanding of the template before you customize it.

  4. 选择“文件” > “另存为”,使用文件名 azuredeploy.json 将该文件的副本保存到本地计算机。 Save a copy of the file to your local computer with the name azuredeploy.json by selecting File > Save As.

编辑模板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"
        }
    }
}

有关此资源定义的详细信息,请查看扩展参考For more information about this resource definition, see the extension reference. 下面是一些重要元素: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 Set name and type for child resources.
  • dependsOn:在创建虚拟机以后创建扩展资源。dependsOn: Create the extension resource after you've created the virtual machine.
  • fileUris:存储脚本文件的位置。fileUris: The locations where the script files are stored. 如果不使用提供的位置,则需更新这些值。If you choose not to use the provided location, you need to update the values.
  • commandToExecute:此命令调用脚本。commandToExecute: This command invokes the script.

部署模板Deploy the template

有关部署过程,请参阅“部署模板”部分,文档为教程:使用依赖的资源创建 Azure 资源管理器模板中使用的。For the deployment procedure, see the "Deploy the template" section of Tutorial: Create Azure Resource Manager templates with dependent resources. 建议使用为虚拟机管理员帐户生成的密码。We recommended that you use a generated password for the virtual machine administrator account. 请参阅本文的先决条件部分。See this article's Prerequisites section.

验证部署Verify the deployment

  1. 在 Azure 门户中选择 VM。In the Azure portal, select the VM.
  2. 在 VM 概述中,选择“单击进行复制”复制 IP 地址,并将其粘贴到浏览器标签页中。 In the VM overview, copy the IP address by selecting Click to copy, and then paste it in a browser tab.
    此时会打开默认的 Internet Information Services (IIS) 欢迎页:The default Internet Information Services (IIS) welcome page opens:

Internet Information Services 欢迎页

清理资源Clean up resources

不再需要部署的 Azure 资源时,请通过删除资源组将其清除。When you no longer need the Azure resources you deployed, clean them up by deleting the resource group.

  1. 在 Azure 门户的左窗格中选择“资源组” 。In the Azure portal, in the left pane, select Resource group.
  2. 在“按名称筛选”框中输入资源组名称。 In the Filter by name box, enter the resource group name.
  3. 选择资源组名称。Select the resource group name.
    将显示资源组中的六个资源。Six resources are displayed in the resource group.
  4. 在顶部菜单中选择“删除资源组”。 In the top menu, select Delete resource group.

后续步骤Next steps

在本教程中,你已部署虚拟机和虚拟机扩展。In this tutorial, you deployed a virtual machine and a virtual machine extension. 此扩展在虚拟机上安装了 IIS Web 服务器。The extension installed the IIS web server on the virtual machine. 若要了解如何使用 Azure SQL 数据库扩展导入 BACPAC 文件,请参阅:To learn how to use the Azure SQL Database extension to import a BACPAC file, see: