教程:使用 Azure PowerShell 创建和管理 Windows VMTutorial: Create and Manage Windows VMs with Azure PowerShell

Azure 虚拟机提供完全可配置的灵活计算环境。Azure virtual machines provide a fully configurable and flexible computing environment. 本教程介绍 Azure 虚拟机 (VM) 的基本部署任务,例如选择 VM 大小、选择 VM 映像和部署 VM。This tutorial covers basic Azure virtual machine (VM) deployment tasks like selecting a VM size, selecting a VM image, and deploying a VM. 你将学习如何执行以下操作:You learn how to:

  • 创建并连接到 VMCreate and connect to a VM
  • 选择并使用 VM 映像Select and use VM images
  • 查看和使用特定 VM 大小View and use specific VM sizes
  • 调整 VM 的大小Resize a VM
  • 查看并了解 VM 状态View and understand VM state

启动 Azure PowerShellLaunch Azure PowerShell

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.

创建资源组Create resource group

使用 New-AzResourceGroup 命令创建资源组。Create a resource group with the New-AzResourceGroup command.

Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed. 必须在创建虚拟机前创建资源组。A resource group must be created before a virtual machine. 在以下示例中,在“ChinaEast”区域中创建了名为“myResourceGroupVM”的资源组:In the following example, a resource group named myResourceGroupVM is created in the ChinaEast region:

Connect-AzAccount -Environment AzureChinaCloud
New-AzResourceGroup `
   -ResourceGroupName "myResourceGroupVM" `
   -Location "ChinaEast"

创建或修改 VM 时指定资源组,本教程会对此进行演示。The resource group is specified when creating or modifying a VM, which can be seen throughout this tutorial.

创建 VMCreate a VM

创建 VM 时,可使用多个选项,例如操作系统映像、网络配置和管理凭据。When creating a VM, several options are available like operating system image, network configuration, and administrative credentials. 此示例创建名为 myVM 的 VM,运行默认版本的 Windows Server 2016 Datacenter。This example creates a VM named myVM, running the default version of Windows Server 2016 Datacenter.

使用 Get-Credential 设置 VM 上管理员帐户所需的用户名和密码:Set the username and password needed for the administrator account on the VM with Get-Credential:

$cred = Get-Credential

使用 New-AzVM 创建 VM。Create the VM with New-AzVM.

New-AzVm `
    -ResourceGroupName "myResourceGroupVM" `
    -Name "myVM" `
    -Location "ChinaEast" `
    -VirtualNetworkName "myVnet" `
    -SubnetName "mySubnet" `
    -SecurityGroupName "myNetworkSecurityGroup" `
    -PublicIpAddressName "myPublicIpAddress" `
    -Credential $cred

连接到 VMConnect to VM

在部署完成后,创建到 VM 的远程桌面连接。After the deployment has completed, create a remote desktop connection with the VM.

运行以下命令,以返回 VM 的公共 IP 地址。Run the following commands to return the public IP address of the VM. 需记下此 IP 地址,以便在后续步骤中使用浏览器连接到它测试 Web 连接。Take note of this IP Address so you can connect to it with your browser to test web connectivity in a future step.

Get-AzPublicIpAddress `
   -ResourceGroupName "myResourceGroupVM"  | Select IpAddress

在本地计算机上使用以下命令创建与 VM 的远程桌面会话。Use the following command, on your local machine, to create a remote desktop session with the VM. 将 IP 地址替换为你的 VM 的 publicIPAddressReplace the IP address with the publicIPAddress of your VM. 出现提示时,输入创建 VM 时使用的凭据。When prompted, enter the credentials used when creating the VM.

mstsc /v:<publicIpAddress>

在“Windows 安全性”窗口中,依次选择“更多选择”、“使用其他帐户”。In the Windows Security window, select More choices and then Use a different account. 键入针对 VM 创建的用户名和密码,然后单击“确定”。Type the username and password you created for the VM and then click OK.

了解市场映像Understand marketplace images

Azure 市场包括许多可用于新建 VM 的映像。The Azure marketplace includes many images that can be used to create a new VM. 在之前的步骤中,使用 Windows Server 2016 Datacenter 映像创建了 VM。In the previous steps, a VM was created using the Windows Server 2016 Datacenter image. 在此步骤中,我们将使用 PowerShell 模块在市场中搜索其他 Windows 映像,这些映像也可用作新 VM 的基础。In this step, the PowerShell module is used to search the marketplace for other Windows images, which can also be used as a base for new VMs. 此过程包括查找发布者、产品/服务、SKU,以及用于标识映像的版本号(可选)。This process consists of finding the publisher, offer, SKU, and optionally a version number to identify the image.

使用 Get-AzVMImagePublisher 命令返回映像发布者的列表:Use the Get-AzVMImagePublisher command to return a list of image publishers:

Get-AzVMImagePublisher -Location "ChinaEast"

使用 Get-AzVMImageOffer 返回映像套餐的列表。Use the Get-AzVMImageOffer to return a list of image offers. 使用此命令,返回筛选了指定发布者(名为 MicrosoftWindowsServer)的列表:With this command, the returned list is filtered on the specified publisher named MicrosoftWindowsServer:

Get-AzVMImageOffer `
   -Location "ChinaEast" `
   -PublisherName "MicrosoftWindowsServer"

结果将如以下示例所示:The results will look something like this example:

Offer             PublisherName          Location
-----             -------------          --------
Windows-HUB       MicrosoftWindowsServer ChinaEast
WindowsServer     MicrosoftWindowsServer ChinaEast
WindowsServer-HUB MicrosoftWindowsServer ChinaEast

然后,使用 Get-AzVMImageSku 命令对发布者和套餐名称进行筛选,以返回映像名称的列表。The Get-AzVMImageSku command will then filter on the publisher and offer name to return a list of image names.

Get-AzVMImageSku `
   -Location "ChinaEast" `
   -PublisherName "MicrosoftWindowsServer" `
   -Offer "WindowsServer"

结果将如以下示例所示:The results will look something like this example:

Skus                                      Offer         PublisherName          Location
----                                      -----         -------------          --------
2008-R2-SP1                               WindowsServer MicrosoftWindowsServer ChinaEast  
2008-R2-SP1-smalldisk                     WindowsServer MicrosoftWindowsServer ChinaEast  
2012-Datacenter                           WindowsServer MicrosoftWindowsServer ChinaEast  
2012-Datacenter-smalldisk                 WindowsServer MicrosoftWindowsServer ChinaEast  
2012-R2-Datacenter                        WindowsServer MicrosoftWindowsServer ChinaEast  
2012-R2-Datacenter-smalldisk              WindowsServer MicrosoftWindowsServer ChinaEast  
2016-Datacenter                           WindowsServer MicrosoftWindowsServer ChinaEast  
2016-Datacenter-Server-Core               WindowsServer MicrosoftWindowsServer ChinaEast  
2016-Datacenter-Server-Core-smalldisk     WindowsServer MicrosoftWindowsServer ChinaEast
2016-Datacenter-smalldisk                 WindowsServer MicrosoftWindowsServer ChinaEast
2016-Datacenter-with-Containers           WindowsServer MicrosoftWindowsServer ChinaEast
2016-Datacenter-with-Containers-smalldisk WindowsServer MicrosoftWindowsServer ChinaEast
2016-Datacenter-with-RDSH                 WindowsServer MicrosoftWindowsServer ChinaEast
2016-Nano-Server                          WindowsServer MicrosoftWindowsServer ChinaEast

此信息可用于部署具有特定映像的 VM。This information can be used to deploy a VM with a specific image. 此示例通过将最新版本的 Windows Server 2016 与容器映像配合使用来部署 VM。This example deploys a VM using the latest version of a Windows Server 2016 with Containers image.

New-AzVm `
    -ResourceGroupName "myResourceGroupVM" `
    -Name "myVM2" `
    -Location "ChinaEast" `
    -VirtualNetworkName "myVnet" `
    -SubnetName "mySubnet" `
    -SecurityGroupName "myNetworkSecurityGroup" `
    -PublicIpAddressName "myPublicIpAddress2" `
    -ImageName "MicrosoftWindowsServer:WindowsServer:2016-Datacenter-with-Containers:latest" `
    -Credential $cred `
    -AsJob

-AsJob 参数以后台任务的方式创建 VM,因此 PowerShell 提示符会返回到你所在的位置。The -AsJob parameter creates the VM as a background task, so the PowerShell prompts return to you. 可以通过 Get-Job cmdlet 查看后台作业的详细信息。You can view details of background jobs with the Get-Job cmdlet.

了解 VM 大小Understand VM sizes

VM 大小决定 VM 可用计算资源(如 CPU、GPU 和内存)的数量。The VM size determines the amount of compute resources like CPU, GPU, and memory that are made available to the VM. 创建的虚拟机大小应适合工作负荷。Virtual machines should be created using a VM size appropriate for the workload. 如果工作负荷增加,也可重设现有虚拟机的大小。If a workload increases, an existing virtual machine can also be resized.

VM 大小VM Sizes

下表将大小分类成了多个用例。The following table categorizes sizes into use cases.

类型Type 常见大小Common sizes 说明Description
常规用途General purpose B、Dsv3、Dv3、DSv2、Dv2、Av2B, Dsv3, Dv3, DSv2, Dv2, Av2 CPU 与内存之比均衡。Balanced CPU-to-memory. 适用于开发/测试、小到中型应用程序和数据解决方案。Ideal for dev / test and small to medium applications and data solutions.
计算优化Compute optimized Fsv2、Fs、FFsv2, Fs, F 高 CPU 与内存之比。High CPU-to-memory. 适用于中等流量的应用程序、网络设备和批处理。Good for medium traffic applications, network appliances, and batch processes.
内存优化Memory optimized Esv3、Ev3、M、DSv2、Dv2Esv3, Ev3, M, DSv2, Dv2 较高的内存核心比。High memory-to-core. 适用于关系数据库、中到大型缓存和内存中分析。Great for relational databases, medium to large caches, and in-memory analytics.
GPUGPU NCv3NCv3 专门针对大量图形绘制和视频编辑的 VM。Specialized VMs targeted for heavy graphic rendering and video editing.

查找可用的 VM 大小Find available VM sizes

若要查看在特定区域可用的 VM 大小的列表,请使用 Get-AzVMSize 命令。To see a list of VM sizes available in a particular region, use the Get-AzVMSize command.

Get-AzVMSize -Location "ChinaEast"

调整 VM 的大小Resize a VM

部署 VM 后,可调整其大小以增加或减少资源分配。After a VM has been deployed, it can be resized to increase or decrease resource allocation.

调整 VM 大小之前,请检查所需的大小在当前 VM 群集上是否可用。Before resizing a VM, check if the size you want is available on the current VM cluster. 使用 Get AzVMSize 命令返回大小的列表。The Get-AzVMSize command returns a list of sizes.

Get-AzVMSize -ResourceGroupName "myResourceGroupVM" -VMName "myVM"

如果大小可用,则可从开机状态调整 VM 大小,但需在此操作期间重启 VM。If the size is available, the VM can be resized from a powered-on state, however it is rebooted during the operation.

$vm = Get-AzVM `
   -ResourceGroupName "myResourceGroupVM"  `
   -VMName "myVM"
$vm.HardwareProfile.VmSize = "Standard_DS3_v2"
Update-AzVM `
   -VM $vm `
   -ResourceGroupName "myResourceGroupVM"

如果所需大小在当前群集上不可用,则需解除分配 VM,才能执行重设大小操作。If the size you want isn't available on the current cluster, the VM needs to be deallocated before the resize operation can occur. 解除分配 VM 时会删除临时磁盘上的任何数据,并且如果不使用静态 IP 地址,则公共 IP 地址会发生更改。Deallocating a VM will remove any data on the temp disk, and the public IP address will change unless a static IP address is being used.

Stop-AzVM `
   -ResourceGroupName "myResourceGroupVM" `
   -Name "myVM" -Force
$vm = Get-AzVM `
   -ResourceGroupName "myResourceGroupVM"  `
   -VMName "myVM"
$vm.HardwareProfile.VmSize = "Standard_E2s_v3"
Update-AzVM -VM $vm `
   -ResourceGroupName "myResourceGroupVM"
Start-AzVM `
   -ResourceGroupName "myResourceGroupVM"  `
   -Name $vm.name

VM 电源状态VM power states

Azure VM 可能会处于多种电源状态之一。An Azure VM can have one of many power states.

电源状态Power State 说明Description
正在启动Starting 正在启动虚拟机。The virtual machine is being started.
正在运行Running 虚拟机正在运行。The virtual machine is running.
正在停止Stopping 正在停止虚拟机。The virtual machine is being stopped.
已停止Stopped VM 已停止。The VM is stopped. 虚拟机处于停止状态时仍会产生计算费用。Virtual machines in the stopped state still incur compute charges.
正在解除分配Deallocating VM 正解除分配。The VM is being deallocated.
已解除分配Deallocated 指示 VM 已从监控程序中删除,但仍可在控制面板中使用。Indicates that the VM is removed from the hypervisor but is still available in the control plane. 处于 Deallocated 状态的虚拟机不会产生计算费用。Virtual machines in the Deallocated state do not incur compute charges.
- VM 的电源状态未知。The power state of the VM is unknown.

若要获取特定 VM 的状态,请使用 Get-AzVM 命令。To get the state of a particular VM, use the Get-AzVM command. 请确保为 VM 和资源组指定有效的名称。Be sure to specify a valid name for a VM and resource group.

Get-AzVM `
    -ResourceGroupName "myResourceGroupVM" `
    -Name "myVM" `
    -Status | Select @{n="Status"; e={$_.Statuses[1].Code}}

输出将如以下示例所示:The output will look something like this example:

Status
------
PowerState/running

管理任务Management tasks

在 VM 生命周期中,可能需要运行管理任务,例如启动、停止或删除 VM。During the lifecycle of a VM, you may want to run management tasks like starting, stopping, or deleting a VM. 此外,可能还需要创建脚本来自动执行重复或复杂的任务。Additionally, you may want to create scripts to automate repetitive or complex tasks. 使用 Azure PowerShell,可从命令行或脚本运行许多常见的管理任务。Using Azure PowerShell, many common management tasks can be run from the command line or in scripts.

停止 VMStop a VM

使用 Stop-AzVM 停止并解除分配 VM:Stop and deallocate a VM with Stop-AzVM:

Stop-AzVM `
   -ResourceGroupName "myResourceGroupVM" `
   -Name "myVM" -Force

若要让 VM 保持已预配状态,请使用 -StayProvisioned 参数。If you want to keep the VM in a provisioned state, use the -StayProvisioned parameter.

启动 VMStart a VM

Start-AzVM `
   -ResourceGroupName "myResourceGroupVM" `
   -Name "myVM"

删除资源组Delete resource group

删除资源组时,会删除其中的一切。Everything inside of a resource group is deleted when you delete the resource group.

Remove-AzResourceGroup `
   -Name "myResourceGroupVM" `
   -Force

后续步骤Next steps

在本教程中,你已学习 VM 创建和管理的基本知识,例如如何:In this tutorial, you learned about basic VM creation and management such as how to:

  • 创建并连接到 VMCreate and connect to a VM
  • 选择并使用 VM 映像Select and use VM images
  • 查看和使用特定 VM 大小View and use specific VM sizes
  • 调整 VM 的大小Resize a VM
  • 查看并了解 VM 状态View and understand VM state

请转到下一教程,了解 VM 磁盘。Advance to the next tutorial to learn about VM disks.