Windows快速创建 Hyper-V 虚拟机的 PowerShell 脚本

在 Windows 系统上使用 Hyper-V 虚拟化技术时,手动创建虚拟机可能会比较繁琐。可以通过 PowerShell 脚本来自动化这一过程,大大提高效率。今天我将分享一个简单的 PowerShell 脚本,用于快速创建一个 Debian 12 虚拟机,并配置相关的硬件资源和集成服务。

我编写的 PowerShell 脚本能够自动化以下任务:

  • 创建一台 Debian 12 虚拟机。
  • 分配内存、CPU 核心、虚拟硬盘。
  • 配置网络适配器。
  • 启动虚拟机并设置增强会话模式。

保存为hyperv-create.ps1

#Requires -RunAsAdministrator
#Requires -Modules Hyper-V

$Name = "Debian12-hyperv"
$Description = "
Username: test
Password: test
(US keyboard layout)"

# Create the virtual machine
New-VM `
  -Generation 2 `
  -Name "$Name" `
  -MemoryStartupBytes 4096MB `
  -SwitchName "Default Switch" `
  -VHDPath ".\Debian12-hyperv.vhdx"

# Set virtual machine notes
Set-VM -Name "$Name" -Notes "$Description"

# Enable enhanced session transport
Set-VM -Name "$Name" -EnhancedSessionTransportType HVSocket

# Disable secure boot
Set-VMFirmware -VMName "$Name" -EnableSecureBoot Off

# Set CPU cores
Set-VMProcessor -VMName "$Name" -Count 2

# Enable integration services
Enable-VMIntegrationService -VMName "$Name" -Name "Guest Service Interface"

# Start the virtual machine
Start-VM -Name $Name

Write-Host "Virtual machine $Name created and started successfully."

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注