使用 DSC 创建具有 IIS 的 VMCreate a VM with IIS using DSC

此脚本创建一个虚拟机,并使用 Azure 虚拟机 DSC 自定义脚本扩展安装和配置 IIS。This script creates a virtual machine, and uses the Azure Virtual Machine DSC custom script extension to install and configure IIS.

若要运行此示例,请安装最新版本的 Azure CLITo run this sample, install the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

适用于 Azure CLI 的示例是针对 bash shell 编写的。Samples for the Azure CLI are written for the bash shell. 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

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

示例脚本Sample script

#!/bin/bash

# Sign in the Azure China Cloud
az cloud set -n AzureChinaCloud
az login

# Update for your admin password
AdminPassword=ChangeYourAdminPassword1

# Create a resource group.
az group create --name myResourceGroup --location chinanorth

# Create a VM
az vm create \
    --resource-group myResourceGroup \
    --name myVM \
    --image win2016datacenter \
    --admin-username azureuser \
    --admin-password $AdminPassword

# Start a CustomScript extension to use a simple bash script to update, download and install WordPress and MySQL 
az vm extension set \
   --name DSC \
   --publisher Microsoft.Powershell \
   --version 2.19 \
   --vm-name myVM \
   --resource-group myResourceGroup \
   --settings '{"ModulesURL":"https://github.com/Azure/azure-quickstart-templates/raw/master/dsc-extension-iis-server-windows-vm/ContosoWebsite.ps1.zip", "configurationFunction": "ContosoWebsite.ps1\\ContosoWebsite", "Properties": {"MachineName": "myVM"} }'

  # open port 80 to allow web traffic to host
  az vm open-port \
    --port 80 \
    --resource-group myResourceGroup \
    --name myVM

清理部署Clean up deployment

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

az group delete --name myResourceGroup --yes

脚本说明Script explanation

此脚本使用以下命令创建资源组、虚拟机和所有相关资源。This script uses the following commands to create a resource group, virtual machine, and all related resources. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 注释Notes
az group createaz group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az vm createaz vm create 创建虚拟机并将其连接到网卡、虚拟网络、子网和 NSG。Creates the virtual machine and connects it to the network card, virtual network, subnet, and NSG. 此命令还指定要使用的虚拟机映像和管理凭据。This command also specifies the virtual machine image to be used, and administrative credentials.
az vm extension setaz vm extension set 将自定义脚本扩展添加到虚拟机,此扩展将调用脚本来安装 IIS。Add the Custom Script Extension to the virtual machine which invokes a script to install IIS.
az vm open-portaz vm open-port 创建网络安全组规则,以允许入站流量。Creates a network security group rule to allow inbound traffic. 在此示例中,为 HTTP 流量打开端口 80。In this sample, port 80 is opened for HTTP traffic.
az group deleteaz group delete 删除资源组,包括所有嵌套的资源。Deletes a resource group including all nested resources.

后续步骤Next steps

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

可以在 Azure Windows VM 文档中找到其他虚拟机 CLI 脚本示例。Additional virtual machine CLI script samples can be found in the Azure Windows VM documentation.