diff --git a/functions/private/Invoke-WinUtilISO.ps1 b/functions/private/Invoke-WinUtilISO.ps1 index af9f6f3d9d..4f3b2595fc 100644 --- a/functions/private/Invoke-WinUtilISO.ps1 +++ b/functions/private/Invoke-WinUtilISO.ps1 @@ -529,6 +529,34 @@ function Invoke-WinUtilISOCleanAndReset { $script.BeginInvoke() } +function Get-WinUtilOSCDImgPath { + # Windows ADK installation + $oscdimg = Get-ChildItem "C:\Program Files (x86)\Windows Kits" -Recurse -Filter "oscdimg.exe" -ErrorAction SilentlyContinue | + Select-Object -First 1 -ExpandProperty FullName + if (-not $oscdimg) { + # Per-user winget installation + $oscdimg = Get-ChildItem "$env:LOCALAPPDATA\Microsoft\WinGet\Packages" -Recurse -Filter "oscdimg.exe" -ErrorAction SilentlyContinue | + Where-Object { $_.FullName -match 'Microsoft\.OSCDIMG' } | + Select-Object -First 1 -ExpandProperty FullName + } + + if (-not $oscdimg) { + # Installation available through the current process PATH + $oscdimg = Get-Command oscdimg.exe -CommandType Application -ErrorAction SilentlyContinue | + Select-Object -First 1 -ExpandProperty Source + } + + if (-not $oscdimg) { + # WinGet links that may not yet be available through the current process PATH + $oscdimg = @( + "$env:LOCALAPPDATA\Microsoft\WinGet\Links\oscdimg.exe" + "$env:ProgramFiles\WinGet\Links\oscdimg.exe" + ) | Where-Object { Test-Path -LiteralPath $_ } | Select-Object -First 1 + } + + return $oscdimg +} + function Invoke-WinUtilISOExport { $contentsDir = $sync["Win11ISOContentsDir"] @@ -551,14 +579,7 @@ function Invoke-WinUtilISOExport { $outputISO = $dlg.FileName - # Locate oscdimg.exe (Windows ADK or winget per-user install) - $oscdimg = Get-ChildItem "C:\Program Files (x86)\Windows Kits" -Recurse -Filter "oscdimg.exe" | - Select-Object -First 1 -ExpandProperty FullName - if (-not $oscdimg) { - $oscdimg = Get-ChildItem "$env:LOCALAPPDATA\Microsoft\WinGet\Packages" -Recurse -Filter "oscdimg.exe" | - Where-Object { $_.FullName -match 'Microsoft\.OSCDIMG' } | - Select-Object -First 1 -ExpandProperty FullName - } + $oscdimg = Get-WinUtilOSCDImgPath if (-not $oscdimg) { Write-WinUtilISOLog "oscdimg.exe not found. Attempting to install via winget..." @@ -569,9 +590,7 @@ function Invoke-WinUtilISOExport { $winget = Get-Command winget $result = & $winget install -e --id Microsoft.OSCDIMG --accept-package-agreements --accept-source-agreements Write-WinUtilISOLog "winget output: $result" - $oscdimg = Get-ChildItem "$env:LOCALAPPDATA\Microsoft\WinGet\Packages" -Recurse -Filter "oscdimg.exe" | - Where-Object { $_.FullName -match 'Microsoft\.OSCDIMG' } | - Select-Object -First 1 -ExpandProperty FullName + $oscdimg = Get-WinUtilOSCDImgPath } catch { Write-WinUtilISOLog "winget not available or install failed: $_" }