教程 - 使用自定义脚本扩展将应用程序部署到 Azure 中的 Windows 虚拟机Tutorial - Deploy applications to a Windows virtual machine in Azure with the Custom Script Extension

若要以快速一致的方式配置虚拟机 (VM),可以使用适用于 Windows 的自定义脚本扩展To configure virtual machines (VMs) in a quick and consistent manner, you can use the 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 本地 ShellLaunch Azure local Shell

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块This article has been updated to use the new Azure PowerShell Az module. AzureRM 将继续获取关键的 bug 修复更新,但新功能将只出现在 Az 模块中。AzureRM will continue to get critical bugfix updates, but new features will be in the Az module only.

  • 若要在本地安装 Az 模块,请参阅安装 Azure PowerShellTo install the Az module locally, see Install Azure PowerShell.
  • 如果在本地安装 Az 模块,可通过运行 Enable-AzureRmAlias 来启用 AzureRM 兼容性。If you install the Az module locally, you can enable the AzureRM compatibility by running Enable-AzureRmAlias.

自定义脚本扩展概述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 Resource Manager 模板集成,也可以使用 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 的管理员用户名和密码:Set the administrator username and password for the VM with Get-Credential:

Connect-AzAccount -Environment AzureChinaCloud
$cred = Get-Credential

现在,可使用 New-AzVM 创建 VM。Now you can create the VM with New-AzVM. 以下示例在“ChinaEast”位置创建一个名为 myVM 的 VM。The following example creates a VM named myVM in the ChinaEast 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-AzVm `
    -ResourceGroupName "myResourceGroupAutomate" `
    -Name "myVM" `
    -Location "China East" `
    -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-AzVMExtension 安装自定义脚本扩展。Use Set-AzVMExtension 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-AzVMExtension -ResourceGroupName "myResourceGroupAutomate" `
    -ExtensionName "IIS" `
    -VMName "myVM" `
    -Location "ChinaEast" `
    -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-AzPublicIPAddress 获取负载均衡器的公共 IP 地址。Obtain the public IP address of your load balancer with Get-AzPublicIPAddress. 以下示例获取前面创建的 myPublicIPAddress 的 IP 地址:The following example obtains the IP address for myPublicIPAddress created earlier:

Get-AzPublicIPAddress `
    -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.