7676} catch {
7777 # This block will catch the error and suppress it.
7878}
79+ # Clean and recreate scratchdir to ensure it's empty
80+ if (Test-Path " $mainOSDrive \scratchdir" ) {
81+ Write-Host " Cleaning existing scratchdir..."
82+ Remove-Item - Path " $mainOSDrive \scratchdir" - Recurse - Force - ErrorAction SilentlyContinue
83+ Start-Sleep - Seconds 2
84+ }
7985New-Item - ItemType Directory - Force - Path " $mainOSDrive \scratchdir" > $null
8086& dism / English " /mount-image" " /imagefile:$ ( $env: SystemDrive ) \tiny11\sources\install.wim" " /index:$index " " /mountdir:$ ( $env: SystemDrive ) \scratchdir"
8187
@@ -156,12 +162,25 @@ foreach ($packagePattern in $packagePatterns) {
156162 # Filter the packages to remove
157163 $packagesToRemove = $allPackages | Where-Object { $_ -like " $packagePattern *" }
158164
165+ if ($packagesToRemove.Count -eq 0 ) {
166+ Write-Host " No packages found matching pattern: $packagePattern (this is normal for some Windows editions)"
167+ continue
168+ }
169+
159170 foreach ($package in $packagesToRemove ) {
160171 # Extract the package identity
161172 $packageIdentity = ($package -split " \s+" )[0 ]
173+
174+ if ([string ]::IsNullOrWhiteSpace($packageIdentity )) {
175+ continue
176+ }
162177
163178 Write-Host " Removing $packageIdentity ..."
164- & dism / image:$scratchDir / Remove-Package / PackageName:$packageIdentity
179+ $result = & dism / image:$scratchDir / Remove-Package / PackageName:$packageIdentity 2>&1
180+
181+ if ($LASTEXITCODE -ne 0 ) {
182+ Write-Host " Warning: Failed to remove $packageIdentity (Error code: $LASTEXITCODE ). This may be normal for this Windows edition."
183+ }
165184 }
166185}
167186
@@ -483,8 +502,51 @@ Write-Host "Unmounting image..."
483502& ' dism' ' /English' ' /unmount-image' " /mountdir:$mainOSDrive \scratchdir" ' /commit'
484503Write-Host " Exporting image..."
485504& ' dism' ' /English' ' /Export-Image' " /SourceImageFile:$mainOSDrive \tiny11\sources\install.wim" " /SourceIndex:$index " " /DestinationImageFile:$mainOSDrive \tiny11\sources\install2.wim" ' /compress:max'
486- Remove-Item - Path " $mainOSDrive \tiny11\sources\install.wim" - Force > null
487- Rename-Item - Path " $mainOSDrive \tiny11\sources\install2.wim" - NewName " install.wim" > null
505+ # Wait for any file handles to be released
506+ Start-Sleep - Seconds 3
507+
508+ # Try to remove install.wim with retries
509+ $retryCount = 0
510+ $maxRetries = 5
511+ do {
512+ try {
513+ if (Test-Path " $mainOSDrive \tiny11\sources\install.wim" ) {
514+ Remove-Item - Path " $mainOSDrive \tiny11\sources\install.wim" - Force - ErrorAction Stop
515+ Write-Host " Successfully removed install.wim"
516+ break
517+ }
518+ } catch {
519+ $retryCount ++
520+ if ($retryCount -lt $maxRetries ) {
521+ Write-Host " Attempt $retryCount failed to remove install.wim, retrying in 2 seconds..."
522+ Start-Sleep - Seconds 2
523+ } else {
524+ Write-Host " Warning: Could not remove install.wim after $maxRetries attempts. Continuing anyway..."
525+ break
526+ }
527+ }
528+ } while ($retryCount -lt $maxRetries )
529+
530+ # Try to rename install2.wim with retries
531+ $retryCount = 0
532+ do {
533+ try {
534+ if (Test-Path " $mainOSDrive \tiny11\sources\install2.wim" ) {
535+ Rename-Item - Path " $mainOSDrive \tiny11\sources\install2.wim" - NewName " install.wim" - ErrorAction Stop
536+ Write-Host " Successfully renamed install2.wim to install.wim"
537+ break
538+ }
539+ } catch {
540+ $retryCount ++
541+ if ($retryCount -lt $maxRetries ) {
542+ Write-Host " Attempt $retryCount failed to rename install2.wim, retrying in 2 seconds..."
543+ Start-Sleep - Seconds 2
544+ } else {
545+ Write-Host " Warning: Could not rename install2.wim after $maxRetries attempts."
546+ break
547+ }
548+ }
549+ } while ($retryCount -lt $maxRetries )
488550Write-Host " Windows image completed. Continuing with boot.wim."
489551Start-Sleep - Seconds 2
490552Clear-Host
@@ -493,6 +555,13 @@ $wimFilePath = "$($env:SystemDrive)\tiny11\sources\boot.wim"
493555& takeown " /F" $wimFilePath > null
494556& icacls $wimFilePath " /grant" " $ ( $adminGroup.Value ) :(F)"
495557Set-ItemProperty - Path $wimFilePath - Name IsReadOnly - Value $false
558+ # Clean and recreate scratchdir to ensure it's empty for boot.wim mounting
559+ if (Test-Path " $mainOSDrive \scratchdir" ) {
560+ Write-Host " Cleaning existing scratchdir for boot.wim..."
561+ Remove-Item - Path " $mainOSDrive \scratchdir" - Recurse - Force - ErrorAction SilentlyContinue
562+ Start-Sleep - Seconds 2
563+ }
564+ New-Item - ItemType Directory - Force - Path " $mainOSDrive \scratchdir" > $null
496565& ' dism' ' /English' ' /mount-image' " /imagefile:$mainOSDrive \tiny11\sources\boot.wim" ' /index:2' " /mountdir:$mainOSDrive \scratchdir"
497566Write-Host " Loading registry..."
498567reg load HKLM\zCOMPONENTS $mainOSDrive \scratchdir\Windows\System32\config\COMPONENTS
0 commit comments