官网链接:https://docs.microsoft.com/zh-cn/powershell/module/hyper-v/?view=win10-ps
环境: Windows 10 Pro 1809
启用 Hyper-V
查询 Hyper-V 功能是否启用
Get-WindowsOptionalFeature -Online -FeatureName "*-hyper-*" | Format-Table
返回结果如下
FeatureName State DisplayName Description RestartRequired----------- ----- ----------- ----------- --------------Microsoft-Hyper-V-All Enabled Hyper-V Provides services and management tools for creating and running virtual machines and their resources. PossibleMicrosoft-Hyper-V Enabled Hyper-V Platform Provides the services that you can use to create and manage virtual machines and their resources. PossibleMicrosoft-Hyper-V-Tools-All Enabled Hyper-V Management Tools Includes GUI and command-line tools for managing Hyper-V. PossibleMicrosoft-Hyper-V-Management-PowerShell Enabled Hyper-V Module for Windows PowerShell Includes Windows PowerShell cmdlets for managing Hyper-V. PossibleMicrosoft-Hyper-V-Hypervisor Enabled Hyper-V Hypervisor Provides the Hyper-V Hypervisor. PossibleMicrosoft-Hyper-V-Services Enabled Hyper-V Services Provides the services that you can use to create and manage virtual machines and their resources. PossibleMicrosoft-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
Enable-WindowsOptionalFeature -Online -FeatureName "Microsoft-Hyper-V-All"
虚拟交换机管理
Get-VMSwitch
Get-VMSwitch
Name SwitchType NetAdapterInterfaceDescription---- ---------- ------------------------------Default Switch Internal
默认内置了一个类型为内部(Internal)网络的虚拟交换机,名为 Default Switch。如果有局域网内非宿主机的其他机器的访问需求,则需要创建一个类型为外部网络的交换机。
New-VMSwitch
创建 External Network 的交换机
先 Get-NetAdapter 一下,找到你现在连接局域网的网卡的 NetAdapterInterfaceDescription。
Name InterfaceDescription ifIndex Status MacAddress LinkSpeed---- -------------------- ------- ------ ---------- ---------Ethernet 3 PANGP Virtual Ethernet Adapter #2 23 Disconnected 02-50-41-00-00-01 2 GbpsvEthernet (Default Switch) Hyper-V Virtual Ethernet Adapter 28 Up 00-15-5D-ED-14-55 10 GbpsEthernet Intel(R) Ethernet Connection (5) I219-LM 21 Up 8C-EC-4B-5A-F4-A8 1 GbpsWi-Fi Intel(R) Dual Band Wireless-AC 8265 16 Disconnected 74-E5-F9-F0-54-6F 0 bpsvEthernet (External network) Hyper-V Virtual Ethernet Adapter #2 5 Up 8C-EC-4B-5A-F4-A8 1 Gbps
然后就可以创建名为 External Switch 的交换机了
New-VMSwitch -Name "External Switch" -NetAdapterInterfaceDescription "Intel(R) Ethernet Connection (5) I219-LM"
虚拟机管理
创建和设置
New-VM
# 常用创建VM的完整命令$new_vm_name="new vm1"$base_path="E:\Hyper-V"$switch_name="External Switch"New-VM `-Name $new_vm_name `-Path "$base_path" `-Generation 1 `-MemoryStartupBytes 2GB `-NewVHDSizeBytes 20GB `-NewVHDPath "$base_path\$new_vm_name\Virtual Hard Disks\$new_vm_name.vhdx" `-SwitchName $switch_name
Set-VM
在 New-VM 的时候有些参数无法设置,只能再通过 Set-VM 设置
# 禁用自动生成检查点Set-VM -AutomaticCheckpointsEnabled 0 -Name $new_vm_name# 设置成静态内存Set-VM -StaticMemory -Name $new_vm_name# 设置自动启动, 默认值:StartIfRunning (Automatically start if it was runnning when the service stopped)# 可选值:Start(Always start this virtual machine automatically), NothingSet-VM -AutomaticStartAction Start -Name $new_vm_name# 设置CPU处理器Set-VMProcessor -Count 4 -VMName $new_vm_name# 设置启动 DVD DriveSet-VMDvdDrive -Path "E:\OS Images\CentOS-7-x86_64-DVD-2003.iso" -VMName $new_vm_name
在之前创建VM时的设置也可以修改。如关机之后,调小内存
Set-VM -MemoryStartupBytes 512MB -Name $new_vm_name
常规管理
Get-VM
# 默认方式查看虚拟机Get-VM# 用自定义的格式查看'$MemoryAssigned=@{Label = "Mem(MB)"; Expression = {$_.MemoryAssigned/1048576}}$MemoryDemand=@{Label = "Demand(MB)"; Expression = {$_.MemoryDemand/1048576}}$CPU=@{Label = "CPU"; Expression = {"{0}%" -f $_.CPUUsage}}$CPU_Count=@{Label = "CPUs"; Expression = {(Get-VMProcessor -VMName $_.Name).Count}}$Uptime=@{Label = "Uptime"Expression = {if ( ($_.Uptime.TotalDays -split "\.")[0] -ne 0 ) { "{0}d{1}h" -f $_.Uptime.Days,$_.Uptime.Hours }elseif ( ($_.Uptime.TotalHours -split "\.")[0] -ne 0 ) { "{0}h{1}m" -f $_.Uptime.Hours,$_.Uptime.Minutes }elseif ( ($_.Uptime.TotalMinutes -split "\.")[0] -ne 0 ) { "{0}m{1}s" -f $_.Uptime.Minutes,$_.Uptime.Seconds }else { "{0}s" -f $_.Uptime.Seconds }}}$AutomaticStartAction=@{Label = "AutoStartAction"; Expression = {$_.AutomaticStartAction}}$ParentCheckpointName=@{Label = "FromCheckpoint"; Expression = {$_.ParentCheckpointName}}Get-VM | Format-Table Name,State,$CPU_Count,$CPU,$MemoryAssigned,$MemoryDemand,$Uptime,Status,$ParentCheckpointName -AutoSize' | Out-File list_vm.ps1.\list_vm.ps1
Start-VM
Start-VM -Name $new_vm_name# 批量启动Get-VM | ? { $_.Name -like "etcd-*" } | Start-VM -Confirm
Restart-VM
Restart-VM -Name $new_vm_name# 一键重启所有名字以 etcd-开头的 VMGet-VM | ? { $_.Name -like "etcd-*" } | Restart-VM -Confirm
Stop-VM
Stop-VM -Name $new_vm_name# 一键关机,适用于宿主机需要关机的时候Get-VM | ForEach-Object { $_ | Stop-VM -Confirm }
快照和恢复
Checkpoint-VM
# 指定快照名存档Checkpoint-VM -Name $new_vm_name -SnapshotName BeforeInstallingUpdates# 过程有点长,可以后台,然后用 Get-VM 查看Checkpoint-VM -Name $new_vm_name -SnapshotName BeforeInstallingUpdates -AsJob# 批量对以 etcd- 开头的虚拟机进行拍快照Get-VM | ? { $_.Name -like "etcd-?" } | Checkpoint-VM -SnapshotName 'OSInstalled' -AsJob# 查询在 Server1 (非本机) 上运行的 vm 然后对其 checkpointGet-VM $new_vm_name -ComputerName Server1 | Checkpoint-VM
Get-VMSnapshot
Get-VMSnapshot -VMName $new_vm_nameGet-VM | ? { $_.Name -like "etcd-?" } | Get-VMSnapshot
Remove-VMSnapshot
Remove-VMSnapshot -VMName $new_vm_name -Name 'OSInstalled'# 删除该虚拟机最近的一个快照Get-VMSnapshot -VMName $new_vm_name | Sort CreationTime | Select -Last 1 | Remove-VMSnapshot
Restore-VMSnapshot
# 从 Snapshot 恢复,会占据当前 powershell 的终端,并在上方出现进度条,不可以做其他操作Restore-VMSnapshot -Name 'OSInstalled' -VMName $new_vm_name# 过程有点长,可以后台,然后用 Get-VM 查看状态,但是系统内存已经快用完的情况下,不建议后台恢复多台Restore-VMSnapshot -Name 'OSInstalled' -VMName $new_vm_name -AsJob# 基于虚拟机命名规则,批量恢复到 OSInstalled 这个快照Get-VM | ? { $_.Name -like "k8s-master-? "} | Restore-VMSnapshot -Name 'OSInstalled'# Applies the most recent checkpoint on all virtual machines with no confirmation prompts.Get-VM | Foreach-Object { $_ | Get-VMSnapshot | Sort CreationTime | Select -Last 1 | Restore-VMSnapshot -Confirm:$false }
删除
Remove-VM
Remove-VM -Name $new_vm_name# Clean the remaining vhdx fileRemove-Item "$base_path\$new_vm_name\Virtual Hard Disks\$new_vm_name.vhdx"
