powershell中使用hyper-v 命令代码

发布时间 2023-12-24 22:49:36作者: suv789

PowerShell 中使用 Hyper-V 挂载、分离、创建和格式化 VHD 的代码:
挂载 VHD 文件:
powershell
# 挂载 VHD 文件
Mount-VHD -Path "C:\test\disk.vhd"
分离 VHD 文件:
powershell
# 分离 VHD 文件
Dismount-VHD -Path "C:\test\disk.vhd"
创建 VHD 文件:
powershell
# 创建 10GB 的 VHD 文件,并存储在 C:\test 目录下
New-VHD -Path "C:\test\disk.vhd" -SizeBytes 10GB
格式化 VHD 文件:
powershell
# 格式化 VHD 文件为 NTFS 文件系统
Format-Volume -DriveLetter E -FileSystem NTFS -Confirm:$false
查看已挂载的 VHD 文件信息:
powershell
Get-VHD -Path "C:\test\disk.vhd"
修改 VHD 文件的属性:
powershell
Set-VHD -Path "C:\test\disk.vhd" -ParentPath "C:\path\to\parent.vhdx"
复制 VHD 文件:
powershell
Copy-Item "C:\source\disk.vhd" -Destination "D:\destination\" 
移动 VHD 文件:
powershell
Move-Item "C:\source\disk.vhd" -Destination "D:\destination\" 
获取已创建的虚拟硬盘列表:
powershell
Get-VMHardDiskDrive -VMName "VM1"
创建动态扩展的 VHDX 文件:
powershell
New-VHD -Path "C:\test\disk.vhdx" -Dynamic -SizeBytes 50GB
将 VHDX 文件转换为 VHD 文件:
powershell
Convert-VHD -Path "C:\test\disk.vhdx" -DestinationPath "C:\test\disk.vhd" -VHDType Fixed
启用和禁用 Hyper-V 的 VHD 文件压缩:
powershell
Enable-VMIntegrationService -VMName "VM1" -IntegrationServiceName "Guest Service Interface" -Passthru | Set-VMCompressPhysicalMemory:$true
Disable-VMIntegrationService -VMName "VM1" -IntegrationServiceName "Guest Service Interface" -Passthru | Set-VMCompressPhysicalMemory:$false
获取 VHD 文件所属的虚拟机名称:
powershell
(Get-Item "C:\test\disk.vhd").Parent.Parent.Name
获取虚拟机中所有的虚拟硬盘:
powershell
Get-VM -Name "VM1" | Get-VMHardDiskDrive
设置虚拟硬盘的最大 IOPS 限制:
powershell
Set-VMHardDiskDrive -VMName "VM1" -ControllerType SCSI -ControllerNumber 0 -ControllerLocation 0 -Path "C:\test\disk.vhdx" -MaxIOPS 500
将虚拟硬盘连接到虚拟机:
powershell
Add-VMHardDiskDrive -VMName "VM1" -ControllerType SCSI -ControllerNumber 0 -ControllerLocation 0 -Path "C:\test\disk.vhdx"
从虚拟机中移除虚拟硬盘:
powershell
Remove-VMHardDiskDrive -VMName "VM1" -ControllerType SCSI -ControllerNumber 0 -ControllerLocation 0
从虚拟硬盘中删除指定的快照文件:
powershell
Remove-VMSnapshot -Name "Snapshot1" -VHDPath "C:\test\disk.vhdx"
获取虚拟机中的所有快照信息:
powershell
Get-VMSnapshot -VMName "VM1"
创建虚拟硬盘的快照:
powershell
Checkpoint-VM -Name "VM1" -SnapshotName "Snapshot1" -Path "C:\test\disk.vhdx"
恢复虚拟硬盘到指定的快照状态:
powershell
Restore-VMSnapshot -Name "Snapshot1" -VHDPath "C:\test\disk.vhdx" -Confirm:$false
获取虚拟机中的存储控制器信息:
powershell
Get-VMStorageController -VMName "VM1"
修改虚拟硬盘的存储控制器位置:
powershell
Set-VMHardDiskDrive -VMName "VM1" -ControllerType SCSI -ControllerNumber 1 -ControllerLocation 0 -Path "C:\test\disk.vhdx"
将虚拟硬盘从一个虚拟机移动到另一个虚拟机:
powershell
$sourceVM = "SourceVM"
$destVM = "DestVM"
$vhdxPath = "C:\path\to\disk.vhdx"

Get-VM $sourceVM | Get-VMHardDiskDrive | Where-Object { $_.Path -eq $vhdxPath } | Move-VMStorage -DestinationStoragePath (Get-VM $destVM).ConfigurationLocation
设置虚拟硬盘的读写缓存策略:
powershell
Set-VMHardDiskDrive -VMName "VM1" -ControllerType SCSI -ControllerNumber 0 -ControllerLocation 0 -Path "C:\test\disk.vhdx" -ReadBuffering None -WriteBuffering WriteThrough
获取虚拟硬盘的详细信息:
powershell
Get-VHD -Path "C:\test\disk.vhdx"
合并虚拟硬盘的快照:
powershell
Merge-VHD -Path "C:\test\disk.vhdx"
将虚拟硬盘设置为只读模式:
powershell
Set-VHD -Path "C:\test\disk.vhdx" -ReadOnly
扩展虚拟硬盘的容量:
powershell
Resize-VHD -Path "C:\test\disk.vhdx" -SizeBytes 50GB
创建虚拟硬盘的副本:
powershell
New-VHD -Path "C:\test\disk_copy.vhdx" -ParentPath "C:\test\disk.vhdx" -Differencing
获取虚拟机中所有的动态内存:
powershell
Get-VM -Name "VM1" | Get-VMDynamicMemory
挂载虚拟硬盘:
powershell
Mount-VHD -Path "C:\test\disk.vhdx"
卸载虚拟硬盘:
powershell
Dismount-VHD -Path "C:\test\disk.vhdx"
创建固定大小的虚拟硬盘:
powershell
New-VHD -Path "C:\test\disk.vhdx" -Fixed -SizeBytes 100GB
获取虚拟硬盘的连接状态:
powershell
Get-VMHardDiskDrive -VMName "VM1" | Select-Object Path, ControllerType, ControllerNumber, ControllerLocation, IsConnected
移除虚拟硬盘的连接:
powershell
Disconnect-VMHardDiskDrive -VMName "VM1" -ControllerType IDE -ControllerNumber 0 -ControllerLocation 1
添加虚拟硬盘的连接:
powershell
Connect-VMHardDiskDrive -VMName "VM1" -ControllerType SCSI -ControllerNumber 0 -ControllerLocation 2 -Path "C:\test\disk.vhdx"
设置虚拟硬盘的启动顺序:
powershell
Set-VMFirmware -VMName "VM1" -FirstBootDevice (Get-VMHardDiskDrive -VMName "VM1" | Where-Object { $_.Path -eq "C:\test\disk.vhdx" })
复制虚拟硬盘:
powershell
Copy-Item "C:\Path\to\SourceDisk.vhdx" -Destination "C:\Path\to\DestinationDisk.vhdx"
导入虚拟硬盘:
powershell
Import-VM -Path "C:\Path\to\VirtualMachineFolder"
导出虚拟硬盘:
powershell
Export-VM -Name "VMName" -Path "C:\ExportFolderPath"
检查虚拟硬盘的一致性:
powershell
Test-VHD -Path "C:\Path\to\Disk.vhdx"
将虚拟硬盘设置为动态大小:
powershell
Resize-VHD -Path "C:\test\disk.vhdx" -ToMinimumSize
检查虚拟硬盘的信息:
powershell
Get-VHD -Path "C:\test\disk.vhdx"
修改虚拟硬盘的类型:
powershell
Convert-VHD -Path "C:\test\disk.vhd" -DestinationPath "C:\test\convertedDisk.vhdx"
获取虚拟机的所有虚拟硬盘:
powershell
Get-VMHardDiskDrive -VMName "VM1"
获取虚拟硬盘的容量信息:
powershell
Get-VHD -Path "C:\test\disk.vhdx" | Select-Object Path, VhdFormat, Size, MaximumSize
修改虚拟硬盘的大小:
powershell
Resize-VHD -Path "C:\test\disk.vhdx" -SizeBytes 200GB
将虚拟硬盘设置为只读模式:
powershell
Set-VHD -Path "C:\test\disk.vhdx" -ReadOnly
将虚拟硬盘设置为读写模式:
powershell
Set-VHD -Path "C:\test\disk.vhdx" -NotReadOnly
将虚拟硬盘挂载到主机文件系统:
powershell
Mount-VHD -Path "C:\test\disk.vhdx"
卸载已经挂载到主机文件系统的虚拟硬盘:
powershell
Dismount-VHD -Path "C:\test\disk.vhdx"
检查虚拟硬盘的状态:
powershell
Test-VHD -Path "C:\test\disk.vhdx" | Select-Object Path, VhdFormat, VhdType, FileSize, Size
创建一个新的虚拟硬盘:
powershell
New-VHD -Path "C:\test\newDisk.vhdx" -SizeBytes 100GB
设置虚拟硬盘的扩展属性(用于生成检查点):
powershell
Set-VHD -Path "C:\test\disk.vhdx" -VHDType "Differencing"
导入虚拟硬盘快照:
powershell
Apply-VHDSnapshot -Path "C:\test\disk.vhdx" -SnapshotFile "C:\test\snapshot.avhdx"
创建虚拟硬盘快照:
powershell
Checkpoint-VM -Name "VMName" -SnapshotName "SnapshotName"
删除虚拟硬盘:
powershell
Remove-VHD -Path "C:\test\disk.vhdx"
复制虚拟硬盘到指定路径:
powershell
Copy-VHD -Path "C:\test\disk.vhdx" -DestinationPath "D:\backup\disk.vhdx"
检查虚拟机的连接状态:
powershell
Get-VMIntegrationService -VMName "VM1" | Select-Object Name, Enabled
启动虚拟机:
powershell
Start-VM -Name "VM1"
停止虚拟机:
powershell
Stop-VM -Name "VM1"
暂停虚拟机:
powershell
Suspend-VM -Name "VM1"
恢复暂停的虚拟机:
powershell
Resume-VM -Name "VM1"
重启虚拟机:
powershell
Restart-VM -Name "VM1"
查看所有虚拟机的状态:
powershell
Get-VM | Select-Object Name, State
查看虚拟机的配置信息:
powershell
Get-VM -Name "VM1" | Select-Object *
设置虚拟机的内存大小:
powershell
Set-VMMemory -VMName "VM1" -StartupBytes 4GB -MinimumBytes 2GB -MaximumBytes 8GB
设置虚拟机的 CPU 核心数量:
powershell
Set-VMProcessor -VMName "VM1" -Count 4
设置虚拟机的网络适配器:
powershell
Set-VMNetworkAdapter -VMName "VM1" -Name "Network Adapter" -SwitchName "Virtual Switch"
更改虚拟机的名称:
powershell
Rename-VM -VMName "VM1" -NewName "NewVMName"
移动虚拟机到新的位置:
powershell
Move-VM -VMName "VM1" -DestinationHost "NewHost" -DestinationStoragePath "E:\Virtual Machines"
导出虚拟机到指定路径:
powershell
Export-VM -Name "VM1" -Path "D:\ExportedVMs\VM1"
设置虚拟机的动态内存分配:
powershell
Set-VMDynamicMemory -VMName "VM1" -MemoryMinimumBytes 1GB -MemoryMaximumBytes 4GB
挂载虚拟硬盘到虚拟机:
powershell
Add-VMHardDiskDrive -VMName "VM1" -Path "C:\VMDisks\AdditionalDisk.vhdx"
移除虚拟机的网络适配器:
powershell
Remove-VMNetworkAdapter -VMName "VM1" -Name "Network Adapter"
查看虚拟交换机的信息:
powershell
Get-VMSwitch
删除虚拟机:
powershell
Remove-VM -Name "VM1" -Force
启用虚拟机的自动快照功能:
powershell
Enable-VMSnapshot -VMName "VM1" -SnapshotFilePath "D:\Snapshots\VM1"
创建虚拟机的快照:
powershell
Checkpoint-VM -Name "VM1" -SnapshotFilePath "D:\Snapshots\VM1\Checkpoint1"
恢复虚拟机到指定快照点:
powershell
Restore-VMSnapshot -Name "VM1" -SnapshotFilePath "D:\Snapshots\VM1\Checkpoint1"
查看虚拟机的事件日志:
powershell
Get-WinEvent -LogName "Microsoft-Windows-Hyper-V-VMMS/Admin" | Where-Object {$_.Message -like "*VM1*"}
启动虚拟机:
powershell
Start-VM -Name "VM1"
关闭虚拟机:
powershell
Stop-VM -Name "VM1"
暂停虚拟机:
powershell
Suspend-VM -Name "VM1"
查看虚拟机的状态信息:
powershell
Get-VM -Name "VM1" | Select-Object VMId, Name, State
修改虚拟机的配置:
powershell
Set-VM -Name "VM1" -ProcessorCount 4 -MemoryStartupBytes 4GB
导入虚拟机配置:
powershell
Import-VM -Path "D:\ExportedVMs\VM1" -Copy -GenerateNewId
检查虚拟机的兼容性:
powershell
Test-VMCompatibility -VMName "VM1"
导出虚拟机配置:
powershell
Export-VM -Name "VM1" -Path "D:\ExportedVMs\VM1" -Copy
修改虚拟机的网络适配器设置:
powershell
Set-VMNetworkAdapter -VMName "VM1" -Name "Network Adapter" -MacAddressSpoofing On
查看虚拟机的详细信息:
powershell
Get-VM -Name "VM1" | Format-List *
设置虚拟机的启动顺序:
powershell
Set-VMFirmware -VMName "VM1" -FirstBootDevice "CD"
重命名虚拟机:
powershell
Rename-VM -Name "VM1" -NewName "VM2"
移动虚拟机到另一个存储位置:
powershell
Move-VMStorage -VMName "VM1" -DestinationStoragePath "D:\Virtual Machines\VM1"
挂载虚拟机的 VHD 文件:
powershell
Mount-VHD -Path "D:\Virtual Machines\VM1\Disk1.vhd"
卸载虚拟机的 VHD 文件:
powershell
Dismount-VHD -Path "D:\Virtual Machines\VM1\Disk1.vhd"
创建新的虚拟机网络:
powershell
New-VMSwitch -Name "InternalSwitch" -SwitchType Internal
修改虚拟机的启动顺序:
powershell
Set-VMFirmware -VMName "VM1" -FirstBootDevice "CD"
启动虚拟机并连接到虚拟机的控制台:
powershell
Start-VM -Name "VM1"
Connect-VM -VMName "VM1"
设置虚拟机的 Generation:
powershell
Set-VM -Name "VM1" -Generation 2
查看虚拟机的网络适配器信息:
powershell
Get-VMNetworkAdapter -VMName "VM1"
修改虚拟机的内部网络适配器连接:
powershell
Set-VMNetworkAdapter -VMName "VM1" -Name "Internal Network" -SwitchName "InternalSwitch"
添加虚拟机的硬盘:
powershell
Add-VMHardDiskDrive -VMName "VM1" -ControllerType IDE -ControllerNumber 0 -ControllerLocation 1 -Path "D:\Virtual Machines\VM1\Disk2.vhd"
删除虚拟机的网络适配器:
powershell
Remove-VMNetworkAdapter -VMName "VM1" -Name "Network Adapter"
设置虚拟机的内存大小:
powershell
Set-VMMemory -VMName "VM1" -DynamicMemoryEnabled $true -MinimumBytes 512MB -MaximumBytes 2GB
修改虚拟机的处理器个数:
powershell
Set-VMProcessor -VMName "VM1" -Count 2
设置虚拟机的快照:
powershell
Checkpoint-VM -Name "VM1" -SnapshotName "BeforeUpdate"
导入虚拟机配置:
powershell
Import-VM -Path "D:\ExportedVMs\VM1" -Copy
设置虚拟机的启动延迟时间:
powershell
Set-VM -Name "VM1" -AutomaticStartAction DelayStart -AutomaticStartDelay 120
导出虚拟机配置:
powershell
Export-VM -Name "VM1" -Path "D:\ExportedVMs\VM1"
修改虚拟机的网络适配器 MAC 地址:
powershell
Set-VMNetworkAdapter -VMName "VM1" -StaticMacAddress "00:15:5D:12:34:56"
设置虚拟机的自动快照:
powershell
Set-VM -Name "VM1" -AutomaticCheckpointsEnabled $true
设置虚拟机的自动关机计划:
powershell
Set-VM -Name "VM1" -AutomaticStopAction ShutDown -AutomaticStopActionDelay 180
修改虚拟机的启动 RAM 内存分配:
powershell
Set-VMMemory -VMName "VM1" -StartupBytes 1GB
启用虚拟机的动态内存平衡:
powershell
Enable-VMMemoryBalancing -VMName "VM1"
设置虚拟机的 CPU 限制:
powershell
Set-VMProcessor -VMName "VM1" -MaximumCount 4
修改虚拟机的启动顺序:
powershell
Set-VMFirmware -VMName "VM1" -FirstBootDevice CD
设置虚拟机的自动化任务:
powershell
Register-ScheduledTask -TaskName "BackupVM1" -Trigger (New-ScheduledTaskTrigger -At 2am -Daily) -Action (New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "C:\Scripts\Backup-VM.ps1")
修改虚拟机的 Generation 版本:
powershell
Set-VM -Name "VM1" -Generation 2
启用虚拟机的迁移功能:
powershell
Enable-VMMigration -VMName "VM1"
设置虚拟机的启动顺序:
powershell
Set-VMFirmware -VMName "VM1" -FirstBootDevice CD
设置虚拟机的网络适配器 NAT 模式:
powershell
Set-VMNetworkAdapter -VMName "VM1" -Isolated -NATSubnetAddress "192.168.1.0/24"
修改虚拟机的电源管理策略:
powershell
Set-VMPowerPolicy -VMName "VM1" -AutomaticStartAction Resume -AutomaticStartDelay 60
设置虚拟机的硬盘容量:
powershell
Set-VHD -Path "D:\VirtualDisks\Disk1.vhdx" -SizeBytes 100GB
设置虚拟机的启动顺序:
powershell
Set-VMFirmware -VMName "VM1" -FirstBootDevice CD
设置虚拟机的网络适配器 NAT 模式:
powershell
Set-VMNetworkAdapter -VMName "VM1" -Isolated -NATSubnetAddress "192.168.1.0/24"
修改虚拟机的电源管理策略:
powershell
Set-VMPowerPolicy -VMName "VM1" -AutomaticStartAction Resume -AutomaticStartDelay 60
设置虚拟机的硬盘容量:
powershell
Set-VHD -Path "D:\VirtualDisks\Disk1.vhdx" -SizeBytes 100GB
导出虚拟机配置:
powershell
Export-VM -Name "VM1" -Path "D:\Exports\VM1"
导入虚拟机配置:
powershell
Import-VM -Path "D:\Exports\VM1"
克隆虚拟机:
powershell
Checkpoint-VM -Name "VM1" -SnapshotName "Snapshot1"
设置虚拟机的高可用性:
powershell
Set-ClusterGroup -Name "VM1" -AntiAffinityClass 1
设置虚拟机的计算资源限制:
powershell
Set-VMProcessor -VMName "VM1" -MaximumCount 4 -RelativeWeight 100
设置虚拟机的内存分配:
powershell
Set-VMMemory -VMName "VM1" -DynamicMemoryEnabled $true -MinimumBytes 2GB -MaximumBytes 8GB
启动虚拟机:
powershell
Start-VM -Name "VM1"
关闭虚拟机:
powershell
Stop-VM -Name "VM1" -Force
设置虚拟机的启动顺序:
powershell
Set-VMFirmware -VMName "VM1" -FirstBootDevice CD
设置虚拟机的网络适配器 NAT 模式:
powershell
Set-VMNetworkAdapter -VMName "VM1" -Isolated -NATSubnetAddress "192.168.1.0/24"
修改虚拟机的电源管理策略:
powershell
Set-VMPowerPolicy -VMName "VM1" -AutomaticStartAction Resume -AutomaticStartDelay 60
设置虚拟机的硬盘容量:
powershell
Set-VHD -Path "D:\VirtualDisks\Disk1.vhdx" -SizeBytes 100GB
设置虚拟机的计算资源限制:
powershell
Set-VMProcessor -VMName "VM1" -MaximumCount 4 -RelativeWeight 100
设置虚拟机的内存分配:
powershell
Set-VMMemory -VMName "VM1" -DynamicMemoryEnabled $true -MinimumBytes 2GB -MaximumBytes 8GB
启动虚拟机:
powershell
Start-VM -Name "VM1"
关闭虚拟机:
powershell
Stop-VM -Name "VM1" -Force
设置虚拟机的启动顺序:
powershell
Set-VMFirmware -VMName "VM1" -FirstBootDevice CD
设置虚拟机的网络适配器 NAT 模式:
powershell
Set-VMNetworkAdapter -VMName "VM1" -Isolated -NATSubnetAddress "192.168.1.0/24"
修改虚拟机的电源管理策略:
powershell
Set-VMPowerPolicy -VMName "VM1" -AutomaticStartAction Resume -AutomaticStartDelay 60
设置虚拟机的硬盘容量:
powershell
Set-VHD -Path "D:\VirtualDisks\Disk1.vhdx" -SizeBytes 100GB
设置虚拟机的计算资源限制:
powershell
Set-VMProcessor -VMName "VM1" -MaximumCount 4 -RelativeWeight 100
这个命令可以设置虚拟机的处理器数量和相对权重,以限制虚拟机对主机处理器的使用。

设置虚拟机的内存分配:
powershell
Set-VMMemory -VMName "VM1" -DynamicMemoryEnabled $true -MinimumBytes 2GB -MaximumBytes 8GB
这个命令可以设置虚拟机的内存分配方式为动态内存,并指定最小和最大内存大小。

启动虚拟机:
powershell
Start-VM -Name "VM1"
这个命令可以启动指定名称的虚拟机。

关闭虚拟机:
powershell
Stop-VM -Name "VM1" -Force
这个命令可以关闭指定名称的虚拟机,使用 -Force 参数可以强制关闭虚拟机。
设置虚拟机的启动顺序:
powershell
Set-VMFirmware -VMName "VM1" -FirstBootDevice CD
设置虚拟机的网络适配器 NAT 模式:
powershell
Set-VMNetworkAdapter -VMName "VM1" -Isolated -NATSubnetAddress "192.168.1.0/24"
修改虚拟机的电源管理策略:
powershell
Set-VMPowerPolicy -VMName "VM1" -AutomaticStartAction Resume -AutomaticStartDelay 60
设置虚拟机的硬盘容量:
powershell
Set-VHD -Path "D:\VirtualDisks\Disk1.vhdx" -SizeBytes 100GB
复制虚拟机配置:
powershell
Copy-VM -Name "VM1" -DestinationHost "HVServer2" -DestinationStoragePath "D:\VirtualMachines"
这个命令可以将指定名称的虚拟机复制到目标主机和存储路径。

导出虚拟机配置:
powershell
Export-VM -Name "VM1" -Path "D:\VMExports\VM1"
这个命令可以导出指定名称的虚拟机配置文件到指定路径。

导入虚拟机配置:
powershell
Import-VM -Path "D:\VMExports\VM1" -Copy -GenerateNewId
这个命令可以导入指定路径的虚拟机配置文件,并复制虚拟硬件和生成新的唯一 ID。

移动虚拟机配置:
powershell
Move-VM -Name "VM1" -DestinationHost "HVServer2" -DestinationStoragePath "D:\VirtualMachines"
这个命令可以将指定名称的虚拟机移动到目标主机和存储路径。
暂停虚拟机:
powershell
Suspend-VM -Name "VM1"
这个命令可以暂停指定名称的虚拟机,将其状态保存到磁盘上。

恢复虚拟机:
powershell
Resume-VM -Name "VM1"
这个命令可以恢复暂停状态的虚拟机,将其从磁盘上加载到内存中。

修改虚拟机的网络适配器配置:
powershell
Get-VMNetworkAdapter -VMName "VM1" | Set-VMNetworkAdapter -StaticMacAddress "00:11:22:33:44:55" -MacAddressSpoofing On
这个命令可以获取虚拟机的网络适配器对象,并修改其静态 MAC 地址和 MAC 地址欺骗设置。

设置虚拟机的快照:
powershell
Checkpoint-VM -Name "VM1" -SnapshotName "Snapshot1" -SnapshotFilePath "C:\Snapshots\Snapshot1.avhdx"
这个命令可以为虚拟机创建快照,包括指定快照的名称和保存路径。

还原虚拟机的快照:
powershell
Restore-VMSnapshot -Name "VM1" -SnapshotName "Snapshot1"
这个命令可以还原虚拟机到指定的快照状态。

请注意,虚拟机的快照操作需要在关闭虚拟机的情况下进行。
设置虚拟机的内存大小:
powershell
Set-VMMemory -VMName "VM1" -DynamicMemoryEnabled $true -MinimumBytes 512MB -StartupBytes 1GB -MaximumBytes 4GB
这个命令可以设置虚拟机的内存为动态内存,并指定最小、启动和最大内存大小。

修改虚拟机的 CPU 核心数:
powershell
Set-VMProcessor -VMName "VM1" -Count 4
这个命令可以修改虚拟机的 CPU 核心数为指定值。

启动虚拟机:
powershell
Start-VM -Name "VM1"
这个命令可以启动指定名称的虚拟机。

关闭虚拟机:
powershell
Stop-VM -Name "VM1"
这个命令可以关闭指定名称的虚拟机。

重启虚拟机:
powershell
Restart-VM -Name "VM1"
这个命令可以重启指定名称的虚拟机。
设置虚拟机的启动顺序:
powershell
Set-VMFirmware -VMName "VM1" -FirstBootDevice "CD"
这个命令可以设置虚拟机的启动顺序,将首选启动设备设置为 CD-ROM。

创建虚拟交换机:
powershell
New-VMSwitch -Name "Switch1" -SwitchType Internal
这个命令可以创建一个内部虚拟交换机,用于虚拟机之间或与主机之间的通信。

添加网络适配器到虚拟机:
powershell
Add-VMNetworkAdapter -VMName "VM1" -SwitchName "Switch1"
这个命令可以将一个网络适配器添加到指定名称的虚拟机,并连接到指定的虚拟交换机。

移除虚拟机的网络适配器:
powershell
Remove-VMNetworkAdapter -VMName "VM1" -Name "NetworkAdapter1"
这个命令可以从指定名称的虚拟机中移除指定名称的网络适配器。

设置虚拟机的启动延迟:
powershell
Set-VMStartupDelay -VMName "VM1" -StartupDelay 30
这个命令可以设置虚拟机启动时的延迟时间,单位为秒。
设置虚拟机的启动顺序:
powershell
Set-VMFirmware -VMName "VM1" -FirstBootDevice "CD"
这个命令可以设置虚拟机的启动顺序,将首选启动设备设置为 CD-ROM。

创建虚拟交换机:
powershell
New-VMSwitch -Name "Switch1" -SwitchType Internal
这个命令可以创建一个内部虚拟交换机,用于虚拟机之间或与主机之间的通信。

添加网络适配器到虚拟机:
powershell
Add-VMNetworkAdapter -VMName "VM1" -SwitchName "Switch1"
这个命令可以将一个网络适配器添加到指定名称的虚拟机,并连接到指定的虚拟交换机。

移除虚拟机的网络适配器:
powershell
Remove-VMNetworkAdapter -VMName "VM1" -Name "NetworkAdapter1"
这个命令可以从指定名称的虚拟机中移除指定名称的网络适配器。

设置虚拟机的启动延迟:
powershell
Set-VMStartupDelay -VMName "VM1" -StartupDelay 30
这个命令可以设置虚拟机启动时的延迟时间,单位为秒。

导出虚拟机配置:
powershell
Export-VM -Name "VM1" -Path "C:\ExportedVMs\VM1"
这个命令可以将指定名称的虚拟机配置导出到指定路径。

导入虚拟机配置:
powershell
Import-VM -Path "C:\ExportedVMs\VM1" -Copy
这个命令可以从指定路径导入虚拟机配置,并创建一个新的虚拟机副本。
设置虚拟机的内存大小:
powershell
Set-VMMemory -VMName "VM1" -DynamicMemoryEnabled $true -MinimumBytes 512MB -StartupBytes 1GB -MaximumBytes 4GB
这个命令可以设置虚拟机的内存为动态分配模式,并指定最小、启动和最大内存大小。

设置虚拟机的处理器数量:
powershell
Set-VMProcessor -VMName "VM1" -Count 2
这个命令可以设置虚拟机的处理器数量为 2 个。

启动虚拟机:
powershell
Start-VM -Name "VM1"
这个命令可以启动指定名称的虚拟机。

关闭虚拟机:
powershell
Stop-VM -Name "VM1"
这个命令可以关闭指定名称的虚拟机。

暂停虚拟机:
powershell
Suspend-VM -Name "VM1"
这个命令可以暂停指定名称的虚拟机。

恢复虚拟机:
powershell
Resume-VM -Name "VM1"
这个命令可以恢复暂停的虚拟机。
创建虚拟机快照:
powershell
Checkpoint-VM -Name "VM1" -SnapshotName "Snapshot1"
这个命令可以创建一个名为 Snapshot1 的虚拟机快照。

恢复虚拟机快照:
powershell
Restore-VMSnapshot -Name "VM1" -SnapshotName "Snapshot1"
这个命令可以将虚拟机恢复到名为 Snapshot1 的虚拟机快照的状态。

删除虚拟机快照:
powershell
Remove-VMSnapshot -Name "VM1" -SnapshotName "Snapshot1"
这个命令可以删除名为 Snapshot1 的虚拟机快照。

设置虚拟机的网络适配器 MAC 地址:
powershell
Set-VMNetworkAdapter -VMName "VM1" -StaticMacAddress "00-15-5D-01-02-03"
这个命令可以设置指定名称的虚拟机网络适配器的 MAC 地址。

设置虚拟机的显示分辨率:
powershell
Set-VMVideoMemory -VMName "VM1" -HorizontalResolution 1920 -VerticalResolution 1080
这个命令可以设置虚拟机的显示分辨率为 1920x1080。
设置虚拟机的复制状态:
powershell
Set-VMReplication -VMName "VM1" -ReplicaServerName "ReplicaServer" -ReplicaServerPort 80 -AuthenticationType Integrated
这个命令可以设置虚拟机的复制状态,将其复制到指定的 ReplicaServer,并指定复制端口和认证类型。

查看虚拟机的事件日志:
powershell
Get-WinEvent -LogName "Microsoft-Windows-Hyper-V-VMMS-Admin" -MaxEvents 50
这个命令可以查看虚拟机管理服务的事件日志,最多显示最新的 50 条事件。

设置虚拟机的自动启动:
powershell
Set-VM -Name "VM1" -AutomaticStartAction Start -AutomaticStopAction ShutDown -AutomaticStartDelay 120
这个命令可以设置虚拟机在主机启动时自动启动,并指定自动关闭操作和自动启动延迟时间为 120 秒。

导出虚拟机的运行状态:
powershell
Export-VM -Name "VM1" -Path "C:\ExportedVMs\VM1" -SnapshotFileFormat vhdx -ExportVmId
这个命令可以导出虚拟机的运行状态到指定路径,并指定快照文件格式为 vhdx 并包括虚拟机的唯一标识符。
设置虚拟机的内存大小:
powershell
Set-VMMemory -VMName "VM1" -DynamicMemoryEnabled $true -MinimumBytes 2GB -MaximumBytes 4GB -StartupBytes 2GB
这个命令可以设置虚拟机的内存大小,并启用动态内存分配。你可以指定最小、最大和启动时的内存大小。

设置虚拟机的 CPU 核心数:
powershell
Set-VMProcessor -VMName "VM1" -Count 2
这个命令可以设置虚拟机的 CPU 核心数为 2。

设置虚拟机的网络适配器:
powershell
Add-VMNetworkAdapter -VMName "VM1" -SwitchName "External Network"
这个命令可以添加一个网络适配器到虚拟机,并将其连接到指定名称的虚拟交换机上。

移动虚拟机的存储位置:
powershell
Move-VMStorage -VMName "VM1" -DestinationStoragePath "\\FileServer\VMs"
这个命令可以将虚拟机的存储位置移动到指定的存储路径。
设置虚拟机的启动顺序:
powershell
Set-VMFirmware -VMName "VM1" -FirstBootDevice "CD"
这个命令可以设置虚拟机的启动顺序为首先从 CD/DVD 驱动器启动。

导入虚拟机配置文件:
powershell
Import-VM -Path "C:\ImportedVMs\VM1" -Copy -VhdDestinationPath "D:\Virtual Hard Disks"
这个命令可以导入虚拟机的配置文件,并将相关的虚拟硬盘文件复制到指定的目标路径。

设置虚拟机的网络适配器静态 IP 地址:
powershell
Set-VMNetworkAdapter -VMName "VM1" -StaticIPAddress "192.168.0.100" -SubnetMask "255.255.255.0" -DefaultGateway "192.168.0.1" -DNS "192.168.0.2", "192.168.0.3"
这个命令可以设置虚拟机网络适配器的静态 IP 地址、子网掩码、默认网关和 DNS 服务器。

设置虚拟机的启动延迟时间:
powershell
Set-VM -Name "VM1" -AutomaticStartDelay 180
这个命令可以设置虚拟机的启动延迟时间为 180 秒。
设置虚拟机的快照:
powershell
Checkpoint-VM -Name "VM1" -SnapshotName "BeforeUpdate"
这个命令可以为虚拟机创建一个快照,方便在更新或配置更改之前进行备份。

移除虚拟机的网络适配器:
powershell
Remove-VMNetworkAdapter -VMName "VM1" -Name "Network Adapter 2"
这个命令可以移除虚拟机的指定网络适配器。

启动虚拟机:
powershell
Start-VM -Name "VM1"
这个命令可以启动指定的虚拟机。

关闭虚拟机:
powershell
Stop-VM -Name "VM1"
这个命令可以关闭指定的虚拟机。
导出虚拟机的配置文件和磁盘文件:
powershell
Export-VM -Name "VM1" -Path "D:\ExportedVMs"
这个命令可以导出虚拟机的配置文件和相关的虚拟硬盘文件到指定的路径。

启用远程管理功能:
powershell
Enable-PSRemoting
这个命令可以启用 PowerShell 远程管理功能,允许你从远程计算机上管理 Hyper-V 虚拟机。

设置虚拟机的自动启动:
powershell
Set-VM -Name "VM1" -AutomaticStartAction StartIfRunning -AutomaticStopAction Save
这个命令可以设置虚拟机的自动启动和自动停止行为。在此示例中,虚拟机将在宿主机启动时自动启动,并在宿主机停止时保存状态。

设置虚拟机的内部网络连接模式:
powershell
Set-VMNetworkAdapter -VMName "VM1" -Internal
这个命令可以将虚拟机的网络适配器连接到内部网络,以便与其他虚拟机进行通信。
设置虚拟机的动态内存分配:
powershell
Set-VMMemory -VMName "VM1" -DynamicMemoryEnabled $true -MinimumBytes 512MB -MaximumBytes 4GB
这个命令可以启用虚拟机的动态内存分配,并设置最小和最大内存限制。

添加虚拟机的硬盘:
powershell
Add-VMHardDiskDrive -VMName "VM1" -ControllerType SCSI -ControllerNumber 0 -ControllerLocation 1 -Path "D:\Virtual Hard Disks\NewDisk.vhdx"
这个命令可以为虚拟机添加一个硬盘,指定控制器类型、控制器号码、控制器位置和硬盘文件路径。

设置虚拟机的 CPU 核心数:
powershell
Set-VMProcessor -VMName "VM1" -Count 4
这个命令可以设置虚拟机的 CPU 核心数为4个。

设置虚拟机的自动快照:
powershell
Set-VM -Name "VM1" -SnapshotFileLocation "D:\Snapshots"
这个命令可以设置虚拟机的自动快照文件保存路径。
移除虚拟机的快照:
powershell
Remove-VMSnapshot -Name "Snapshot1" -VMName "VM1"
这个命令可以移除指定虚拟机的指定快照。

设置虚拟机的内存优先级:
powershell
Set-VM -Name "VM1" -MemoryPriority High
这个命令可以设置虚拟机的内存优先级为高。

设置虚拟机的启动顺序:
powershell
Set-VMFirmware -VMName "VM1" -FirstBootDevice CD
这个命令可以设置虚拟机的启动顺序,将首选启动设备设置为 CD/DVD 驱动器。

设置虚拟机的网络适配器连接类型:
powershell
Set-VMNetworkAdapter -VMName "VM1" -SwitchName "Virtual Switch"
这个命令可以将虚拟机的网络适配器连接到指定的虚拟交换机。
设置虚拟机的启动顺序:
powershell
Set-VMFirmware -VMName "VM1" -FirstBootDevice VHD
这个命令可以设置虚拟机的启动顺序,将首选启动设备设置为虚拟硬盘。

设置虚拟机的检查点自动停止:
powershell
Set-VM -Name "VM1" -CheckpointType Disabled
这个命令可以禁用虚拟机的检查点功能,确保在虚拟机关闭时不会自动创建检查点。

设置虚拟机的优先级:
powershell
Set-VM -Name "VM1" -Priority High
这个命令可以设置虚拟机的优先级为高,确保在宿主机资源受限时获得更多的资源配额。

复制虚拟机配置:
powershell
Copy-VM -Name "VM1" -DestinationHost "AnotherHost" -IncludeStorage -VHDPathMapping @{ "C:\VMs\VM1"="D:\VMs\VM1" }
这个命令可以将指定虚拟机及其相关的存储复制到另一台 Hyper-V 宿主机上。