您现在访问的是微软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 two ways to launch the Cloud Shell:

单击代码块右上角的“试用”。Click Try It in the upper right corner of a code block. 本文中的 Cloud Shell
单击 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 模块版本 3.6 或更高版本。If you choose to install and use the PowerShell locally, this tutorial requires the Azure PowerShell module version 3.6 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,则还需运行 Login-AzureRmAccount 以创建与 Azure 的连接。If you are running PowerShell locally, you also need to run Login-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

创建 VM 之前,需使用 New-AzureRmResourceGroup 创建资源组。Before you can create a VM, create a resource group with New-AzureRmResourceGroup. 以下示例在“EastUS”位置创建名为“myResourceGroupAutomate”的资源组:The following example creates a resource group named myResourceGroupAutomate in the EastUS location:

New-AzureRmResourceGroup -ResourceGroupName myResourceGroupAutomate -Location EastUS

使用 New-AzureRmNetworkInterface 设置 VM 的管理员用户名和密码:Set an administrator username and password for the VMs with Get-Credential:

$cred = Get-Credential

现在,可使用 New-AzureRmVM 创建 VM。Now you can create the VM with New-AzureRmVM. 以下示例创建所需的虚拟网络组件、OS 配置,并创建名为“myVM”的 VM:The following example creates the required virtual network components, the OS configuration, and then creates a VM named myVM:

# Create a subnet configuration
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig `
    -Name mySubnet `
    -AddressPrefix 192.168.1.0/24

# Create a virtual network
$vnet = New-AzureRmVirtualNetwork `
    -ResourceGroupName myResourceGroupAutomate `
    -Location EastUS `
    -Name myVnet `
    -AddressPrefix 192.168.0.0/16 `
    -Subnet $subnetConfig

# Create a public IP address and specify a DNS name
$publicIP = New-AzureRmPublicIpAddress `
    -ResourceGroupName myResourceGroupAutomate `
    -Location EastUS `
    -AllocationMethod Static `
    -IdleTimeoutInMinutes 4 `
    -Name "myPublicIP"

# Create an inbound network security group rule for port 3389
$nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig `
    -Name myNetworkSecurityGroupRuleRDP  `
    -Protocol Tcp `
    -Direction Inbound `
    -Priority 1000 `
    -SourceAddressPrefix * `
    -SourcePortRange * `
    -DestinationAddressPrefix * `
    -DestinationPortRange 3389 `
    -Access Allow

# Create an inbound network security group rule for port 80
$nsgRuleWeb = New-AzureRmNetworkSecurityRuleConfig `
    -Name myNetworkSecurityGroupRuleWWW  `
    -Protocol Tcp `
    -Direction Inbound `
    -Priority 1001 `
    -SourceAddressPrefix * `
    -SourcePortRange * `
    -DestinationAddressPrefix * `
    -DestinationPortRange 80 `
    -Access Allow

# Create a network security group
$nsg = New-AzureRmNetworkSecurityGroup `
    -ResourceGroupName myResourceGroupAutomate `
    -Location EastUS `
    -Name myNetworkSecurityGroup `
    -SecurityRules $nsgRuleRDP,$nsgRuleWeb

# Create a virtual network card and associate with public IP address and NSG
$nic = New-AzureRmNetworkInterface `
    -Name myNic `
    -ResourceGroupName myResourceGroupAutomate `
    -Location EastUS `
    -SubnetId $vnet.Subnets[0].Id `
    -PublicIpAddressId $publicIP.Id `
    -NetworkSecurityGroupId $nsg.Id

# Create a virtual machine configuration
$vmConfig = New-AzureRmVMConfig -VMName myVM -VMSize Standard_DS2 | `
Set-AzureRmVMOperatingSystem -Windows -ComputerName myVM -Credential $cred | `
Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer `
    -Offer WindowsServer -Skus 2016-Datacenter -Version latest | `
Add-AzureRmVMNetworkInterface -Id $nic.Id

# Create a virtual machine using the configuration
New-AzureRmVM -ResourceGroupName myResourceGroupAutomate -Location EastUS -VM $vmConfig

创建资源和 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 `
    -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)"}' `
    -Location EastUS

测试网站Test web site

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

Get-AzureRmPublicIPAddress `
    -ResourceGroupName myResourceGroupAutomate `
    -Name myPublicIP | 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.