Improve driver injection by mounting WIMs

Replaces the slow WIM extraction process with a faster WIM mount operation. This significantly improves performance when injecting drivers from a WIM file.

The script now properly unmounts the WIM after injection to ensure resources are cleaned up correctly.

Additionally, log messages are now written to the console for better real-time feedback during script execution.
This commit is contained in:
rbalsleyMSFT
2025-07-10 12:24:49 -07:00
parent f64514cbe6
commit 9f6ea0fb58
@@ -47,6 +47,7 @@ function Get-HardDrive() {
function WriteLog($LogText) { function WriteLog($LogText) {
Add-Content -path $LogFile -value "$((Get-Date).ToString()) $LogText" Add-Content -path $LogFile -value "$((Get-Date).ToString()) $LogText"
Write-Host $LogText
} }
function Set-DiskpartAnswerFiles($DiskpartFile, $DiskID) { function Set-DiskpartAnswerFiles($DiskpartFile, $DiskID) {
@@ -760,10 +761,10 @@ if ($null -ne $DriverSourcePath) {
WriteLog "Creating temporary directory for drivers at $TempDriverDir" WriteLog "Creating temporary directory for drivers at $TempDriverDir"
New-Item -Path $TempDriverDir -ItemType Directory -Force | Out-Null New-Item -Path $TempDriverDir -ItemType Directory -Force | Out-Null
WriteLog "Extracting WIM contents to $TempDriverDir" WriteLog "Mounting WIM contents to $TempDriverDir"
Write-Warning 'Extracting Drivers from WIM - dism will pop a window with no progress. This can take a few minutes to complete. Please be patient.' # For some reason can't use /mount-image with invoke-process, so using dism.exe directly
Invoke-Process dism.exe "/Apply-Image /ImageFile:""$DriverSourcePath"" /Index:1 /ApplyDir:""$TempDriverDir""" dism.exe /Mount-Image /ImageFile:$DriverSourcePath /Index:1 /MountDir:$TempDriverDir /ReadOnly /optimize
WriteLog "WIM extraction successful." WriteLog "WIM mount successful."
WriteLog "Injecting drivers from $TempDriverDir" WriteLog "Injecting drivers from $TempDriverDir"
Write-Warning 'Injecting Drivers from WIM - dism will pop a window with no progress. This can take a few minutes to complete. Please be patient.' Write-Warning 'Injecting Drivers from WIM - dism will pop a window with no progress. This can take a few minutes to complete. Please be patient.'
@@ -779,6 +780,9 @@ if ($null -ne $DriverSourcePath) {
} }
finally { finally {
if (Test-Path -Path $TempDriverDir) { if (Test-Path -Path $TempDriverDir) {
WriteLog "Unmounting WIM from $TempDriverDir"
Invoke-Process dism.exe "/Unmount-Image /MountDir:""$TempDriverDir"" /Discard"
WriteLog "Unmount successful."
WriteLog "Cleaning up temporary driver directory: $TempDriverDir" WriteLog "Cleaning up temporary driver directory: $TempDriverDir"
Remove-Item -Path $TempDriverDir -Recurse -Force Remove-Item -Path $TempDriverDir -Recurse -Force
WriteLog "Cleanup successful." WriteLog "Cleanup successful."