Files

62 lines
2.3 KiB
PowerShell

#Requires -RunAsAdministrator
<#
.SYNOPSIS
Готовит VMware VM к загрузке Ventoy с raw-диска:
сбрасывает NVRAM (boot-записи) и, при -SecureBoot Off, выключает SB в VM.
.PARAMETER Vmx
Путь к .vmx.
.PARAMETER SecureBoot
On/Off. По умолчанию Off (для первого теста; сертификат Ventoy в NVRAM VM
не импортирован, при On потребуется MokManager).
#>
[CmdletBinding()]
param(
[string]$Vmx = 'D:\Paravis\Documents\Virtual Machines\Windows 10\Windows 10.vmx',
[ValidateSet('On', 'Off')]
[string]$SecureBoot = 'Off'
)
$ErrorActionPreference = 'Stop'
function Write-Step([string]$t) { Write-Host "`n=== $t ===" -ForegroundColor Cyan }
function Write-Ok([string]$t) { Write-Host "OK: $t" -ForegroundColor Green }
function Write-Warn([string]$t) { Write-Host "WARN: $t" -ForegroundColor Yellow }
if (-not (Test-Path $Vmx)) { throw "Нет файла: $Vmx" }
$dir = Split-Path $Vmx
$vmxProc = Get-Process -Name 'vmware-vmx' -ErrorAction SilentlyContinue
if ($vmxProc) { throw "VM запущена. Выключите её." }
Write-Step "Бэкап VMX"
$bak = "$Vmx.bak-{0:yyyyMMdd-HHmmss}" -f (Get-Date)
Copy-Item $Vmx $bak -Force
Write-Ok $bak
Write-Step "Secure Boot = $SecureBoot"
$txt = Get-Content $Vmx -Raw -Encoding UTF8
if ($SecureBoot -eq 'Off') {
if ($txt -match 'uefi\.secureBoot\.enabled') {
$txt = $txt -replace 'uefi\.secureBoot\.enabled\s*=\s*"[^"]*"', 'uefi.secureBoot.enabled = "FALSE"'
} else {
$txt = $txt.TrimEnd() + "`nuefi.secureBoot.enabled = `"FALSE`"`n"
}
} else {
$txt = $txt -replace 'uefi\.secureBoot\.enabled\s*=\s*"[^"]*"', 'uefi.secureBoot.enabled = "TRUE"'
}
[IO.File]::WriteAllText($Vmx, $txt, (New-Object System.Text.UTF8Encoding($false)))
Write-Ok "записано"
Write-Step "Сброс NVRAM"
Get-ChildItem $dir -Filter '*.nvram' | ForEach-Object {
$b = "$($_.FullName).bak-{0:yyyyMMdd-HHmmss}" -f (Get-Date)
Copy-Item $_.FullName $b -Force
Remove-Item $_.FullName -Force
Write-Ok ("удалён {0} (бэкап {1})" -f $_.Name, (Split-Path $b -Leaf))
}
Write-Step "Диски VM"
Select-String -Path $Vmx -Pattern '^(sata|nvme|scsi|ide).*(fileName|deviceType)|^firmware|secureBoot' |
Select-Object -ExpandProperty Line