From 102231c0e3b910d0b16c235238378727f1a39831 Mon Sep 17 00:00:00 2001 From: CodingWonders <101426328+CodingWonders@users.noreply.github.com> Date: Mon, 23 Sep 2024 19:29:34 +0200 Subject: [PATCH] [MicroWin] ISO Downloader Enhancements (#2787) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Avoid duplicating items in lang list and fix issue - Changed the first item in the language list to show "System language". Then we determine it and download it - Fixed an issue where the entire contents of `gci` were being passed to the variable, which causes the rest to fail * Add spaces for consistent indentation * Modify current dir command to automatic variable Thanks @ruxunderscore for suggesting * Move downloaded ISO to user-specified path * Indentation fixes for comments * Update functions/public/Invoke-WPFGetIso.ps1 Co-authored-by: Luka Momčilović * Modify first language item and add error handling --------- Co-authored-by: Luka Momčilović --- functions/public/Invoke-WPFGetIso.ps1 | 53 +++++++++++++++++++++++++-- scripts/main.ps1 | 6 +-- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/functions/public/Invoke-WPFGetIso.ps1 b/functions/public/Invoke-WPFGetIso.ps1 index 98b653d5..954ee432 100644 --- a/functions/public/Invoke-WPFGetIso.ps1 +++ b/functions/public/Invoke-WPFGetIso.ps1 @@ -77,17 +77,64 @@ function Invoke-WPFGetIso { return } } elseif ($sync["ISOdownloader"].IsChecked) { + # Create folder browsers for user-specified locations + [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null + $isoDownloaderFBD = New-Object System.Windows.Forms.FolderBrowserDialog + $isoDownloaderFBD.Description = "Please specify the path to download the ISO file to:" + $isoDownloaderFBD.ShowNewFolderButton = $true + if ($isoDownloaderFBD.ShowDialog() -ne [System.Windows.Forms.DialogResult]::OK) + { + return + } + + # Grab the location of the selected path + $targetFolder = $isoDownloaderFBD.SelectedPath + # Auto download newest ISO # Credit: https://github.com/pbatard/Fido $fidopath = "$env:temp\Fido.ps1" - $originalLocation = Get-Location + $originalLocation = $PSScriptRoot Invoke-WebRequest "https://github.com/pbatard/Fido/raw/master/Fido.ps1" -OutFile $fidopath Set-Location -Path $env:temp - & $fidopath -Win 'Windows 11' -Rel $sync["ISORelease"].SelectedItem -Arch "x64" -Lang $sync["ISOLanguage"].SelectedItem -Ed "Windows 11 Home/Pro/Edu" + # Detect if the first option ("System language") has been selected and get a Fido-approved language from the current culture + $lang = if ($sync["ISOLanguage"].SelectedIndex -eq 0) { + Get-FidoLangFromCulture -langName (Get-Culture).Name + } else { + $sync["ISOLanguage"].SelectedItem + } + + & $fidopath -Win 'Windows 11' -Rel $sync["ISORelease"].SelectedItem -Arch "x64" -Lang $lang -Ed "Windows 11 Home/Pro/Edu" + if (-not $?) + { + Write-Host "Could not download the ISO file. Look at the output of the console for more information." + $msg = "The ISO file could not be downloaded" + [System.Windows.MessageBox]::Show($msg, "Winutil", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Error) + return + } Set-Location $originalLocation - $filePath = Get-ChildItem -Path "$env:temp" -Filter "Win11*.iso" | Sort-Object LastWriteTime -Descending | Select-Object -First 1 + # Use the FullName property to only grab the file names. Using this property is necessary as, without it, you're passing the usual output of Get-ChildItem + # to the variable, and let's be honest, that does NOT exist in the file system + $filePath = (Get-ChildItem -Path "$env:temp" -Filter "Win11*.iso").FullName | Sort-Object LastWriteTime -Descending | Select-Object -First 1 + $fileName = [IO.Path]::GetFileName("$filePath") + + if (($targetFolder -ne "") -and (Test-Path "$targetFolder")) + { + try + { + # "Let it download to $env:TEMP and then we **move** it to the file path." - CodingWonders + $destinationFilePath = "$targetFolder\$fileName" + Write-Host "Moving ISO file. Please wait..." + Move-Item -Path "$filePath" -Destination "$destinationFilePath" -Force + $filePath = $destinationFilePath + } + catch + { + Write-Host "Unable to move the ISO file to the location you specified. The downloaded ISO is in the `"$env:TEMP`" folder" + Write-Host "Error information: $($_.Exception.Message)" -ForegroundColor Yellow + } + } } Write-Host "File path $($filePath)" diff --git a/scripts/main.ps1 b/scripts/main.ps1 index 61f6845f..873aab4d 100644 --- a/scripts/main.ps1 +++ b/scripts/main.ps1 @@ -415,9 +415,7 @@ $sync["ISORelease"].Items.Add("22H2") | Out-Null $sync["ISORelease"].Items.Add("21H2") | Out-Null $sync["ISORelease"].SelectedItem = "23H2" -$currentCulture = Get-FidoLangFromCulture -langName (Get-Culture).Name - -$sync["ISOLanguage"].Items.Add($currentCulture) | Out-Null +$sync["ISOLanguage"].Items.Add("System Language ($(Get-FidoLangFromCulture -langName $((Get-Culture).Name)))") | Out-Null if ($currentCulture -ne "English International") { $sync["ISOLanguage"].Items.Add("English International") | Out-Null } @@ -427,7 +425,7 @@ if ($currentCulture -ne "English") { if ($sync["ISOLanguage"].Items.Count -eq 1) { $sync["ISOLanguage"].IsEnabled = $false } -$sync["ISOLanguage"].SelectedItem = $currentCulture +$sync["ISOLanguage"].SelectedIndex = 0 # Load Checkboxes and Labels outside of the Filter function only once on startup for performance reasons