快速入门:使用 Azure CLI 创建 Windows 虚拟机Quickstart: Create a Windows virtual machine with the Azure CLI

Azure CLI 用于从命令行或脚本创建和管理 Azure 资源。The Azure CLI is used to create and manage Azure resources from the command line or in scripts. 本快速入门展示了如何使用 Azure CLI 在 Azure 中部署运行 Windows Server 2016 的虚拟机 (VM)。This quickstart shows you how to use the Azure CLI to deploy a virtual machine (VM) in Azure that runs Windows Server 2016. 若要查看运行中的 VM,可以通过 RDP 登录到该 VM 并安装 IIS Web 服务器。To see your VM in action, you then RDP to the VM and install the IIS web server.

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

启动 Azure 本地 ShellLaunch Azure local Shell

可以启动 Azure 本地 CLI 控制台以运行以下脚本。You can launch the Azure local CLI console to run the following scripts.

备注

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

创建资源组Create a resource group

使用“az group create”命令创建资源组。Create a resource group with the az group create command. Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed. 以下示例在“chinaeast” 位置创建名为“myResourceGroup” 的资源组:The following example creates a resource group named myResourceGroup in the chinaeast location:

az group create --name myResourceGroup --location chinaeast

创建虚拟机Create virtual machine

使用 az vm create 创建 VM。Create a VM with az vm create. 以下示例创建一个名为 myVM 的 VM。The following example creates a VM named myVM. 此示例使用 azureuser 作为管理用户名。This example uses azureuser for an administrative user name.

你将需要提供符合 Azure VM 密码要求的密码。You will need to supply a password that meets the password requirements for Azure VMs. 使用以下示例时,系统将提示你在命令行中输入密码。Using the example below, you will be prompted to enter a password at the command line. 你还可以在 --admin-password 参数中添加密码值。You could also add the the --admin-password parameter with a value for your password. 用户名和密码将在以后连接到 VM 时使用。The user name and password will be used later, when you connect to the VM.

az vm create \
    --resource-group myResourceGroup \
    --name myVM \
    --image win2016datacenter \
    --admin-username azureuser 

创建 VM 和支持资源需要几分钟时间。It takes a few minutes to create the VM and supporting resources. 以下示例输出表明 VM 创建操作已成功。The following example output shows the VM create operation was successful.

{
  "fqdns": "",
  "id": "/subscriptions/<guid>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM",
  "location": "chinaeast",
  "macAddress": "00-0D-3A-23-9A-49",
  "powerState": "VM running",
  "privateIpAddress": "10.0.0.4",
  "publicIpAddress": "52.174.34.95",
  "resourceGroup": "myResourceGroup"
}

记下 VM 输出中自己的 publicIpAddressNote your own publicIpAddress in the output from your VM. 在后续步骤中,将使用此地址访问 VM。This address is used to access the VM in the next steps.

为 Web 流量打开端口 80Open port 80 for web traffic

默认情况下,在 Azure 中创建 Windows VM 时仅会打开 RDP 连接。By default, only RDP connections are opened when you create a Windows VM in Azure. 请使用 az vm open-port 打开 TCP 端口 80 以供 IIS Web 服务器使用:Use az vm open-port to open TCP port 80 for use with the IIS web server:

az vm open-port --port 80 --resource-group myResourceGroup --name myVM

连接到虚拟机Connect to virtual machine

使用以下命令从本地计算机创建远程桌面会话。Use the following command to create a remote desktop session from your local computer. 将 IP 地址替换为你的 VM 的公共 IP 地址。Replace the IP address with the public IP address of your VM. 出现提示时,输入创建 VM 时使用的凭据:When prompted, enter the credentials used when the VM was created:

mstsc /v:publicIpAddress

安装 Web 服务器Install web server

若要查看运行中的 VM,请安装 IIS Web 服务器。To see your VM in action, install the IIS web server. 在 VM 中打开 PowerShell 提示符并运行以下命令:Open a PowerShell prompt on the VM and run the following command:

Install-WindowsFeature -name Web-Server -IncludeManagementTools

完成后,关闭到 VM 的 RDP 连接。When done, close the RDP connection to the VM.

查看运行中的 Web 服务器View the web server in action

如果 IIS 已安装,并且 VM 上的端口 80 已对 Internet 开放, 则可以使用所选的 Web 浏览器查看默认的 IIS 欢迎页。With IIS installed and port 80 now open on your VM from the Internet, use a web browser of your choice to view the default IIS welcome page. 使用上一步中获取的 VM 的公共 IP 地址。Use the public IP address of your VM obtained in a previous step. 以下示例展示了默认 IIS 网站:The following example shows the default IIS web site:

IIS 默认站点

清理资源Clean up resources

如果不再需要资源组、VM 和所有相关的资源,可以使用 az group delete 命令将其删除:When no longer needed, you can use the az group delete command to remove the resource group, VM, and all related resources:

az group delete --name myResourceGroup

后续步骤Next steps

在本快速入门中,你部署了简单的虚拟机,打开了 Web 流量的网络端口,并安装了一个基本 Web 服务器。In this quickstart, you deployed a simple virtual machine, open a network port for web traffic, and installed a basic web server. 若要深入了解 Azure 虚拟机,请继续学习 Windows VM 教程。To learn more about Azure virtual machines, continue to the tutorial for Windows VMs.