PowerShell 创建Hyper-v 虚拟机

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PowerShell 创建Hyper-v 虚拟机相关的知识,希望对你有一定的参考价值。

在日常工作中,经常遇到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
elseSet-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

PowerShell


总结

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/​

以上是关于PowerShell 创建Hyper-v 虚拟机的主要内容,如果未能解决你的问题,请参考以下文章

怎样用PowerShell命令开启或重启hyper-v server中的虚拟机

Hyper-V 2016 系列教程40 使用 PowerShell 实现虚拟机自动化和管理虚拟机

Hyper-V 批量创建虚拟机自动改IP并配置PPPOE拨号

Hyper-V 2016 系列教程39 在 Windows 10中使用 Hyper-V 和 Windows PowerShell

监控Hyper-V 宿主机上的虚拟机数量

powershell Hyper-V:创建虚拟硬盘以安装Linux