使用 Windows PowerShell 管理 Azure Stack HCI 上的 VMManage VMs on Azure Stack HCI using Windows PowerShell

适用于 Azure Stack HCI 版本 20H2;Windows Server 2019Applies to Azure Stack HCI, version 20H2; Windows Server 2019

可以使用 Windows PowerShell 在 Azure Stack HCI 上创建和管理虚拟机 (VM)。Windows PowerShell can be used to create and manage your virtual machines (VMs) on Azure Stack HCI.

通常,你可以从远程计算机(而不是群集中的主机服务器)管理 VM。Typically, you manage VMs from a remote computer, rather than on a host server in a cluster. 此远程计算机称为管理计算机。This remote computer is called the management computer.

备注

从某一管理计算机运行 PowerShell 命令时,请将 -ComputerName 参数与你管理的主机服务器的名称配合使用。When running PowerShell commands from a management computer, include the -ComputerName parameter with the name of the host server you are managing. 允许使用 NetBIOS 名称、IP 地址和完全限定的域名。NetBIOS names, IP addresses, and fully qualified domain names are allowable.

有关使用 PowerShell 管理 VM 的完整参考文档,请参阅 Hyper-V 参考For the complete reference documentation for managing VMs using PowerShell, see Hyper-V reference.

创建 VMCreate a VM

New-VM cmdlet 用于创建新的 VM。The New-VM cmdlet is used to create a new VM. 有关详细用法,请查看 New-VM 参考文档。For detailed usage, see the New-VM reference documentation.

下面是使用现有虚拟硬盘创建新的 VM 时可以指定的设置,其中:Here are the settings that you can specify when creating a new VM with an existing virtual hard disk, where:

  • -Name 是为你要创建的虚拟机提供的名称。-Name is the name that you provide for the virtual machine that you're creating.

  • -MemoryStartupBytes 是启动时可供虚拟机使用的内存量。-MemoryStartupBytes is the amount of memory that is available to the virtual machine at start up.

  • -BootDevice 是虚拟机在启动时引导到的设备。-BootDevice is the device that the virtual machine boots to when it starts. 通常情况下,它是虚拟硬盘 (VHD)、.iso 文件(对于基于 DVD 的引导)或网络适配器 (NetworkAdapter)(对于网络引导)。Typically this is a virtual hard disk (VHD), an .iso file for DVD-based boot, or a network adapter (NetworkAdapter) for network boot.

  • -VHDPath 是要使用的虚拟机磁盘的路径。-VHDPath is the path to the virtual machine disk that you want to use.

  • -Path 是存储虚拟机配置文件的路径。-Path is the path to store the virtual machine configuration files.

  • -Generation 是虚拟机代系。-Generation is the virtual machine generation. 对于 VHD,请使用第 1 代;对于 VHDX,请使用第 2 代。Use generation 1 for VHD and generation 2 for VHDX.

  • -Switch 是你希望虚拟机用来连接其他虚拟机或网络的虚拟交换机的名称。-Switch is the name of the virtual switch that you want the virtual machine to use to connect to other virtual machines or the network. 可以使用 Get-VMSwitch 获取虚拟交换机的名称。Get the name of the virtual switch by using Get-VMSwitch. 例如:For example:

用于创建名为 VM1 的 VM 的完整命令如下所示:The full command as follows for creating a VM called VM1:

New-VM -ComputerName Server1 -Name VM1 -MemoryStartupBytes <Memory> -BootDevice <BootDevice> -VHDPath <VHDPath> -Path <Path> -Generation <Generation> -Switch <SwitchName>

下一个示例将创建内存为 4GB 的第 2 代虚拟机。The next example creates a Generation 2 virtual machine with 4GB of memory. 它从当前目录中的 VMs\Win10.vhdx 文件夹引导,并使用名为 ExternalSwitch 的虚拟交换机。It boots from the folder VMs\Win10.vhdx in the current directory and uses the virtual switch named ExternalSwitch. 虚拟机配置文件存储在 VMData 文件夹中。The virtual machine configuration files are stored in the folder VMData.

New-VM -ComputerName Server1 -Name VM1 -MemoryStartupBytes 4GB -BootDevice VHD -VHDPath .\VMs\Win10.vhdx -Path .\VMData -Generation 2 -Switch ExternalSwitch

以下参数用于指定虚拟硬盘。The following parameters are used to specify virtual hard disks.

若要使用新的虚拟硬盘创建虚拟机,请将以上示例中的 -VHDPath 参数替换为 -NewVHDPath 并添加 -NewVHDSizeBytes 参数,如下所示:To create a virtual machine with a new virtual hard disk, replace the -VHDPath parameter from the example above with -NewVHDPath and add the -NewVHDSizeBytes parameter as shown here:

New-VM -ComputerName Server1 -Name VM1 -MemoryStartupBytes 4GB -BootDevice VHD -NewVHDPath .\VMs\Win10.vhdx -Path .\VMData -NewVHDSizeBytes 20GB -Generation 2 -Switch ExternalSwitch  

若要使用新的虚拟磁盘创建引导到操作系统映像的虚拟机,请参阅为 Windows 10 上的 Hyper-V 创建虚拟机演练中的 PowerShell 示例。To create a virtual machine with a new virtual disk that boots to an operating system image, see the PowerShell example in Create virtual machine walkthrough for Hyper-V on Windows 10.

获取 VM 列表Get a list of VMs

以下示例返回 Server1 上所有 VM 的列表。The following example returns a list of all VMs on Server1.

Get-VM -ComputerName Server1

以下示例使用 Where-Object 命令添加了一个筛选器,返回服务器上所有正在运行的 VM 的列表。The following example returns a list of all running VMs on a server by adding a filter using the Where-Object command. 有关详细信息,请参阅使用 Where-Object 文档。For more information, see Using the Where-Object documentation.

Get-VM -ComputerName Server1 | Where-Object -Property State -eq "Running"

下一个示例将返回服务器上所有已关闭 VM 的列表。The next example returns a list of all shut-down VMs on the server.

Get-VM -ComputerName Server1 | Where-Object -Property State -eq "Off"

启动和停止 VMStart and stop a VM

使用 Start-VMStop-VM 命令启动或停止 VM。Use the Start-VM and Stop-VM commands to start or stop a VM. 有关详细信息,请查看 Start-VMStop-VM 参考文档。For detailed information, see the Start-VM and Stop-VM reference documentation.

以下示例展示了如何启动名为 VM1 的 VM:The following example shows how to start a VM named VM1:

Start-VM -Name VM1 -ComputerName Server1

以下示例展示了如何关闭名为 TestVM 的 VM:The following example shows how to shut-down a VM named TestVM:

Stop-VM -Name VM1 -ComputerName Server1

移动 VMMove a VM

Move-VM cmdlet 可将 VM 移动到其他服务器。The Move-VM cmdlet moves a VM to a different server. 有关详细信息,请查看 Move-VM 参考文档。For more information, see the Move-VM reference documentation.

下面的示例展示了当 VM 存储在 Server1 的 SMB 共享上时,如何将 VM 移动到 Server2:The following example shows how to move a VM to Server2 when the VM is stored on an SMB share on Server1:

Move-VM -ComputerName Server1 -Name VM1 -DestinationHost Server2

下面的示例展示了如何将 VM 从 Server1 移动到 Server2,并将与该 VM 关联的所有文件移动到远程计算机上的 D:\VM_name:The following example shows how to move a VM to Server2 from Server1 and move all files associated with the VM to D:\VM_name on the remote computer:

Move-VM -ComputerName Server1 -Name VM1 -DestinationHost Server2 -IncludeStorage -DestinationStoragePath D:\VM_name

导入或导出 VMImport or export a VM

Import-VMExport-VM cmdlet 可导入和导出 VM。The Import-VM and Export-VM cmdlets import and export a VM. 下面显示了一些示例。The following shows a couple of examples. 有关详细信息,请查看 Import-VMExport-VM 参考文档。For more information, see the Import-VM and Export-VM reference documentation.

下面的示例展示了如何基于 VM 的配置文件导入 VM。The following example shows how to import a VM from its configuration file. VM 就地注册,因此不会复制其文件:The VM is registered in-place, so its files are not copied:

Import-VM -ComputerName Server1 -Name VM1 -Path 'C:\<vm export path>\2B91FEB3-F1E0-4FFF-B8BE-29CED892A95A.vmcx'

下面的示例将 VM 导出到 D 驱动器的根目录:The following example exports a VM to the root of the D drive:

Export-VM -ComputerName Server1 -Name VM1 -Path D:\

重命名 VMRename a VM

Rename-VM cmdlet 用于重命名 VM。The Rename-VM cmdlet is used to rename a VM. 有关详细信息,请查看 Rename-VM 参考文档。For detailed information, see the Rename-VM reference documentation.

下面的示例将 VM1 重命名为 VM2,并显示重命名后的虚拟机:The following example renames VM1 to VM2 and displays the renamed virtual machine:

Rename-VM -ComputerName Server1 -Name VM1 -NewName VM2

创建 VM 检查点Create a VM checkpoint

Checkpoint-VM cmdlet 用于为 VM 创建检查点。The Checkpoint-VM cmdlet is used to create a checkpoint for a VM. 有关详细信息,请查看 Checkpoint-VM 参考文档。For detailed information, see the Checkpoint-VM reference documentation.

下面的示例为名为 Test 的 VM 创建名为 BeforeInstallingUpdates 的检查点。The following example creates a checkpoint named BeforeInstallingUpdates for the VM named Test.

Checkpoint-VM -ComputerName Server1 -Name VM1 -SnapshotName BeforeInstallingUpdates

为 VM 创建 VHDCreate a VHD for a VM

New-VHD cmdlet 用于为 VM 创建新的 VHD。The New-VHD cmdlet is used to create a new VHD for a VM. 有关如何使用该 cmdlet 的详细信息,请查看 New-VHD 参考文档。For detailed information on how to use it, see the New-VHD reference documentation.

下面的示例采用 VHDX 格式创建一个大小为 10 GB 的动态虚拟硬盘。The following example creates a dynamic virtual hard disk in VHDX format that is 10 GB in size. 由于未指定类型,因此文件扩展名决定了格式,将使用默认类型“动态”。The file name extension determines the format and the default type of dynamic is used because no type is specified.

New-VHD -ComputerName Server1 -Name VM1 -Path c:\Base.vhdx -SizeBytes 10GB

向 VM 添加网络适配器Add a network adapter to a VM

Add-VMNetworkAdapter cmdlet 用于向 VM 添加虚拟网络适配器。The Add-VMNetworkAdapter cmdlet is used to add a virtual network adapter to a VM. 下面显示了一些示例。The following shows a couple of examples. 有关如何使用该 cmdlet 的详细信息,请查看 Add-VMNetworkAdapter 参考文档。For detailed information on how to use it, see the Add-VMNetworkAdapter reference documentation.

下面的示例将名为 Redmond NIC1 的虚拟网络适配器添加到名为 VM1 的虚拟机:The following example adds a virtual network adapter named Redmond NIC1 to a virtual machine named VM1:

Add-VMNetworkAdapter -ComputerName Server1 -VMName VM1 -Name "Redmond NIC1"

此示例将一个虚拟网络适配器添加到名为 VM1 的虚拟机,并将其连接到名为 Network 的虚拟交换机:This example adds a virtual network adapter to a virtual machine named VM1 and connects it to a virtual switch named Network:

Add-VMNetworkAdapter -ComputerName Server1 -VMName VM1 -SwitchName Network

为 VM 创建虚拟交换机Create a virtual switch for a VM

New-VMSwitch cmdlet 用于在 VM 主机上新建虚拟交换机。The New-VMSwitch cmdlet is used to new virtual switch on a VM host. 有关如何使用该 cmdlet 的详细信息,请查看 New-VMSwitch 参考文档。For detailed information on how to use it, see the New-VMSwitch reference documentation.

以下示例创建一个名为“QoS switch”的新交换机,该交换机绑定到名为 Wired Ethernet Connection 3 的网络适配器,并支持基于权重的最小带宽。The following example creates a new switch called "QoS switch", which binds to a network adapter called Wired Ethernet Connection 3 and supports weight-based minimum bandwidth.

New-VMSwitch "QoS Switch" -NetAdapterName "Wired Ethernet Connection 3" -MinimumBandwidthMode Weight

为 VM 设置内存Set memory for a VM

Set-VMMemory cmdlet 用于为 VM 配置内存。The Set-VMMemory cmdlet is used to configure the memory a VM. 有关如何使用该 cmdlet 的详细信息,请查看 Set-VMMemory 参考文档。For detailed information on how to use it, see the Set-VMMemory reference documentation.

以下示例在名为 VM1 的 VM 上启用动态内存,设置其最小内存、启动内存和最大内存、其内存优先级及其缓冲区。The following example enables dynamic memory on a VM named VM1, sets its minimum, startup, and maximum memory, its memory priority, and its buffer.

Set-VMMemory -ComputerName Server1 -Name VM1 -DynamicMemoryEnabled $true -MinimumBytes 64MB -StartupBytes 256MB -MaximumBytes 2GB -Priority 80 -Buffer 25

为 VM 设置虚拟处理器Set virtual processors for a VM

Set-VMProcessor cmdlet 用于为 VM 配置虚拟处理器。The Set-VMProcessor cmdlet is used to configure the virtual processors for a VM. 有关如何使用该 cmdlet 的详细信息,请查看 Set-VMProcessor 参考文档。For detailed information on how to use it, see the Set-VMProcessor reference documentation.

下面的示例配置名为 VM1 的 VM,该 VM 具有两个虚拟处理器,预留比例为 10%,限制为 75%,相对权重为 200。The following example configures a VM named VM1 with two virtual processors, a reserve of 10%, a limit of 75%, and a relative weight of 200.

Set-VMProcessor -ComputerName Server1 -Name VM1 -Count 2 -Reserve 10 -Maximum 75 -RelativeWeight 200

后续步骤Next steps

你还可以使用 Windows Admin Center 创建和管理 VM。You can also create and manage VMs using Windows Admin Center. 有关详细信息,请参阅 Windows Admin CenterFor more information, see Windows Admin Center.