用于创建和管理 Azure 虚拟机的常用 PowerShell 命令Common PowerShell commands for creating and managing Azure Virtual Machines

本文介绍一些可用于在 Azure 订阅中创建和管理虚拟机的 Azure PowerShell 命令。This article covers some of the Azure PowerShell commands that you can use to create and manage virtual machines in your Azure subscription. 若要获取特定命令行开关和选项的详细帮助,可以使用 Get-Help 命令。For more detailed help with specific command-line switches and options, you can use the Get-Help command.

如果在本文运行多个命令,以下变量可能对你有用:These variables might be useful for you if running more than one of the commands in this article:

  • $location - 虚拟机的位置。$location - The location of the virtual machine. 可以使用 Get-AzLocation 查找适合你的地理区域You can use Get-AzLocation to find a geographical region that works for you.
  • $myResourceGroup - 包含虚拟机的资源组的名称。$myResourceGroup - The name of the resource group that contains the virtual machine.
  • $myVM - 虚拟机的名称。$myVM - The name of the virtual machine.

创建 VM - 简化Create a VM - simplified

任务Task 命令Command
创建简单的 VMCreate a simple VM New-AzVM -Name $myVMNew-AzVM -Name $myVM

New-AzVM 有一组简化的参数,其中只有名称是必需的。New-AzVM has a set of simplified parameters, where all that is required is a single name. -Name 的值将用作创建新 VM 所需的所有资源的名称。The value for -Name will be used as the name for all of the resources required for creating a new VM. 你可以指定更多,但只需要这个。You can specify more, but this is all that is required.
从自定义映像创建 VMCreate a VM from a custom image New-AzVm -ResourceGroupName $myResourceGroup -Name $myVM ImageName "myImage" -Location $locationNew-AzVm -ResourceGroupName $myResourceGroup -Name $myVM ImageName "myImage" -Location $location

需要已创建自己的托管映像You need to have already created your own managed image. 可以使用一个映像生成多个相同的 VM。You can use an image to make multiple, identical VMs.

创建 VM 配置Create a VM configuration

任务Task 命令Command
创建 VM 配置Create a VM configuration $vm = New-AzVMConfig -VMName $myVM -VMSize "Standard_D1_v1"$vm = New-AzVMConfig -VMName $myVM -VMSize "Standard_D1_v1"

VM 配置用于定义或更新 VM 的设置。The VM configuration is used to define or update settings for the VM. 使用 VM 的名称及其大小对配置进行初始化。The configuration is initialized with the name of the VM and its size.
添加配置设置Add configuration settings $vm = Set-AzVMOperatingSystem -VM $vm -Windows -ComputerName $myVM -Credential $cred -ProvisionVMAgent -EnableAutoUpdate$vm = Set-AzVMOperatingSystem -VM $vm -Windows -ComputerName $myVM -Credential $cred -ProvisionVMAgent -EnableAutoUpdate

向以前使用 New-AzVMConfig 创建的配置对象添加包括凭据在内的操作系统设置。Operating system settings including credentials are added to the configuration object that you previously created using New-AzVMConfig.
添加网络接口Add a network interface $vm = Add-AzVMNetworkInterface -VM $vm -Id $nic.Id$vm = Add-AzVMNetworkInterface -VM $vm -Id $nic.Id

VM 必须使用网络接口在虚拟网络中通信。A VM must have a network interface to communicate in a virtual network. 还可使用 Get-AzNetworkInterface 检索现有网络接口对象。You can also use Get-AzNetworkInterface to retrieve an existing network interface object.
指定平台映像Specify a platform image $vm = Set-AzVMSourceImage -VM $vm -PublisherName "publisher_name" -Offer "publisher_offer" -Skus "product_sku" -Version "latest"$vm = Set-AzVMSourceImage -VM $vm -PublisherName "publisher_name" -Offer "publisher_offer" -Skus "product_sku" -Version "latest"

向以前使用 New-AzVMConfig 创建的配置对象添加映像信息Image information is added to the configuration object that you previously created using New-AzVMConfig. 仅当将操作系统磁盘设置为使用平台映像时,才使用此命令返回的对象。The object returned from this command is only used when you set the OS disk to use a platform image.
创建 VMCreate a VM New-AzVM -ResourceGroupName $myResourceGroup -Location $location -VM $vmNew-AzVM -ResourceGroupName $myResourceGroup -Location $location -VM $vm

所有资源在资源组中创建。All resources are created in a resource group. 运行此命令之前,请运行 New-AzVMConfig、Set-AzVMOperatingSystem、Set-AzVMSourceImage、Add-AzVMNetworkInterface 和 Set-AzVMOSDisk。Before you run this command, run New-AzVMConfig, Set-AzVMOperatingSystem, Set-AzVMSourceImage, Add-AzVMNetworkInterface, and Set-AzVMOSDisk.
更新 VMUpdate a VM Update-AzVM -ResourceGroupName $myResourceGroup -VM $vmUpdate-AzVM -ResourceGroupName $myResourceGroup -VM $vm

使用 Get-AzVM 获取当前 VM 配置,更改 VM 对象上的配置设置,并运行此命令。Get the current VM configuration using Get-AzVM, change configuration settings on the VM object, and then run this command.

获取有关 VM 的信息Get information about VMs

任务Task 命令Command
列出订阅中的 VMList VMs in a subscription Get-AzVMGet-AzVM
列出资源组中的 VMList VMs in a resource group Get-AzVM -ResourceGroupName $myResourceGroupGet-AzVM -ResourceGroupName $myResourceGroup

若要获取订阅中的资源组列表,请使用 Get-AzResourceGroupTo get a list of resource groups in your subscription, use Get-AzResourceGroup.
获取有关 VM 的信息Get information about a VM Get-AzVM -ResourceGroupName $myResourceGroup -Name $myVMGet-AzVM -ResourceGroupName $myResourceGroup -Name $myVM

管理 VMManage VMs

任务Task 命令Command
启动 VMStart a VM Start-AzVM -ResourceGroupName $myResourceGroup -Name $myVMStart-AzVM -ResourceGroupName $myResourceGroup -Name $myVM
停止 VMStop a VM Stop-AzVM -ResourceGroupName $myResourceGroup -Name $myVMStop-AzVM -ResourceGroupName $myResourceGroup -Name $myVM
重启正在运行的 VMRestart a running VM Restart-AzVM -ResourceGroupName $myResourceGroup -Name $myVMRestart-AzVM -ResourceGroupName $myResourceGroup -Name $myVM
删除 VMDelete a VM Remove-AzVM -ResourceGroupName $myResourceGroup -Name $myVMRemove-AzVM -ResourceGroupName $myResourceGroup -Name $myVM

后续步骤Next steps