|
| 1 | +function Expand-DpWindowsImage { |
| 2 | +<# |
| 3 | + .SYNOPSIS |
| 4 | + Applies a Windows image from a WIM file to a target directory using DISM. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Wrapper for DISM.exe to apply a specific image index from a WIM file to a target directory. Mimics Expand-WindowsImage. Optionally uses /Compact for compact OS deployment. |
| 8 | +
|
| 9 | + .PARAMETER ImagePath |
| 10 | + Path to the WIM file containing the Windows image. |
| 11 | +
|
| 12 | + .PARAMETER Index |
| 13 | + Index of the image inside the WIM to apply. |
| 14 | +
|
| 15 | + .PARAMETER ApplyPath |
| 16 | + Target directory where the image will be applied. |
| 17 | +
|
| 18 | + .PARAMETER Compact |
| 19 | + If specified, applies the image using the /Compact option for compact OS deployment. |
| 20 | +
|
| 21 | + .EXAMPLE |
| 22 | + Expand-DpWindowsImage -ImagePath 'C:\images\install.wim' -Index 1 -ApplyPath 'D:\Windows' |
| 23 | +
|
| 24 | + Applies the first image from install.wim to the D:\Windows directory using DISM. |
| 25 | +
|
| 26 | + .EXAMPLE |
| 27 | + Expand-DpWindowsImage -ImagePath 'C:\images\install.wim' -Index 1 -ApplyPath 'D:\Windows' -Compact |
| 28 | +
|
| 29 | + Applies the first image from install.wim to the D:\Windows directory using DISM with the /Compact option for compact OS deployment. |
| 30 | +
|
| 31 | + .NOTES |
| 32 | + Author: WindowsImageTools Team |
| 33 | + Requires: Administrator privileges |
| 34 | +#> |
| 35 | + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] |
| 36 | + param ( |
| 37 | + [Parameter(Mandatory, Position = 0, HelpMessage = 'Path to the WIM file')] |
| 38 | + [ValidateNotNullOrEmpty()] |
| 39 | + [string]$ImagePath, |
| 40 | + |
| 41 | + [Parameter(Mandatory, Position = 1, HelpMessage = 'Index of the image inside the WIM')] |
| 42 | + [ValidateNotNullOrEmpty()] |
| 43 | + [int]$Index, |
| 44 | + |
| 45 | + [Parameter(Mandatory, Position = 2, HelpMessage = 'Target directory to apply the image')] |
| 46 | + [ValidateNotNullOrEmpty()] |
| 47 | + [string]$ApplyPath, |
| 48 | + |
| 49 | + [switch]$Compact |
| 50 | + ) |
| 51 | + |
| 52 | + $ImagePath = Get-FullFilePath -Path $ImagePath |
| 53 | + $ApplyPath = Get-FullFilePath -Path $ApplyPath |
| 54 | + |
| 55 | + $dismArgs = @( |
| 56 | + '/Apply-Image', |
| 57 | + "/ImageFile:$ImagePath", |
| 58 | + "/Index:$Index", |
| 59 | + "/ApplyDir:$ApplyPath" |
| 60 | + ) |
| 61 | + if ($Compact) { |
| 62 | + $dismArgs += '/Compact' |
| 63 | + } |
| 64 | + |
| 65 | + if ($PSCmdlet.ShouldProcess("Apply image index $Index from $ImagePath to $ApplyPath")) { |
| 66 | + $psi = New-Object System.Diagnostics.ProcessStartInfo |
| 67 | + $psi.FileName = 'dism.exe' |
| 68 | + $psi.Arguments = $dismArgs -join ' ' |
| 69 | + $psi.RedirectStandardOutput = $true |
| 70 | + $psi.RedirectStandardError = $true |
| 71 | + $psi.UseShellExecute = $false |
| 72 | + $psi.CreateNoWindow = $true |
| 73 | + $process = [System.Diagnostics.Process]::Start($psi) |
| 74 | + $progressActivity = 'Expand Windows Image' |
| 75 | + $progressId = 0 |
| 76 | + $lastPercent = 0 |
| 77 | + while (-not $process.HasExited) { |
| 78 | + while ($null -ne ($line = $process.StandardOutput.ReadLine())) { |
| 79 | + if ($line -match '(\d{1,3}\.\d)%') { |
| 80 | + $percent = [double]$Matches[1] |
| 81 | + $lastPercent = $percent |
| 82 | + Write-Progress -Id $progressId -Activity $progressActivity -Status "$percent% Complete" -PercentComplete $percent |
| 83 | + } |
| 84 | + } |
| 85 | + Start-Sleep -Milliseconds 100 |
| 86 | + } |
| 87 | + # Read any remaining output after exit |
| 88 | + while ($null -ne ($line = $process.StandardOutput.ReadLine())) { |
| 89 | + if ($line -match '(\d{1,3}\.\d)%') { |
| 90 | + $percent = [double]$Matches[1] |
| 91 | + $lastPercent = $percent |
| 92 | + Write-Progress -Id $progressId -Activity $progressActivity -Status "$percent% Complete" -PercentComplete $percent |
| 93 | + } |
| 94 | + } |
| 95 | + # Always write 100% on exit |
| 96 | + Write-Progress -Id $progressId -Activity $progressActivity -Status '100% Complete' -PercentComplete 100 -Completed |
| 97 | + $stderr = $process.StandardError.ReadToEnd() |
| 98 | + if ($process.ExitCode -ne 0) { |
| 99 | + throw "DISM failed with exit code $($process.ExitCode): $stderr" |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments