Files
Ventoy/build-scripts/layout/fix-vmware-rawdisk.ps1

71 lines
2.9 KiB
PowerShell
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#Requires -RunAsAdministrator
<#
.SYNOPSIS
Убирает из VMware VM второй raw-диск (nvme0:0), указывающий на тот же
\\.\PhysicalDrive2, что и sata0:0. Плюс чистит устаревшие .lck.
.DESCRIPTION
VMware не может открыть один физический диск дважды — отсюда
"Операция с файлом \\.\PhysicalDrive2 не удалась".
Скрипт:
1. проверяет, что VM не запущена (нет процесса vmware-vmx);
2. делает бэкап .vmx;
3. удаляет строки nvme0.*;
4. удаляет устаревшие каталоги *.lck;
5. печатает оставшиеся диски.
.PARAMETER Vmx
Путь к .vmx. По умолчанию "D:\Paravis\Documents\Virtual Machines\Windows 10\Windows 10.vmx".
#>
[CmdletBinding()]
param(
[string]$Vmx = 'D:\Paravis\Documents\Virtual Machines\Windows 10\Windows 10.vmx'
)
$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
Write-Step "Проверка, что VM не запущена"
$vmxProc = Get-Process -Name 'vmware-vmx' -ErrorAction SilentlyContinue
if ($vmxProc) {
throw "VM запущена (vmware-vmx PID $($vmxProc.Id -join ',')). Выключите её и повторите."
}
Write-Ok "vmware-vmx не запущен."
Write-Step "Бэкап"
$bak = "$Vmx.bak-{0:yyyyMMdd-HHmmss}" -f (Get-Date)
Copy-Item $Vmx $bak -Force
Write-Ok "бэкап: $bak"
Write-Step "Удаление nvme0:* из VMX"
$lines = Get-Content $Vmx -Encoding UTF8
$keep = $lines | Where-Object { $_ -notmatch '^nvme0' }
$removed = $lines.Count - $keep.Count
if ($removed -eq 0) {
Write-Warn "строк nvme0 не найдено — возможно, уже убрано."
} else {
$utf8 = New-Object System.Text.UTF8Encoding($false)
[IO.File]::WriteAllLines($Vmx, $keep, $utf8)
Write-Ok ("удалено строк: {0}" -f $removed)
}
Write-Step "Удаление устаревших .lck"
Get-ChildItem $dir -Directory -Filter '*.lck' -ErrorAction SilentlyContinue | ForEach-Object {
Remove-Item $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
Write-Ok ("удалён {0}" -f $_.Name)
}
Write-Step "Текущие диски в VMX"
Select-String -Path $Vmx -Pattern 'present = "TRUE"|fileName|deviceType' |
Where-Object { $_.Line -match '^(sata|nvme|scsi|ide)' } |
Select-Object -ExpandProperty Line
Write-Step "Готово"
Write-Host "Запускайте VM. Должен остаться один raw-диск (sata0:0 -> Windows 10-1.vmdk)." -ForegroundColor Green