[CmdletBinding()] param( [Parameter(Mandatory=$true)] [string]$IsoPath, [int]$Index = 0, [switch]$NoUnmount ) $ErrorActionPreference = 'Stop' function Write-Section([string]$title) { Write-Host "" Write-Host "=== $title ===" } function Test-IsAdmin { $principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) } if (!(Test-IsAdmin)) { throw 'Please run this script as Administrator.' } $isoFull = (Resolve-Path $IsoPath).Path if (!(Test-Path -LiteralPath $isoFull)) { throw "ISO not found: $isoFull" } Write-Section 'Mount ISO' Write-Host "ISO: $isoFull" $disk = Mount-DiskImage -ImagePath $isoFull -PassThru Start-Sleep -Seconds 1 $vol = ($disk | Get-Volume | Select-Object -First 1) if ($null -eq $vol -or [string]::IsNullOrWhiteSpace($vol.DriveLetter)) { throw 'Failed to get mounted ISO drive letter.' } $drive = ($vol.DriveLetter + ':') Write-Host "Mounted drive: $drive" try { Write-Section 'Locate install.wim/install.esd' $wim = Join-Path $drive 'sources\install.wim' $esd = Join-Path $drive 'sources\install.esd' $imagePath = $null if (Test-Path -LiteralPath $wim) { $imagePath = $wim } elseif (Test-Path -LiteralPath $esd) { $imagePath = $esd } else { throw 'install.wim/install.esd not found under sources\\' } Write-Host "Image: $imagePath" Write-Section 'Show available indexes' dism /English /Get-WimInfo /WimFile:"$imagePath" if ($Index -le 0) { Write-Host "\nIndex is 0: please re-run with -Index matching your edition (e.g. Pro/Home)." return } Write-Section 'DISM RestoreHealth (from ISO source)' Write-Host "Using Index: $Index" $sourceArg = if ($imagePath.ToLowerInvariant().EndsWith('.wim')) { "wim:$imagePath:$Index" } else { "esd:$imagePath:$Index" } dism /Online /Cleanup-Image /RestoreHealth /Source:$sourceArg /LimitAccess Write-Section 'SFC Scannow' sfc /scannow Write-Host '\nDone. Reboot recommended.' } finally { if (-not $NoUnmount) { Write-Section 'Unmount ISO' Dismount-DiskImage -ImagePath $isoFull -ErrorAction SilentlyContinue } else { Write-Host 'NoUnmount set; ISO left mounted.' } }