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

如何在 Azure 中自定义 Windows 虚拟机How to customize a Windows virtual machine in Azure

若要以快速一致的方式配置虚拟机 (VM),通常需要某种形式的自动化。To configure virtual machines (VMs) in a quick and consistent manner, some form of automation is typically desired. 自定义 Windows VM 的一种常用方法是使用适用于 Windows 的自定义脚本扩展A common approach to customize a Windows VM is to use Custom Script Extension for Windows. 本教程介绍如何执行下列操作:In this tutorial you learn how to:

  • 使用自定义脚本扩展安装 IISUse the Custom Script Extension to install IIS
  • 创建使用自定义脚本扩展的 VMCreate a VM that uses the Custom Script Extension
  • 在应用扩展后查看正在运行的 IIS 站点View a running IIS site after the extension is applied

启动 Azure Cloud ShellLaunch Azure Cloud Shell

Azure Cloud Shell 是免费的交互式 shell,可以使用它运行本文中的步骤。The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. 它预安装有常用 Azure 工具并将其配置与帐户一起使用。It has common Azure tools preinstalled and configured to use with your account. 请直接单击“复制”对代码进行复制,将其粘贴到 Cloud Shell 中,然后按 Enter 来运行它。Just click the Copy to copy the code, paste it into the Cloud Shell, and then press enter to run it. 可通过多种方式来启动 Cloud Shell:There are a few ways to launch the Cloud Shell:

单击代码块右上角的“试用”。Click Try It in the upper right corner of a code block. 本文中的 Cloud Shell
在浏览器中打开 Cloud Shell。Open Cloud Shell in your browser. https://shell.azure.com/powershellhttps://shell.azure.com/powershell
单击 Azure 门户右上角菜单上的“Cloud Shell”按钮。Click the Cloud Shell button on the menu in the upper right of the Azure portal. 门户中的 Cloud ShellCloud Shell in the portal

如果选择在本地安装并使用 PowerShell,则本教程需要 Azure PowerShell 模块 5.3 或更高版本。If you choose to install and use the PowerShell locally, this tutorial requires the Azure PowerShell module version 5.3 or later. 运行 Get-Module -ListAvailable AzureRM 即可查找版本。Run Get-Module -ListAvailable AzureRM to find the version. 如果需要升级,请参阅安装 Azure PowerShell 模块If you need to upgrade, see Install Azure PowerShell module. 如果在本地运行 PowerShell,则还需运行 Connect-AzureRmAccount 以创建与 Azure 的连接。If you are running PowerShell locally, you also need to run Connect-AzureRmAccount to create a connection with Azure.

自定义脚本扩展概述Custom script extension overview

自定义脚本扩展在 Azure VM 上下载和执行脚本。The Custom Script Extension downloads and executes scripts on Azure VMs. 此扩展适用于部署后配置、软件安装或其他任何配置/管理任务。This extension is useful for post deployment configuration, software installation, or any other configuration / management task. 可以从 Azure 存储或 GitHub 下载脚本,或者在扩展运行时会脚本提供给 Azure 门户。Scripts can be downloaded from Azure storage or GitHub, or provided to the Azure portal at extension run time.

自定义脚本扩展与 Azure 资源管理器模板集成,也可以使用 Azure CLI、PowerShell、Azure 门户或 Azure 虚拟机 REST API 来运行它。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.

自定义脚本扩展适用于 Windows 和 Linux VM。You can use the Custom Script Extension with both Windows and Linux VMs.

创建虚拟机Create virtual machine

首先,使用 Get-Credential 设置 VM 的管理员用户名和密码:First, set an administrator username and password for the VM with Get-Credential:

$cred = Get-Credential

现在,可使用 New-AzureRmVM 创建 VM。Now you can create the VM with New-AzureRmVM. 以下示例在“EastUS”位置创建一个名为 myVM 的 VM。The following example creates a VM named myVM in the EastUS location. 如果资源组 myResourceGroupAutomate 和支持的网络资源不存在,则会创建它们。If they do not already exist, the resource group myResourceGroupAutomate and supporting network resources are created. 此 cmdlet 还打开端口 80,目的是允许 Web 流量。To allow web traffic, the cmdlet also opens port 80.

New-AzureRmVm `
    -ResourceGroupName "myResourceGroupAutomate" `
    -Name "myVM" `
    -Location "East US" `
    -VirtualNetworkName "myVnet" `
    -SubnetName "mySubnet" `
    -SecurityGroupName "myNetworkSecurityGroup" `
    -PublicIpAddressName "myPublicIpAddress" `
    -OpenPorts 80 `
    -Credential $cred

创建资源和 VM 需要几分钟的时间。It takes a few minutes for the resources and VM to be created.

自动安装 IISAutomate IIS install

使用 Set-AzureRmVMExtension 安装自定义脚本扩展。Use Set-AzureRmVMExtension to install the Custom Script Extension. 该扩展运行 powershell Add-WindowsFeature Web-Server 以安装 IIS Web 服务器,并更新“Default.htm”页以显示 VM 的主机名:The extension runs powershell Add-WindowsFeature Web-Server to install the IIS webserver and then updates the Default.htm page to show the hostname of the VM:

Set-AzureRmVMExtension -ResourceGroupName "myResourceGroupAutomate" `
    -ExtensionName "IIS" `
    -VMName "myVM" `
    -Location "EastUS" `
    -Publisher Microsoft.Compute `
    -ExtensionType CustomScriptExtension `
    -TypeHandlerVersion 1.8 `
    -SettingString '{"commandToExecute":"powershell Add-WindowsFeature Web-Server; powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $($env:computername)"}'

测试网站Test web site

使用 Get-AzureRmPublicIPAddress 获取负载均衡器的公共 IP 地址。Obtain the public IP address of your load balancer with Get-AzureRmPublicIPAddress. 以下示例获取前面创建的“myPublicIPAddress”的 IP 地址:The following example obtains the IP address for myPublicIPAddress created earlier:

Get-AzureRmPublicIPAddress `
    -ResourceGroupName "myResourceGroupAutomate" `
    -Name "myPublicIPAddress" | select IpAddress

然后,可将公共 IP 地址输入 web 浏览器中。You can then enter the public IP address in to a web browser. 随即显示网站,包括负载均衡器将流量分发到的 VM 的主机名,如下例所示:The website is displayed, including the hostname of the VM that the load balancer distributed traffic to as in the following example:

运行 IIS 网站

后续步骤Next steps

在本教程中,会在 VM 上自动执行 IIS 安装。In this tutorial, you automated the IIS install on a VM. 你已了解如何:You learned how to:

  • 使用自定义脚本扩展安装 IISUse the Custom Script Extension to install IIS
  • 创建使用自定义脚本扩展的 VMCreate a VM that uses the Custom Script Extension
  • 在应用扩展后查看正在运行的 IIS 站点View a running IIS site after the extension is applied

转到下一教程,了解如何创建自定义 VM 映像。Advance to the next tutorial to learn how to create custom VM images.