Skip to content

Commit d671f18

Browse files
committed
Fix with oscdimg.exe search, ExecutionPolicy, compression, etc..
PR: ntdevlabs#319
1 parent 49af624 commit d671f18

File tree

4 files changed

+71
-45
lines changed

4 files changed

+71
-45
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ You can now use it on ANY Windows 11 release (not just a specific build), as wel
1010
This is made possible thanks to the much-improved scripting capabilities of PowerShell, compared to the older Batch release.
1111
</br>
1212
Since it is written in PowerShell, you need to set the execution policy to `Unrestricted`, so that you could run the script.
13-
If you haven't done this before, make sure to run `Set-ExecutionPolicy unrestricted` as administrator in PowerShell before running the script, otherwise it would just crash.
13+
If you haven't done this before, make sure to run `Set-ExecutionPolicy -Scope Process unrestricted` as administrator in PowerShell before running the script, otherwise it would just crash.
1414

1515

1616
This is a script created to automate the build of a streamlined Windows 11 image, similar to tiny11.

Run.bat

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:: Reference from https://github.com/Raphire/Win11Debloat/blob/master/Run.bat licensed under MIT license.
2+
3+
@echo off
4+
5+
Powershell -ExecutionPolicy Bypass -Command "& {Start-Process Powershell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dp0tiny11maker.ps1""' -Verb RunAs}"

tiny11Coremaker.ps1

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Enable debugging
22
Set-PSDebug -Trace 1
33

4-
# Check if PowerShell execution is restricted
5-
if ((Get-ExecutionPolicy) -eq 'Restricted') {
6-
Write-Host "Your current PowerShell Execution Policy is set to Restricted, which prevents scripts from running. Do you want to change it to RemoteSigned? (yes/no)"
4+
# Check if PowerShell execution is Restricted or AllSigned or Undefined
5+
$needchange = @("AllSigned", "Restricted", "Undefined")
6+
$curpolicy = Get-ExecutionPolicy
7+
if ($curpolicy -in $needchange) {
8+
Write-Host "Your current PowerShell Execution Policy is set to $curpolicy, which prevents scripts from running. Do you want to change it to RemoteSigned? (yes/no)"
79
$response = Read-Host
810
if ($response -eq 'yes') {
9-
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm:$false
11+
Set-ExecutionPolicy RemoteSigned -Scope Process -Confirm:$false
1012
} else {
1113
Write-Host "The script cannot be run without changing the execution policy. Exiting..."
1214
exit
@@ -732,14 +734,24 @@ Write-Host "Exporting ESD. This may take a while..."
732734
Remove-Item "$mainOSDrive\tiny11\sources\install.wim" > $null 2>&1
733735
Write-Host "The tiny11 image is now completed. Proceeding with the making of the ISO..."
734736
Write-Host "Creating ISO image..."
735-
$ADKDepTools = "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\$hostarchitecture\Oscdimg"
737+
# Get Windows ADK path from registry(following Visual Studio's winsdk.bat approach).
738+
$WinSDKPath = [Microsoft.Win32.Registry]::GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows Kits\Installed Roots", "KitsRoot10", $null)
739+
if ($null -eq $WinSDKPath) {
740+
$WinSDKPath = [Microsoft.Win32.Registry]::GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Kits\Installed Roots", "KitsRoot10", $null)
741+
}
742+
743+
if ($null -ne $WinSDKPath) {
744+
# Trim the following backslash for path concatenation.
745+
$WinSDKPath = $WinSDKPath.TrimEnd('\')
746+
$ADKDepTools = "$WinSDKPath\Assessment and Deployment Kit\Deployment Tools\$hostarchitecture\Oscdimg"
747+
}
736748
$localOSCDIMGPath = "$PSScriptRoot\oscdimg.exe"
737749

738-
if ([System.IO.Directory]::Exists($ADKDepTools)) {
750+
if ((Test-Path variable:ADKDepTools) -and (Test-Path "$ADKDepTools\oscdimg.exe" -PathType leaf)) {
739751
Write-Host "Will be using oscdimg.exe from system ADK."
740752
$OSCDIMG = "$ADKDepTools\oscdimg.exe"
741753
} else {
742-
Write-Host "ADK folder not found. Will be using bundled oscdimg.exe."
754+
Write-Host "oscdimg.exe from system ADK not found. Will be using bundled oscdimg.exe."
743755

744756

745757
$url = "https://msdl.microsoft.com/download/symbols/oscdimg.exe/3D44737265000/oscdimg.exe"

tiny11maker.ps1

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,26 @@
22
#Set-PSDebug -Trace 1
33

44
param (
5-
[ValidatePattern('^[c-zC-Z]$')]
5+
[ValidatePattern('^[c-zC-Z]:$')]
66
[string]$ScratchDisk
77
)
88

99
if (-not $ScratchDisk) {
10-
$ScratchDisk = $PSScriptRoot -replace '[\\]+$', ''
10+
$ScratchDisk = ((Get-Location).Drive.Name) + ":"
1111
} else {
1212
$ScratchDisk = $ScratchDisk + ":"
1313
}
1414

1515
Write-Output "Scratch disk set to $ScratchDisk"
1616

17-
# Check if PowerShell execution is restricted
18-
if ((Get-ExecutionPolicy) -eq 'Restricted') {
19-
Write-Host "Your current PowerShell Execution Policy is set to Restricted, which prevents scripts from running. Do you want to change it to RemoteSigned? (yes/no)"
17+
# Check if PowerShell execution is Restricted or AllSigned or Undefined
18+
$needchange = @("AllSigned", "Restricted", "Undefined")
19+
$curpolicy = Get-ExecutionPolicy
20+
if ($curpolicy -in $needchange) {
21+
Write-Host "Your current PowerShell Execution Policy is set to $curpolicy, which prevents scripts from running. Do you want to change it to RemoteSigned? (yes/no)"
2022
$response = Read-Host
2123
if ($response -eq 'yes') {
22-
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm:$false
24+
Set-ExecutionPolicy RemoteSigned -Scope Process -Confirm:$false
2325
} else {
2426
Write-Host "The script cannot be run without changing the execution policy. Exiting..."
2527
exit
@@ -107,8 +109,9 @@ try {
107109
# This block will catch the error and suppress it.
108110
}
109111
New-Item -ItemType Directory -Force -Path "$ScratchDisk\scratchdir" > $null
110-
Mount-WindowsImage -ImagePath $ScratchDisk\tiny11\sources\install.wim -Index $index -Path $ScratchDisk\scratchdir
112+
Mount-WindowsImage -ImagePath $wimFilePath -Index $index -Path $ScratchDisk\scratchdir
111113

114+
# Powershell dism module does not have direct equivalent for /Get-Intl
112115
$imageIntl = & dism /English /Get-Intl "/Image:$($ScratchDisk)\scratchdir"
113116
$languageLine = $imageIntl -split '\n' | Where-Object { $_ -match 'Default system UI language : ([a-zA-Z]{2}-[a-zA-Z]{2})' }
114117

@@ -119,33 +122,28 @@ if ($languageLine) {
119122
Write-Host "Default system UI language code not found."
120123
}
121124

122-
$imageInfo = & 'dism' '/English' '/Get-WimInfo' "/wimFile:$($ScratchDisk)\tiny11\sources\install.wim" "/index:$index"
123-
$lines = $imageInfo -split '\r?\n'
124-
125-
foreach ($line in $lines) {
126-
if ($line -like '*Architecture : *') {
127-
$architecture = $line -replace 'Architecture : ',''
128-
# If the architecture is x64, replace it with amd64
129-
if ($architecture -eq 'x64') {
130-
$architecture = 'amd64'
131-
}
132-
Write-Host "Architecture: $architecture"
133-
break
134-
}
125+
# Defined in (Microsoft.Dism.Commands.ImageInfoObject).Architecture formatting script
126+
# 0 -> x86, 5 -> arm(currently unused), 6 -> ia64(currently unused), 9 -> x64, 12 -> arm64
127+
switch ((Get-WindowsImage -ImagePath $wimFilePath -Index $index).Architecture)
128+
{
129+
0 { $architecture = "x86" }
130+
9 { $architecture = "amd64" }
131+
12 { $architecture = "arm64" }
135132
}
136133

137-
if (-not $architecture) {
134+
if ($architecture) {
135+
Write-Host "Architecture: $architecture"
136+
} else {
138137
Write-Host "Architecture information not found."
139138
}
140139

141-
Write-Host "Mounting complete! Performing removal of applications..."
140+
Write-Host "Mounting complete! Performing removal of applications...`n"
142141

143-
$packages = & 'dism' '/English' "/image:$($ScratchDisk)\scratchdir" '/Get-ProvisionedAppxPackages' |
142+
$packages = Get-ProvisionedAppxPackage -Path "$ScratchDisk\scratchdir" |
144143
ForEach-Object {
145-
if ($_ -match 'PackageName : (.*)') {
146-
$matches[1]
147-
}
144+
$_.PackageName
148145
}
146+
149147
$packagePrefixes =
150148
'Clipchamp.Clipchamp_',
151149
'Microsoft.BingNews_',
@@ -187,11 +185,12 @@ $packagesToRemove = $packages | Where-Object {
187185
$packagePrefixes -contains ($packagePrefixes | Where-Object { $packageName -like "$_*" })
188186
}
189187
foreach ($package in $packagesToRemove) {
190-
& 'dism' '/English' "/image:$($ScratchDisk)\scratchdir" '/Remove-ProvisionedAppxPackage' "/PackageName:$package"
188+
Write-Host "Removing $package..."
189+
Remove-AppxProvisionedPackage -Path "$ScratchDisk\scratchdir" -PackageName "$package" | Out-Null
191190
}
192191

193192

194-
Write-Host "Removing Edge:"
193+
Write-Host "`nRemoving Edge:"
195194
Remove-Item -Path "$ScratchDisk\scratchdir\Program Files (x86)\Microsoft\Edge" -Recurse -Force | Out-Null
196195
Remove-Item -Path "$ScratchDisk\scratchdir\Program Files (x86)\Microsoft\EdgeUpdate" -Recurse -Force | Out-Null
197196
Remove-Item -Path "$ScratchDisk\scratchdir\Program Files (x86)\Microsoft\EdgeCore" -Recurse -Force | Out-Null
@@ -433,10 +432,10 @@ Write-Host ' '
433432
Write-Host "Unmounting image..."
434433
Dismount-WindowsImage -Path $ScratchDisk\scratchdir -Save
435434
Write-Host "Exporting image..."
436-
# Compressiontype Recovery is not supported with PShell https://learn.microsoft.com/en-us/powershell/module/dism/export-windowsimage?view=windowsserver2022-ps#-compressiontype
437-
Export-WindowsImage -SourceImagePath $ScratchDisk\tiny11\sources\install.wim -SourceIndex $index -DestinationImagePath $ScratchDisk\tiny11\sources\install2.wim -CompressionType Fast
438-
Remove-Item -Path "$ScratchDisk\tiny11\sources\install.wim" -Force | Out-Null
439-
Rename-Item -Path "$ScratchDisk\tiny11\sources\install2.wim" -NewName "install.wim" | Out-Null
435+
# Run `Export-WindowsImage` with undocumented CompressionType "LZMS" (which is the same compression used for Recovery from dism.exe)
436+
Export-WindowsImage -SourceImagePath "$ScratchDisk\tiny11\sources\install.wim" -SourceIndex "$index" -DestinationImagePath "$ScratchDisk\tiny11\sources\install2.wim" -CompressionType "LZMS"
437+
Move-Item -Path "$ScratchDisk\tiny11\sources\install2.wim" -Destination "$ScratchDisk\tiny11\sources\install.wim" -Force | Out-Null
438+
440439
Write-Host "Windows image completed. Continuing with boot.wim."
441440
Start-Sleep -Seconds 2
442441
Clear-Host
@@ -481,22 +480,32 @@ Write-Host "The tiny11 image is now completed. Proceeding with the making of the
481480
Write-Host "Copying unattended file for bypassing MS account on OOBE..."
482481
Copy-Item -Path "$PSScriptRoot\autounattend.xml" -Destination "$ScratchDisk\tiny11\autounattend.xml" -Force | Out-Null
483482
Write-Host "Creating ISO image..."
484-
$ADKDepTools = "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\$hostarchitecture\Oscdimg"
483+
# Get Windows ADK path from registry(following Visual Studio's winsdk.bat approach).
484+
$WinSDKPath = [Microsoft.Win32.Registry]::GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows Kits\Installed Roots", "KitsRoot10", $null)
485+
if (!$WinSDKPath) {
486+
$WinSDKPath = [Microsoft.Win32.Registry]::GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Kits\Installed Roots", "KitsRoot10", $null)
487+
}
488+
489+
if ($WinSDKPath) {
490+
# Trim the following backslash for path concatenation.
491+
$WinSDKPath = $WinSDKPath.TrimEnd('\')
492+
$ADKDepTools = "$WinSDKPath\Assessment and Deployment Kit\Deployment Tools\$hostarchitecture\Oscdimg"
493+
}
485494
$localOSCDIMGPath = "$PSScriptRoot\oscdimg.exe"
486495

487-
if ([System.IO.Directory]::Exists($ADKDepTools)) {
496+
if ($ADKDepTools -and [System.IO.File]::Exists("$ADKDepTools\oscdimg.exe")) {
488497
Write-Host "Will be using oscdimg.exe from system ADK."
489498
$OSCDIMG = "$ADKDepTools\oscdimg.exe"
490499
} else {
491-
Write-Host "ADK folder not found. Will be using bundled oscdimg.exe."
500+
Write-Host "oscdimg.exe from system ADK not found. Will be using bundled oscdimg.exe."
492501

493502
$url = "https://msdl.microsoft.com/download/symbols/oscdimg.exe/3D44737265000/oscdimg.exe"
494503

495-
if (-not (Test-Path -Path $localOSCDIMGPath)) {
504+
if (![System.IO.File]::Exists($localOSCDIMGPath)) {
496505
Write-Host "Downloading oscdimg.exe..."
497506
Invoke-WebRequest -Uri $url -OutFile $localOSCDIMGPath
498507

499-
if (Test-Path $localOSCDIMGPath) {
508+
if ([System.IO.File]::Exists($localOSCDIMGPath)) {
500509
Write-Host "oscdimg.exe downloaded successfully."
501510
} else {
502511
Write-Error "Failed to download oscdimg.exe."

0 commit comments

Comments
 (0)