mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2026-10-08 11:39:10 +11:00
* Rework the Win11 Creator tab into a three step wizard - Turn the stacked sections into pages you can navigate between - Move the edition picker and driver option to the modify step - Show a working page while mounting and modifying run - Show what the run changes and a finished panel with the output path - Put the status log in its own card next to the steps - Rename Clean & Reset to Start Over and move it below the actions * Follow the existing colours on the Win11 Creator tab - Take button text and icon colour from the button they sit in - Stop the implicit TextBlock style painting a label background on buttons - Colour step headers from the tab item state - Use the label accent for headings and field labels like the other tabs * Cut repeated markup on the Win11 Creator tab - Render button icon and label from a ContentTemplate, glyph goes in Tag - Render the step header from a HeaderTemplate the same way - Move the page scroll wrapper into the tab control template - Derive the text styles from one another instead of repeating setters * fix: tidy the dropdown and stop the spinner stuttering - combo box items painted their own background while the popup painted another, so the list read as loose labels on a mismatched sheet; items are transparent now and the popup carries the colour - stretch the item presenter so a highlight spans the popup instead of just the text, and bind the popup width to the closed box - cache the working page's spinner: it animates a rotation on a TextBlock, which re-rasterises the glyph every frame on the interface thread - run that animation only while the page is visible; it was started on Loaded and never stopped, so it kept spinning after the step was done * Stop the Win11 Creator status log rendering justified The shared TextBox style sets HorizontalContentAlignment to Stretch, which WPF reads as TextAlignment.Justify. Every other box in WinUtil is single line, so the status log is the only place it shows, and there it stretched each wrapped line to the full width with gaps between the words. Left-aligns it, and while the log is being read as a log rather than as prose: monospace from a new Win11LogFontFamily theme key so the timestamps form a column and a wrapped line is visibly not a new entry, and no effect, since the shared style's drop shadow has neither border nor background to sit under on this control and smeared the glyphs instead. * Show the working page for export, USB write and Start Over Only mount and modify moved to the working page. Saving an ISO, writing a USB drive and Start Over all ran with the finished output page still on screen, so the three longest operations in the tab were the ones that looked like nothing was happening. All three now open the working page for their duration and return to the page they belong on: the output step for the two that produce media, the ISO picker for Start Over, which has just deleted the working directory. Failures leave the working page before their message box rather than putting a modal over a spinner, and a finally guard covers cancellation, which skips catch. Start Over spins the icon backwards. Its work undoes rather than produces, and reading that off the animation is quicker than reading the label. * cut comments Leaves the four that answer a "why is it written this way" a reader cannot get from the code: the shared TextBox style's justify and drop shadow, the spinner tag being set before the page is shown, going back to Select rather than Modify after a failed modification, and the finally guards existing for cancellation rather than for failure. * cut changes that only reword Reverts three edits that changed wording without changing meaning, and drops the comment on the failed-modification branch. * cut comments that narrate the code * address coderabbit review Hyphenate three-step, and let the chevrons grow past their minimum instead of pinning a size the glyph could outgrow. * dismount a replaced ISO and name the USB disk when done Picking a second ISO after verifying one left the first attached: the mount job clears Win11ISOImagePath, which is the only handle cleanup has to it. Dismount it in the job, off the interface thread, before that reset. The done panel now names the disk it wrote, the way the ISO branch names its file. * keep the wizard inside the card at the minimum window width The step column and the chevron grid's middle column were both pinned at 620, while the left card is about 485px at the 800px minimum. The pages do not scroll sideways, so controls past the card edge were unreachable. Both are now capped rather than fixed, and the removed WPFWin11ISOArchLabel was never assigned by anything. * stop a replacement mount when the old ISO will not dismount The warn-and-continue path cleared Win11ISOImagePath anyway, which strands the old mount for the rest of the session - the leak the dismount was added to close. It now reports and throws instead, from inside the try so the finally still re-enables the browse and mount buttons. * cut comments that narrate the code * drop the OneDrive sync claim from the modify step list The post-install script sets DisableFileSyncNGSC=1, but it is prepended to FirstLogon.ps1, whose own body then sets the same value back to 0. The policy never survives, so the bullet was promising something the run does not do. Replaced with search box suggestions, which it does do.
263 lines
14 KiB
PowerShell
263 lines
14 KiB
PowerShell
function Invoke-WinUtilISORefreshUSBDrives {
|
|
$combo = $sync["WPFWin11ISOUSBDriveComboBox"]
|
|
$removable = @(Get-Disk | Where-Object { $_.BusType -eq "USB" } | Sort-Object Number)
|
|
|
|
$combo.Items.Clear()
|
|
|
|
if ($removable.Count -eq 0) {
|
|
$combo.Items.Add("No USB drives detected.")
|
|
$combo.SelectedIndex = 0
|
|
$sync["Win11ISOUSBDisks"] = @()
|
|
Write-WinUtilISOLog "No USB drives detected."
|
|
return
|
|
}
|
|
|
|
foreach ($disk in $removable) {
|
|
$sizeGB = [math]::Round($disk.Size / 1GB, 1)
|
|
$combo.Items.Add("Disk $($disk.Number): $($disk.FriendlyName) [$sizeGB GB] - $($disk.PartitionStyle)")
|
|
}
|
|
$combo.SelectedIndex = 0
|
|
Write-WinUtilISOLog "Found $($removable.Count) USB drive(s)."
|
|
$sync["Win11ISOUSBDisks"] = $removable
|
|
}
|
|
|
|
function Get-WinUtilFreeDriveLetter {
|
|
<#
|
|
.SYNOPSIS
|
|
Returns the first unused drive letter between D and Z, or $null when there is none.
|
|
#>
|
|
|
|
$used = (Get-PSDrive -PSProvider FileSystem).Name
|
|
foreach ($c in [char[]](68..90)) {
|
|
if ($used -notcontains [string]$c) { return $c }
|
|
}
|
|
return $null
|
|
}
|
|
|
|
function Invoke-WinUtilISOWriteUSB {
|
|
$contentsDir = $sync["Win11ISOContentsDir"]
|
|
$usbDisks = $sync["Win11ISOUSBDisks"]
|
|
|
|
if (-not $contentsDir -or -not (Test-Path $contentsDir)) {
|
|
Show-WinUtilMessage -Message "No modified ISO content found. Please run the modification step first." -Title "Not Ready" -Button "OK" -Icon "Warning" | Out-Null
|
|
return
|
|
}
|
|
|
|
$installEsd = Join-Path $contentsDir "sources\install.esd"
|
|
if (Test-Path $installEsd) {
|
|
$esdSizeBytes = (Get-Item $installEsd).Length
|
|
if ($esdSizeBytes -ge 4GB) {
|
|
$esdSizeMB = [math]::Ceiling($esdSizeBytes / 1MB)
|
|
Show-WinUtilMessage -Message "This ISO uses an install.esd file that is $esdSizeMB MB. WinUtil's FAT32 USB format cannot store files larger than 4 GB.`n`nExport an ISO instead or use media with install.wim." -Title "USB Creation Not Supported" -Button "OK" -Icon "Warning" | Out-Null
|
|
return
|
|
}
|
|
}
|
|
|
|
$combo = $sync["WPFWin11ISOUSBDriveComboBox"]
|
|
$selectedIndex = $combo.SelectedIndex
|
|
$selectedItemText = [string]$combo.SelectedItem
|
|
$usbDisks = @($usbDisks)
|
|
|
|
$targetDisk = $null
|
|
if ($selectedIndex -ge 0 -and $selectedIndex -lt $usbDisks.Count) {
|
|
$targetDisk = $usbDisks[$selectedIndex]
|
|
} elseif ($selectedItemText -match 'Disk\s+(\d+):') {
|
|
$selectedDiskNum = [int]$matches[1]
|
|
$targetDisk = $usbDisks | Where-Object { $_.Number -eq $selectedDiskNum } | Select-Object -First 1
|
|
}
|
|
|
|
if (-not $targetDisk) {
|
|
Show-WinUtilMessage -Message "Please select a USB drive from the dropdown." -Title "No Drive Selected" -Button "OK" -Icon "Warning" | Out-Null
|
|
return
|
|
}
|
|
|
|
$diskNum = $targetDisk.Number
|
|
$sizeGB = [math]::Round($targetDisk.Size / 1GB, 1)
|
|
|
|
$confirm = Show-WinUtilMessage -Message "ALL data on Disk $diskNum ($($targetDisk.FriendlyName), $sizeGB GB) will be PERMANENTLY ERASED.`n`nAre you sure you want to continue?" -Title "Confirm USB Erase" -Button "YesNo" -Icon "Warning"
|
|
if ($confirm -ne "Yes") {
|
|
Write-WinUtilISOLog "USB write cancelled by user."
|
|
return
|
|
}
|
|
|
|
Start-WinUtilJob -Name "USB write" -Description "Writing USB drive" -Parameters @{
|
|
DiskNumber = $diskNum
|
|
ContentsDir = $contentsDir
|
|
} -ScriptBlock {
|
|
param($DiskNumber, $contentsDir)
|
|
|
|
Invoke-WPFUIThread -ScriptBlock { $sync["WPFWin11ISOWriteUSBButton"].IsEnabled = $false }
|
|
Set-WinUtilISOStep -Step "Working" -Label "Writing the USB drive"
|
|
|
|
try {
|
|
Write-WinUtilISOLog "Starting USB write to Disk $DiskNumber..."
|
|
Step-WinUtilJob -Status "Formatting USB drive..." -Percent 10
|
|
|
|
# Phase 1: Clean disk via diskpart (retry once if the drive is not yet ready)
|
|
$dpFile1 = Join-Path $env:TEMP "winutil_diskpart_$(Get-Random).txt"
|
|
"select disk $DiskNumber`nclean`nexit" | Set-Content -Path $dpFile1 -Encoding ASCII
|
|
Write-WinUtilISOLog "Running diskpart clean on Disk $DiskNumber..."
|
|
$dpCleanOut = diskpart /s $dpFile1
|
|
$dpCleanOut | Where-Object { $_ -match '\S' } | ForEach-Object { Write-WinUtilISOLog " diskpart: $_" }
|
|
Remove-Item $dpFile1 -Force
|
|
|
|
if (($dpCleanOut -join ' ') -match 'device is not ready') {
|
|
Write-WinUtilISOLog "Disk $DiskNumber was not ready; waiting 5 seconds and retrying clean..."
|
|
Start-Sleep -Seconds 5
|
|
Update-Disk -Number $DiskNumber
|
|
$dpFile1b = Join-Path $env:TEMP "winutil_diskpart_$(Get-Random).txt"
|
|
"select disk $DiskNumber`nclean`nexit" | Set-Content -Path $dpFile1b -Encoding ASCII
|
|
diskpart /s $dpFile1b | Where-Object { $_ -match '\S' } | ForEach-Object { Write-WinUtilISOLog " diskpart: $_" }
|
|
Remove-Item $dpFile1b -Force
|
|
}
|
|
|
|
# Phase 2: Initialize as GPT
|
|
Start-Sleep -Seconds 2
|
|
Update-Disk -Number $DiskNumber
|
|
$diskObj = Get-Disk -Number $DiskNumber
|
|
if ($diskObj.PartitionStyle -eq 'RAW') {
|
|
Initialize-Disk -Number $DiskNumber -PartitionStyle GPT
|
|
Write-WinUtilISOLog "Disk $DiskNumber initialized as GPT."
|
|
} else {
|
|
Set-Disk -Number $DiskNumber -PartitionStyle GPT
|
|
Write-WinUtilISOLog "Disk $DiskNumber converted to GPT (was $($diskObj.PartitionStyle))."
|
|
}
|
|
|
|
# Phase 3: Create FAT32 partition via diskpart, then format with Format-Volume
|
|
# (diskpart's 'format' command can fail with "no volume selected" on fresh/never-formatted drives)
|
|
$volLabel = "W11-" + (Get-Date).ToString('yyMMdd')
|
|
$dpFile2 = Join-Path $env:TEMP "winutil_diskpart2_$(Get-Random).txt"
|
|
$maxFat32PartitionMB = 32768
|
|
$diskSizeMB = [int][Math]::Floor((Get-Disk -Number $DiskNumber).Size / 1MB)
|
|
$createPartitionCommand = "create partition primary"
|
|
if ($diskSizeMB -gt $maxFat32PartitionMB) {
|
|
$createPartitionCommand = "create partition primary size=$maxFat32PartitionMB"
|
|
Write-WinUtilISOLog "Disk $DiskNumber is $diskSizeMB MB; creating FAT32 partition capped at $maxFat32PartitionMB MB (32 GB)."
|
|
}
|
|
|
|
@(
|
|
"select disk $DiskNumber"
|
|
$createPartitionCommand
|
|
"exit"
|
|
) | Set-Content -Path $dpFile2 -Encoding ASCII
|
|
Write-WinUtilISOLog "Creating partitions on Disk $DiskNumber..."
|
|
diskpart /s $dpFile2 | Where-Object { $_ -match '\S' } | ForEach-Object { Write-WinUtilISOLog " diskpart: $_" }
|
|
Remove-Item $dpFile2 -Force
|
|
|
|
Step-WinUtilJob -Status "Formatting USB partition..." -Percent 25
|
|
Start-Sleep -Seconds 3
|
|
Update-Disk -Number $DiskNumber
|
|
|
|
$partitions = Get-Partition -DiskNumber $DiskNumber
|
|
Write-WinUtilISOLog "Partitions on Disk $DiskNumber after creation: $($partitions.Count)"
|
|
foreach ($p in $partitions) {
|
|
Write-WinUtilISOLog " Partition $($p.PartitionNumber) Type=$($p.Type) Letter=$($p.DriveLetter) Size=$([math]::Round($p.Size/1MB))MB"
|
|
}
|
|
|
|
$winpePart = $partitions | Where-Object { $_.Type -eq "Basic" } | Select-Object -Last 1
|
|
if (-not $winpePart) {
|
|
throw "Could not find the Basic partition on Disk $DiskNumber after creation."
|
|
}
|
|
|
|
# Format using Format-Volume (reliable on fresh drives; diskpart format fails
|
|
# with 'no volume selected' when the partition has never been formatted before)
|
|
Write-WinUtilISOLog "Formatting Partition $($winpePart.PartitionNumber) as FAT32 (label: $volLabel)..."
|
|
Get-Partition -DiskNumber $DiskNumber -PartitionNumber $winpePart.PartitionNumber |
|
|
Format-Volume -FileSystem FAT32 -NewFileSystemLabel $volLabel -Force -Confirm:$false
|
|
Write-WinUtilISOLog "Partition $($winpePart.PartitionNumber) formatted as FAT32."
|
|
|
|
Step-WinUtilJob -Status "Assigning drive letters..." -Percent 30
|
|
Start-Sleep -Seconds 2
|
|
Update-Disk -Number $DiskNumber
|
|
|
|
try { Remove-PartitionAccessPath -DiskNumber $DiskNumber -PartitionNumber $winpePart.PartitionNumber -AccessPath "$($winpePart.DriveLetter):" } catch { Write-WinUtilISOLog -Level "WARN" -Message "Could not remove existing partition access path: $_" }
|
|
$usbLetter = Get-WinUtilFreeDriveLetter
|
|
if (-not $usbLetter) { throw "No free drive letters (D-Z) available to assign to the USB data partition." }
|
|
Set-Partition -DiskNumber $DiskNumber -PartitionNumber $winpePart.PartitionNumber -NewDriveLetter $usbLetter
|
|
Write-WinUtilISOLog "Assigned drive letter $usbLetter to WINPE partition (Partition $($winpePart.PartitionNumber))."
|
|
Start-Sleep -Seconds 2
|
|
|
|
$usbDrive = "${usbLetter}:"
|
|
$retries = 0
|
|
while (-not (Test-Path $usbDrive) -and $retries -lt 6) {
|
|
$retries++
|
|
Write-WinUtilISOLog "Waiting for $usbDrive to become accessible (attempt $retries/6)..."
|
|
Start-Sleep -Seconds 2
|
|
}
|
|
if (-not (Test-Path $usbDrive)) { throw "Drive $usbDrive is not accessible after letter assignment." }
|
|
Write-WinUtilISOLog "USB data partition: $usbDrive"
|
|
|
|
$contentSizeBytes = (Get-ChildItem -LiteralPath $contentsDir -File -Recurse -Force | Measure-Object -Property Length -Sum).Sum
|
|
if (-not $contentSizeBytes) { $contentSizeBytes = 0 }
|
|
$usbVolume = Get-Volume -DriveLetter $usbLetter
|
|
$partitionCapacityBytes = [int64]$usbVolume.Size
|
|
$partitionFreeBytes = [int64]$usbVolume.SizeRemaining
|
|
|
|
$contentSizeGB = [math]::Round($contentSizeBytes / 1GB, 2)
|
|
$partitionCapacityGB = [math]::Round($partitionCapacityBytes / 1GB, 2)
|
|
$partitionFreeGB = [math]::Round($partitionFreeBytes / 1GB, 2)
|
|
|
|
Write-WinUtilISOLog "Source content size: $contentSizeGB GB. USB partition capacity: $partitionCapacityGB GB, free: $partitionFreeGB GB."
|
|
|
|
if ($contentSizeBytes -gt $partitionCapacityBytes) {
|
|
throw "ISO content ($contentSizeGB GB) is larger than the USB partition capacity ($partitionCapacityGB GB). Use a larger USB drive or reduce image size."
|
|
}
|
|
|
|
if ($contentSizeBytes -gt $partitionFreeBytes) {
|
|
throw "Insufficient free space on USB partition. Required: $contentSizeGB GB, available: $partitionFreeGB GB."
|
|
}
|
|
|
|
Step-WinUtilJob -Status "Copying Windows 11 files to USB..." -Percent 45
|
|
|
|
# Copy files; split install.wim if > 4 GB (FAT32 limit)
|
|
$installWim = Join-Path $contentsDir "sources\install.wim"
|
|
if (Test-Path $installWim) {
|
|
$wimSizeMB = [math]::Round((Get-Item $installWim).Length / 1MB)
|
|
if ($wimSizeMB -gt 3800) {
|
|
Write-WinUtilISOLog "install.wim is $wimSizeMB MB - splitting for FAT32 compatibility... This will take several minutes."
|
|
Set-ItemProperty -LiteralPath $installWim -Name IsReadOnly -Value $false
|
|
$splitDest = Join-Path $usbDrive "sources\install.swm"
|
|
New-Item -ItemType Directory -Path (Split-Path $splitDest) -Force | Out-Null
|
|
Split-WindowsImage -ImagePath $installWim -SplitImagePath $splitDest -FileSize 3800 -CheckIntegrity
|
|
Write-WinUtilISOLog "install.wim split complete."
|
|
Write-WinUtilISOLog "Copying remaining files to USB..."
|
|
Invoke-WinUtilRobocopy -Source $contentsDir -Destination $usbDrive -Arguments @("/E","/XF","install.wim","/NFL","/NDL","/NJH","/NJS")
|
|
} else {
|
|
Invoke-WinUtilRobocopy -Source $contentsDir -Destination $usbDrive -Arguments @("/E","/NFL","/NDL","/NJH","/NJS")
|
|
}
|
|
} else {
|
|
Invoke-WinUtilRobocopy -Source $contentsDir -Destination $usbDrive -Arguments @("/E","/NFL","/NDL","/NJH","/NJS")
|
|
}
|
|
|
|
Step-WinUtilJob -Status "Finalising USB drive..." -Percent 90
|
|
Write-WinUtilISOLog "Files copied to USB."
|
|
Step-WinUtilJob -Status "USB write complete" -Percent 100
|
|
Write-WinUtilISOLog "USB drive is ready for use."
|
|
|
|
Invoke-WPFUIThread -Parameters @{ DiskNumber = $DiskNumber } -ScriptBlock {
|
|
param($DiskNumber)
|
|
|
|
$sync["WPFWin11ISODoneLabel"].Text = "Disk $DiskNumber is ready to boot from."
|
|
$sync["WPFWin11ISODonePanel"].Visibility = "Visible"
|
|
}
|
|
Set-WinUtilISOStep -Step "Output"
|
|
Show-WinUtilMessage -Message "USB drive created successfully!`n`nYou can now boot from this drive to install Windows 11." -Title "USB Ready" -Button "OK" -Icon "Info" | Out-Null
|
|
} catch {
|
|
Write-WinUtilISOLog -Level "ERROR" -Message "USB write failed: $_"
|
|
$_.Exception.Data["WinUtilErrorReported"] = $true
|
|
Set-WinUtilISOStep -Step "Output"
|
|
Show-WinUtilMessage -Message "USB write failed:`n`n$_" -Title "USB Write Error" -Button "OK" -Icon "Error" | Out-Null
|
|
throw
|
|
} finally {
|
|
Invoke-WPFUIThread -ScriptBlock {
|
|
$sync["WPFWin11ISOWriteUSBButton"].IsEnabled = $true
|
|
|
|
# Cancellation skips catch, so the working page can still be up here
|
|
if ($sync["WPFWin11ISOWorkingSection"].IsSelected) {
|
|
Set-WinUtilISOStep -Step "Output"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|