您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:使用 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。This tutorial covers basic Azure virtual machine deployment items such as 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 Cloud ShellLaunch Azure Cloud Shell

Azure Cloud Shell 是免费的交互式 shell,可以使用它运行本文中的步骤。The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. 它预安装有常用 Azure 工具并将其配置与帐户一起使用。It has common Azure tools preinstalled and configured to use with your account. 请直接单击“复制”对代码进行复制,将其粘贴到 Cloud Shell 中,然后按 Enter 来运行它。Just click the Copy to copy the code, paste it into the Cloud Shell, and then press enter to run it. 可通过多种方式来启动 Cloud Shell:There are a few ways to launch the Cloud Shell:

单击代码块右上角的“试用”。Click Try It in the upper right corner of a code block. 本文中的 Cloud Shell
在浏览器中打开 Cloud Shell。Open Cloud Shell in your browser. https://shell.azure.com/powershell
单击 Azure 门户右上角菜单上的“Cloud Shell”按钮。Click the Cloud Shell button on the menu in the upper right of the Azure portal. 门户中的 Cloud ShellCloud Shell in the portal

如果选择在本地安装并使用 PowerShell,则本教程需要 Azure PowerShell 模块 5.7.0 或更高版本。If you choose to install and use the PowerShell locally, this tutorial requires the Azure PowerShell module version 5.7.0 or later. 运行 Get-Module -ListAvailable AzureRM 即可查找版本。Run Get-Module -ListAvailable AzureRM to find the version. 如果需要升级,请参阅安装 Azure PowerShell 模块If you need to upgrade, see Install Azure PowerShell module. 如果在本地运行 PowerShell,则还需运行 Connect-AzureRmAccount 以创建与 Azure 的连接。If you are running PowerShell locally, you also need to run Connect-AzureRmAccount to create a connection with Azure.

创建资源组Create resource group

使用 New-AzureRmResourceGroup 命令创建资源组。Create a resource group with the New-AzureRmResourceGroup 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. 在以下示例中,在“EastUS”区域中创建了名为“myResourceGroupVM”的资源组:In the following example, a resource group named myResourceGroupVM is created in the EastUS region:

New-AzureRmResourceGroup -ResourceGroupName "myResourceGroupVM" -Location "EastUS"

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

创建虚拟机Create virtual machine

创建虚拟机时,可使用多个选项,例如操作系统映像、网络配置和管理凭据。When creating a virtual machine, several options are available such as operating system image, network configuration, and administrative credentials. 在此示例中,将创建名为“myVM”的虚拟机,它运行默认的 Windows Server 2016 Datacenter 最新版本。In this example, a virtual machine is created with a name of myVM running the default latest version of Windows Server 2016 Datacenter.

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

$cred = Get-Credential

使用 New-AzureRmVM 创建虚拟机。Create the virtual machine with New-AzureRmVM.

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

连接到 VMConnect to VM

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

运行以下命令,以返回虚拟机的公共 IP 地址。Run the following commands to return the public IP address of the virtual machine. 需记下此 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-AzureRmPublicIpAddress -ResourceGroupName "myResourceGroupVM"  | Select IpAddress

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

mstsc /v:<publicIpAddress>

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

了解 VM 映像Understand VM images

Azure 市场包括许多可用于新建虚拟机的虚拟机映像。The Azure marketplace includes many virtual machine images that can be used to create a new virtual machine. 在之前的步骤中,使用 Windows Server 2016 Datacenter 映像创建了虚拟机。In the previous steps, a virtual machine 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 AzureRmVMImagePublisher 命令返回映像发布者的列表:Use the Get-AzureRmVMImagePublisher command to return a list of image publishers:

Get-AzureRmVMImagePublisher -Location "EastUS"

使用 Get AzureRmVMImageOffer 返回映像产品/服务的列表。Use the Get-AzureRmVMImageOffer to return a list of image offers. 使用此命令,返回筛选了特定发布者的列表:With this command, the returned list is filtered on the specified publisher:

Get-AzureRmVMImageOffer -Location "EastUS" -PublisherName "MicrosoftWindowsServer"
Offer             PublisherName          Location
-----             -------------          -------- 
Windows-HUB       MicrosoftWindowsServer EastUS 
WindowsServer     MicrosoftWindowsServer EastUS   
WindowsServer-HUB MicrosoftWindowsServer EastUS   

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

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

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

New-AzureRmVm `
    -ResourceGroupName "myResourceGroupVM" `
    -Name "myVM2" `
    -Location "EastUS" `
    -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

虚拟机大小决定虚拟机可用计算资源(如 CPU、GPU 和内存)的数量。A virtual machine size determines the amount of compute resources such as CPU, GPU, and memory that are made available to the virtual machine. 创建的虚拟机大小需适合预期的工作负荷。Virtual machines need to be created with a size appropriate for the expect workload. 如果工作负荷增加,则可调整现有虚拟机的大小。If workload increases, an existing virtual machine can be resized.

VM 大小VM Sizes

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

TypeType 常见大小Common sizes 说明Description
常规用途General purpose Dsv3、Dv3、DSv2、Dv2、DS、D、Av2、A0-7Dsv3, Dv3, DSv2, Dv2, DS, D, Av2, A0-7 CPU 与内存之比均衡。Balanced CPU-to-memory. 适用于开发/测试、小到中型应用程序和数据解决方案。Ideal for dev / test and small to medium applications and data solutions.
计算优化Compute optimized Fs, FFs, F 高 CPU 与内存之比。High CPU-to-memory. 适用于中等流量的应用程序、网络设备和批处理。Good for medium traffic applications, network appliances, and batch processes.
内存优化Memory optimized Esv3、Ev3、M、GS、G、DSv2、DS、Dv2、DEsv3, Ev3, M, GS, G, DSv2, DS, Dv2, D 较高的内存核心比。High memory-to-core. 适用于关系数据库、中到大型缓存和内存中分析。Great for relational databases, medium to large caches, and in-memory analytics.
存储优化Storage optimized LSLs 高磁盘吞吐量和 IO。High disk throughput and IO. 适用于大数据、SQL 和 NoSQL 数据库。Ideal for Big Data, SQL, and NoSQL databases.
GPUGPU NV, NCNV, NC 专门针对大量图形绘制和视频编辑的 VM。Specialized VMs targeted for heavy graphic rendering and video editing.
高性能High performance H, A8-11H, A8-11 功能极其强大的 CPU VM 具有可选的高吞吐量网络接口 (RDMA)。Our most powerful CPU VMs with optional high-throughput network interfaces (RDMA).

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

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

Get-AzureRmVMSize -Location "EastUS"

调整 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 desired size is available on the current VM cluster. 使用 Get AzureRmVMSize 命令返回大小的列表。The Get-AzureRmVMSize command returns a list of sizes.

Get-AzureRmVMSize -ResourceGroupName "myResourceGroupVM" -VMName "myVM"

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

$vm = Get-AzureRmVM -ResourceGroupName "myResourceGroupVM"  -VMName "myVM"
$vm.HardwareProfile.VmSize = "Standard_D4"
Update-AzureRmVM -VM $vm -ResourceGroupName "myResourceGroupVM"

如果所需大小在当前群集上不可用,则需解除分配 VM,才能执行调整大小操作。If the desired size is not on the current cluster, the VM needs to be deallocated before the resize operation can occur. 请注意,重新启动 VM 时会删除临时磁盘上的任何数据,并且如果不使用静态 IP 地址,则公共 IP 地址会发生更改。Note, when the VM is powered back on, any data on the temp disk are removed, and the public IP address change unless a static IP address is being used.

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

VM 电源状态VM power states

Azure VM 可能会处于多种电源状态之一。An Azure VM can have one of many power states. 从虚拟机监控程序的角度来看,此状态表示 VM 的当前状态。This state represents the current state of the VM from the standpoint of the hypervisor.

电源状态Power states

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

查找电源状态Find power state

若要检索特定 VM 的状态,请使用 Get-AzureRmVM 命令。To retrieve the state of a particular VM, use the Get-AzureRmVM command. 请确保为虚拟机和资源组指定有效的名称。Be sure to specify a valid name for a virtual machine and resource group.

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

输出:Output:

Status
------
PowerState/running

管理任务Management tasks

在虚拟机生命周期中,可能需要运行管理任务,例如启动、停止或删除虚拟机。During the lifecycle of a virtual machine, you may want to run management tasks such as starting, stopping, or deleting a virtual machine. 此外,可能还需要创建脚本来自动执行重复或复杂的任务。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.

停止虚拟机Stop virtual machine

使用 Stop-AzureRmVM 停止并解除分配虚拟机:Stop and deallocate a virtual machine with Stop-AzureRmVM:

Stop-AzureRmVM -ResourceGroupName "myResourceGroupVM" -Name "myVM" -Force

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

启动虚拟机Start virtual machine

Start-AzureRmVM -ResourceGroupName "myResourceGroupVM" -Name "myVM"

删除资源组Delete resource group

删除资源组会删除其包含的所有资源。Deleting a resource group also deletes all resources contained within.

Remove-AzureRmResourceGroup -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.