在日常工作中,经常遇到Hyper-v 创建虚拟机的情况。

手动创建十分繁琐且缓慢,以下是我通过Powershell 创建Hyper-v虚拟机的方式。分享给大家(* ̄︶ ̄)

function Create-VM{

Param(
[Parameter(Mandatory=$true)]
[string]$VMName,
#can create a VM using an existing VHD template,if not create the new one.$VHDPath = Full path contains file name and extension.
$VHDPath, # Templete Path, Can be empty
$BootOp = "CD", #Boot Up
$VMRootPath = "C:\hyper-v\", # VM Save Path
$VMGen = 2, # Memory Gen 2
$VMSwitch = "Default Switch", # SWitch Name
$VMISOPath = "E:\ISO\en_windows_server_2019_updated_jun_2021_x64_dvd_a2a2f782.iso", # ISO path, Can be empty
$VMDiskSize = 100GB,
$VMMem = 4GB,
$VMProcesssorCount = 12,
$StaticMemory = $false
)


if(($VMRootPath -match "\\$"))
{
$VMRootPath = $VMRootPath -replace "\\$"
}

# judge VM path --inexistence,Create
if(!(Test-Path -Path $VMRootPath))
{
New-Item $VMRootPath -ItemType Directory
}


$VMPath = (Get-Item $VMRootPath).FullName + "\" + $VMName
if(!(Test-Path $VMPath))
{New-Item $VMPath -ItemType Directory}

# use exist template VHD Create VM
if($VHDPath)
{
Get-Item $VHDPath
Copy-Item -Path $VHDPath -Destination $($VMPath + "\" + $VMName + ".vhdx")
New-VM -Name $VMName -MemoryStartupBytes 4GB -Path $VMPath -Generation 2 -SwitchName "Default Switch"

Add-VMHardDiskDrive -VMName $VMName -Path $($VMPath + "\" + $VMName + ".vhdx") -ControllerType SCSI
$VMDiskDrive = Get-VMHardDiskDrive -VMName $VMName
Set-VMFirmware -FirstBootDevice $VMDiskDrive -VMName $VMName

}
#if VHD inexistence, New VHD Create VM
else
{
$VMHDPath = $VMPath + "\" + $VMName + ".vhdx"
$VMHDPath = New-VHD -Path $VMHDPath -Dynamic -SizeBytes $VMDiskSize
New-VM -Name $VMName -MemoryStartupBytes $VMMem -Path $VMPath -Generation 2 -SwitchName "Default Switch" -BootDevice $BootOp
Add-VMHardDiskDrive -VMName $VMName -Path $VMHDPath.Path -ControllerType SCSI
}

if($VMISOPath)
{
Set-VMDvdDrive -VMName $VMName -Path $VMISOPath
}

# Set VM Processor Count and Disable AutomaticCheckpoints
Set-VMProcessor -VMName $VMName -Count $VMProcesssorCount -ExposeVirtualizationExtensions $true
if($StaticMemory){Set-VM -AutomaticCheckpointsEnabled $false -VMName $VMName -StaticMemory}
else{Set-VM -AutomaticCheckpointsEnabled $false -VMName $VMName}


Start-VM $VMName
}


# Create-VM -VMName Centos8.4_Docker_Package1 -VMISOPath "E:\ISO\CentOS-8.4.2105-x86_64-dvd1.iso" -VMGen 1
# Create-VM -VMName H2019-DC03 -VMRootPath C:\hyper-v1\ -VMGen 2 -VMSwitch "Default Switch" -VMISOPath "E:\ISO\en_windows_server_2019_updated_jun_2021_x64_dvd_a2a2f782.iso" -VMDiskSize 80GB -VMProcesssorCount 10
# Create-VM -VMName H2019-Host01 -VMGen 2 -VMSwitch "Default Switch" -VMISOPath "E:\ISO\en_windows_server_2019_updated_jun_2021_x64_dvd_a2a2f782.iso" -VMDiskSize 80GB -VMProcesssorCount 10 -VHDPath D:\Hyper-V\Winsrv-2019-EN.vhdx
# Create-VM -VMName H2019-Host02 -VMGen 2 -VMSwitch "Default Switch" -VMISOPath "E:\ISO\en_windows_server_2019_updated_jun_2021_x64_dvd_a2a2f782.iso" -VMDiskSize 80GB -VMProcesssorCount 10 -VHDPath D:\Hyper-V\Winsrv-2019-EN.vhdx
# Create-VM -VMName H2019-Host03 -VMGen 2 -VMSwitch "Default Switch" -VMISOPath "E:\ISO\en_windows_server_2019_updated_jun_2021_x64_dvd_a2a2f782.iso" -VMDiskSize 80GB -VMProcesssorCount 10 -VHDPath D:\Hyper-V\Winsrv-2019-EN.vhdx
# Create-VM -VMName H2019-Host04 -VMGen 2 -VMSwitch "Default Switch" -VMISOPath "E:\ISO\en_windows_server_2019_updated_jun_2021_x64_dvd_a2a2f782.iso" -VMDiskSize 80GB -VMProcesssorCount 10 -VHDPath D:\Hyper-V\Winsrv-2019-EN.vhdx
Create-VM -VMName H2019-Host05 -VMGen 2 -VMSwitch "Default Switch" -VMISOPath "E:\ISO\en_windows_server_2019_updated_jun_2021_x64_dvd_a2a2f782.iso" -VMDiskSize 80GB -VMProcesssorCount 10 -VHDPath D:\Hyper-V\Winsrv-2019-EN.vhdx
Create-VM -VMName H2019-Host06 -VMGen 2 -VMSwitch "Default Switch" -VMISOPath "E:\ISO\en_windows_server_2019_updated_jun_2021_x64_dvd_a2a2f782.iso" -VMDiskSize 80GB -VMProcesssorCount 10 -VHDPath D:\Hyper-V\Winsrv-2019-EN.vhdx

PowerShell 创建Hyper-v 虚拟机_创建虚机

PowerShell 创建Hyper-v 虚拟机_Windows Server_02


总结

Update 2023-3-3 add Set-VMFirmware -FirstBootDevice $VMDiskDrive -VMName $VMName

添加VHD后,网络驱动启动很烦,修改设置为VHD启动。

Update 2023-3-10   $VMMem = 4GB, $StaticMemory = $false

添加内存参数,并添加静态内存配置选项。


Quoting:

​https://learn.microsoft.com/zh-cn/virtualization/hyper-v-on-windows/about/​