使用 PowerShell 将应用程序安装到虚拟机规模集中Install applications into a virtual machine scale set with PowerShell

此脚本创建运行 Windows Server 2016 的虚拟机规模集,并使用自定义脚本扩展安装基本 Web 应用程序。This script creates a virtual machine scale set running Windows Server 2016 and uses the Custom Script Extension to install a basic web application. 运行脚本后,可以通过 Web 浏览器访问该 Web 应用。After running the script, you can access the web app through a web browser.

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

备注

本文已经过更新,以便使用 Azure Az PowerShell 模块。This article has been updated to use the Azure Az PowerShell module. 若要与 Azure 交互,建议使用的 PowerShell 模块是 Az PowerShell 模块。The Az PowerShell module is the recommended PowerShell module for interacting with Azure. 若要开始使用 Az PowerShell 模块,请参阅安装 Azure PowerShellTo get started with the Az PowerShell module, see Install Azure PowerShell. 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 AzTo learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.

示例脚本Sample script

# Provide your own secure password for use with the VM instances
$cred = Get-Credential

# Create a virtual machine scale set and supporting resources
# A resource group, virtual network, load balancer, and NAT rules are automatically
# created if they do not already exist
New-AzVmss `
  -ResourceGroupName "myResourceGroup" `
  -VMScaleSetName "myScaleSet" `
  -Location "ChinaNorth" `
  -VirtualNetworkName "myVnet" `
  -SubnetName "mySubnet" `
  -PublicIpAddressName "myPublicIPAddress" `
  -LoadBalancerName "myLoadBalancer" `
  -UpgradePolicyMode "Automatic" `
  -Credential $cred

# Create a configuration object to store the Custom Script Extension definition
$customConfig = @{
"fileUris" = (,"https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/automate-iis.ps1");
"commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File automate-iis.ps1"
}

# Get information about the scale set
$vmss = Get-AzVmss `
          -ResourceGroupName "myResourceGroup" `
          -VMScaleSetName "myScaleSet"

# Add the Custom Script Extension to install IIS and configure basic website
$vmss = Add-AzVmssExtension `
  -VirtualMachineScaleSet $vmss `
  -Name "customScript" `
  -Publisher "Microsoft.Compute" `
  -Type "CustomScriptExtension" `
  -TypeHandlerVersion 1.8 `
  -Setting $customConfig

# Update the scale set and apply the Custom Script Extension to the VM instances
Update-AzVmss `
  -ResourceGroupName "myResourceGroup" `
  -Name "myScaleSet" `
  -VirtualMachineScaleSet $vmss

# Get the public IP address of your load balancer. To see your scale set in action, open this address in a web browser
Get-AzPublicIpAddress -ResourceGroupName "myResourceGroup" | Select IpAddress

清理部署Clean up deployment

运行以下命令可删除资源组、规模集和所有相关资源。Run the following command to remove the resource group, scale set, and all related resources.

Remove-AzResourceGroup -Name myResourceGroup

脚本说明Script explanation

此脚本使用以下命令创建部署。This script uses the following commands to create the deployment. 表中的每一项均链接到特定于命令的文档。Each item in the table links to command specific documentation.

命令Command 注释Notes
New-AzVmssNew-AzVmss 创建虚拟机规模集和所有支持资源,包括虚拟网络、负载均衡器和 NAT 规则。Creates the virtual machine scale set and all supporting resources, including virtual network, load balancer, and NAT rules.
Get-AzVmssGet-AzVmss 获取有关虚拟机规模集的信息。Gets information on a virtual machine scale set.
Add-AzVmssExtensionAdd-AzVmssExtension 添加自定义脚本的 VM 扩展以安装基本的 Web 应用程序。Adds a VM extension for Custom Script to install a basic web application.
Update-AzVmssUpdate-AzVmss 更新虚拟机规模集模型以应用 VM 扩展。Updates the virtual machine scale set model to apply the VM extension.
Get-AzPublicIpAddressGet-AzPublicIpAddress 获取有关负载均衡器使用的分配公共 IP 地址的信息。Gets information on the public IP address assigned used by the load balancer.
Remove-AzResourceGroupRemove-AzResourceGroup 删除资源组及其中包含的所有资源。Removes a resource group and all resources contained within.

后续步骤Next steps

有关 Azure PowerShell 模块的详细信息,请参阅 Azure PowerShell 文档For more information on the Azure PowerShell module, see Azure PowerShell documentation.