官网链接:https://docs.microsoft.com/zh-cn/powershell/module/hyper-v/?view=win10-ps

环境: Windows 10 Pro 1809

启用 Hyper-V

查询 Hyper-V 功能是否启用

  1. Get-WindowsOptionalFeature -Online -FeatureName "*-hyper-*" | Format-Table

返回结果如下

  1. FeatureName State DisplayName Description RestartRequire
  2. d
  3. ----------- ----- ----------- ----------- --------------
  4. Microsoft-Hyper-V-All Enabled Hyper-V Provides services and management tools for creating and running virtual machines and their resources. Possible
  5. Microsoft-Hyper-V Enabled Hyper-V Platform Provides the services that you can use to create and manage virtual machines and their resources. Possible
  6. Microsoft-Hyper-V-Tools-All Enabled Hyper-V Management Tools Includes GUI and command-line tools for managing Hyper-V. Possible
  7. Microsoft-Hyper-V-Management-PowerShell Enabled Hyper-V Module for Windows PowerShell Includes Windows PowerShell cmdlets for managing Hyper-V. Possible
  8. Microsoft-Hyper-V-Hypervisor Enabled Hyper-V Hypervisor Provides the Hyper-V Hypervisor. Possible
  9. Microsoft-Hyper-V-Services Enabled Hyper-V Services Provides the services that you can use to create and manage virtual machines and their resources. Possible
  10. Microsoft-Hyper-V-Management-Clients Enabled Hyper-V GUI Management Tools Includes the Hyper-V Manager snap-in and Virtual Machine Connection tool. Possible

启用 Hyper-V

  1. Enable-WindowsOptionalFeature -Online -FeatureName "Microsoft-Hyper-V-All"

虚拟交换机管理

Get-VMSwitch

  1. Get-VMSwitch
  1. Name SwitchType NetAdapterInterfaceDescription
  2. ---- ---------- ------------------------------
  3. Default Switch Internal

默认内置了一个类型为内部(Internal)网络的虚拟交换机,名为 Default Switch。如果有局域网内非宿主机的其他机器的访问需求,则需要创建一个类型为外部网络的交换机。

New-VMSwitch

创建 External Network 的交换机

Get-NetAdapter 一下,找到你现在连接局域网的网卡的 NetAdapterInterfaceDescription。

  1. Name InterfaceDescription ifIndex Status MacAddress LinkSpeed
  2. ---- -------------------- ------- ------ ---------- ---------
  3. Ethernet 3 PANGP Virtual Ethernet Adapter #2 23 Disconnected 02-50-41-00-00-01 2 Gbps
  4. vEthernet (Default Switch) Hyper-V Virtual Ethernet Adapter 28 Up 00-15-5D-ED-14-55 10 Gbps
  5. Ethernet Intel(R) Ethernet Connection (5) I219-LM 21 Up 8C-EC-4B-5A-F4-A8 1 Gbps
  6. Wi-Fi Intel(R) Dual Band Wireless-AC 8265 16 Disconnected 74-E5-F9-F0-54-6F 0 bps
  7. vEthernet (External network) Hyper-V Virtual Ethernet Adapter #2 5 Up 8C-EC-4B-5A-F4-A8 1 Gbps

然后就可以创建名为 External Switch 的交换机了

  1. New-VMSwitch -Name "External Switch" -NetAdapterInterfaceDescription "Intel(R) Ethernet Connection (5) I219-LM"

虚拟机管理

创建和设置

New-VM

  1. # 常用创建VM的完整命令
  2. $new_vm_name="new vm1"
  3. $base_path="E:\Hyper-V"
  4. $switch_name="External Switch"
  5. New-VM `
  6. -Name $new_vm_name `
  7. -Path "$base_path" `
  8. -Generation 1 `
  9. -MemoryStartupBytes 2GB `
  10. -NewVHDSizeBytes 20GB `
  11. -NewVHDPath "$base_path\$new_vm_name\Virtual Hard Disks\$new_vm_name.vhdx" `
  12. -SwitchName $switch_name

Set-VM

在 New-VM 的时候有些参数无法设置,只能再通过 Set-VM 设置

  1. # 禁用自动生成检查点
  2. Set-VM -AutomaticCheckpointsEnabled 0 -Name $new_vm_name
  3. # 设置成静态内存
  4. Set-VM -StaticMemory -Name $new_vm_name
  5. # 设置自动启动, 默认值:StartIfRunning (Automatically start if it was runnning when the service stopped)
  6. # 可选值:Start(Always start this virtual machine automatically), Nothing
  7. Set-VM -AutomaticStartAction Start -Name $new_vm_name
  8. # 设置CPU处理器
  9. Set-VMProcessor -Count 4 -VMName $new_vm_name
  10. # 设置启动 DVD Drive
  11. Set-VMDvdDrive -Path "E:\OS Images\CentOS-7-x86_64-DVD-2003.iso" -VMName $new_vm_name

在之前创建VM时的设置也可以修改。如关机之后,调小内存

  1. Set-VM -MemoryStartupBytes 512MB -Name $new_vm_name

常规管理

Get-VM

  1. # 默认方式查看虚拟机
  2. Get-VM
  3. # 用自定义的格式查看
  4. '
  5. $MemoryAssigned=@{Label = "Mem(MB)"; Expression = {$_.MemoryAssigned/1048576}}
  6. $MemoryDemand=@{Label = "Demand(MB)"; Expression = {$_.MemoryDemand/1048576}}
  7. $CPU=@{Label = "CPU"; Expression = {"{0}%" -f $_.CPUUsage}}
  8. $CPU_Count=@{Label = "CPUs"; Expression = {(Get-VMProcessor -VMName $_.Name).Count}}
  9. $Uptime=@{
  10. Label = "Uptime"
  11. Expression = {
  12. if ( ($_.Uptime.TotalDays -split "\.")[0] -ne 0 ) { "{0}d{1}h" -f $_.Uptime.Days,$_.Uptime.Hours }
  13. elseif ( ($_.Uptime.TotalHours -split "\.")[0] -ne 0 ) { "{0}h{1}m" -f $_.Uptime.Hours,$_.Uptime.Minutes }
  14. elseif ( ($_.Uptime.TotalMinutes -split "\.")[0] -ne 0 ) { "{0}m{1}s" -f $_.Uptime.Minutes,$_.Uptime.Seconds }
  15. else { "{0}s" -f $_.Uptime.Seconds }
  16. }
  17. }
  18. $AutomaticStartAction=@{Label = "AutoStartAction"; Expression = {$_.AutomaticStartAction}}
  19. $ParentCheckpointName=@{Label = "FromCheckpoint"; Expression = {$_.ParentCheckpointName}}
  20. Get-VM | Format-Table Name,State,$CPU_Count,$CPU,$MemoryAssigned,$MemoryDemand,$Uptime,Status,$ParentCheckpointName -AutoSize
  21. ' | Out-File list_vm.ps1
  22. .\list_vm.ps1

Start-VM

  1. Start-VM -Name $new_vm_name
  2. # 批量启动
  3. Get-VM | ? { $_.Name -like "etcd-*" } | Start-VM -Confirm

Restart-VM

  1. Restart-VM -Name $new_vm_name
  2. # 一键重启所有名字以 etcd-开头的 VM
  3. Get-VM | ? { $_.Name -like "etcd-*" } | Restart-VM -Confirm

Stop-VM

  1. Stop-VM -Name $new_vm_name
  2. # 一键关机,适用于宿主机需要关机的时候
  3. Get-VM | ForEach-Object { $_ | Stop-VM -Confirm }

快照和恢复

Checkpoint-VM

  1. # 指定快照名存档
  2. Checkpoint-VM -Name $new_vm_name -SnapshotName BeforeInstallingUpdates
  3. # 过程有点长,可以后台,然后用 Get-VM 查看
  4. Checkpoint-VM -Name $new_vm_name -SnapshotName BeforeInstallingUpdates -AsJob
  5. # 批量对以 etcd- 开头的虚拟机进行拍快照
  6. Get-VM | ? { $_.Name -like "etcd-?" } | Checkpoint-VM -SnapshotName 'OSInstalled' -AsJob
  7. # 查询在 Server1 (非本机) 上运行的 vm 然后对其 checkpoint
  8. Get-VM $new_vm_name -ComputerName Server1 | Checkpoint-VM

Get-VMSnapshot

  1. Get-VMSnapshot -VMName $new_vm_name
  2. Get-VM | ? { $_.Name -like "etcd-?" } | Get-VMSnapshot

Remove-VMSnapshot

  1. Remove-VMSnapshot -VMName $new_vm_name -Name 'OSInstalled'
  2. # 删除该虚拟机最近的一个快照
  3. Get-VMSnapshot -VMName $new_vm_name | Sort CreationTime | Select -Last 1 | Remove-VMSnapshot

Restore-VMSnapshot

  1. # 从 Snapshot 恢复,会占据当前 powershell 的终端,并在上方出现进度条,不可以做其他操作
  2. Restore-VMSnapshot -Name 'OSInstalled' -VMName $new_vm_name
  3. # 过程有点长,可以后台,然后用 Get-VM 查看状态,但是系统内存已经快用完的情况下,不建议后台恢复多台
  4. Restore-VMSnapshot -Name 'OSInstalled' -VMName $new_vm_name -AsJob
  5. # 基于虚拟机命名规则,批量恢复到 OSInstalled 这个快照
  6. Get-VM | ? { $_.Name -like "k8s-master-? "} | Restore-VMSnapshot -Name 'OSInstalled'
  7. # Applies the most recent checkpoint on all virtual machines with no confirmation prompts.
  8. Get-VM | Foreach-Object { $_ | Get-VMSnapshot | Sort CreationTime | Select -Last 1 | Restore-VMSnapshot -Confirm:$false }

删除

Remove-VM

  1. Remove-VM -Name $new_vm_name
  2. # Clean the remaining vhdx file
  3. Remove-Item "$base_path\$new_vm_name\Virtual Hard Disks\$new_vm_name.vhdx"