From 8c897e93fe5fee62cd6b7dfa5f4f06c6841bc6aa Mon Sep 17 00:00:00 2001 From: Zehadi Alam <63765084+zehadialam@users.noreply.github.com> Date: Thu, 4 Jul 2024 22:57:14 -0400 Subject: [PATCH 1/9] Added support for AppList to be in JSON format. WinGet searches now use app ID. Modified InstallAppsandSysprep.cmd to handle packages with no dependencies --- FFUDevelopment/Apps/AppList.json | 14 ++ FFUDevelopment/Apps/AppsList.txt | 2 - FFUDevelopment/Apps/InstallAppsandSysprep.cmd | 18 +-- FFUDevelopment/BuildFFUVM.ps1 | 121 +++++++++++------- 4 files changed, 98 insertions(+), 57 deletions(-) create mode 100644 FFUDevelopment/Apps/AppList.json delete mode 100644 FFUDevelopment/Apps/AppsList.txt diff --git a/FFUDevelopment/Apps/AppList.json b/FFUDevelopment/Apps/AppList.json new file mode 100644 index 0000000..ea7e1d7 --- /dev/null +++ b/FFUDevelopment/Apps/AppList.json @@ -0,0 +1,14 @@ +{ + "apps": [ + { + "name": "7-Zip", + "id": "7zip.7zip", + "source": "winget" + }, + { + "name": "Company Portal", + "id": "9WZDNCRFJ3PZ", + "source": "msstore" + } + ] +} \ No newline at end of file diff --git a/FFUDevelopment/Apps/AppsList.txt b/FFUDevelopment/Apps/AppsList.txt deleted file mode 100644 index b4903ae..0000000 --- a/FFUDevelopment/Apps/AppsList.txt +++ /dev/null @@ -1,2 +0,0 @@ -winget:7-Zip -store:Company Portal \ No newline at end of file diff --git a/FFUDevelopment/Apps/InstallAppsandSysprep.cmd b/FFUDevelopment/Apps/InstallAppsandSysprep.cmd index fde1977..1fefc88 100644 --- a/FFUDevelopment/Apps/InstallAppsandSysprep.cmd +++ b/FFUDevelopment/Apps/InstallAppsandSysprep.cmd @@ -33,20 +33,20 @@ for /d %%D in ("%basepath%\*") do ( set "licensefile=%%F" ) if defined mainpackage ( + set "dism_command=DISM /Online /Add-ProvisionedAppxPackage /PackagePath:"!mainpackage!"" if exist "!dependenciesfolder!" ( - set "dism_command=DISM /Online /Add-ProvisionedAppxPackage /PackagePath:"!mainpackage!"" for %%G in ("!dependenciesfolder!\*") do ( set "dism_command=!dism_command! /DependencyPackagePath:"%%G"" ) - if defined licensefile ( - set "dism_command=!dism_command! /LicensePath:"!licensefile!"" - ) else ( - set "dism_command=!dism_command! /SkipLicense" - ) - set "dism_command=!dism_command! /Region:All" - echo !dism_command! - !dism_command! ) + if defined licensefile ( + set "dism_command=!dism_command! /LicensePath:"!licensefile!"" + ) else ( + set "dism_command=!dism_command! /SkipLicense" + ) + set "dism_command=!dism_command! /Region:All" + echo !dism_command! + !dism_command! ) ) :remaining diff --git a/FFUDevelopment/BuildFFUVM.ps1 b/FFUDevelopment/BuildFFUVM.ps1 index ef6da11..f0dadac 100644 --- a/FFUDevelopment/BuildFFUVM.ps1 +++ b/FFUDevelopment/BuildFFUVM.ps1 @@ -1737,12 +1737,13 @@ function New-WinGetSettings { function Add-Win32SilentInstallCommand { param ( [string]$AppFolder, - [string]$AppFolderPath + [string]$AppFolderPath, + [string]$LineNumber ) $appName = $AppFolder - $installerPath = Get-ChildItem -Path "$appFolderPath\*" -Include *.exe, *.msi -File -ErrorAction Stop + $installerPath = Get-ChildItem -Path "$appFolderPath\*" -Include "*.exe", "*.msi" -File -ErrorAction Stop $installer = Split-Path -Path $installerPath -Leaf - $yamlFile = Get-ChildItem -Path "$appFolderPath\*" -Include *.yaml -File -ErrorAction Stop + $yamlFile = Get-ChildItem -Path "$appFolderPath\*" -Include "*.yaml" -File -ErrorAction Stop $yamlContent = Get-Content -Path $yamlFile -Raw $silentInstallSwitch = [regex]::Match($yamlContent, 'Silent:\s*(.+)').Groups[1].Value $silentInstallSwitch = $silentInstallSwitch.Replace("'", "").Trim() @@ -1758,6 +1759,11 @@ function Add-Win32SilentInstallCommand { elseif ($installerFileExtension -eq ".msi") { $silentInstallCommand = "msiexec /i `"D:\win32\$appFolder\$installer`" $silentInstallSwitch" } + else { + WriteLog "No win32 app installers were found. Skipping the inclusion of $appName" + Remove-Item -Path $AppFolderPath -Recurse -Force + return + } $cmdFile = "$AppsPath\InstallAppsandSysprep.cmd" $cmdContent = Get-Content -Path $cmdFile $cmdContent = $cmdContent[0..($lineNumber - 2)] + $silentInstallCommand.Trim() + $cmdContent[($lineNumber - 1)..($cmdContent.Length - 1)] @@ -1767,38 +1773,59 @@ function Add-Win32SilentInstallCommand { function Get-WinGetApp { param ( - [string]$WinGetApp, + [string]$WinGetAppName, + [string]$WinGetAppId, [int]$LineNumber ) - $wingetSearchResult = & winget.exe search --name "$WinGetApp" --exact --accept-source-agreements --source winget + $wingetSearchResult = & winget.exe search --id "$WinGetAppId" --exact --accept-source-agreements --source winget if ($wingetSearchResult -contains "No package found matching input criteria.") { - WriteLog "$WinGetApp not found in WinGet repository. Skipping download." + WriteLog "$WinGetAppName not found in WinGet repository. Skipping download." return } - $appFolderPath = Join-Path -Path "$AppsPath\Win32" -ChildPath $WinGetApp + $appFolderPath = Join-Path -Path "$AppsPath\Win32" -ChildPath $WinGetAppName New-Item -Path $appFolderPath -ItemType Directory -Force | Out-Null $appFolder = Split-Path -Path $appFolderPath -Leaf - WriteLog "Downloading $WinGetApp..." - $wingetDownloadResult = & winget.exe download --name "$WinGetApp" --exact --download-directory "$appFolderPath" --scope machine --source winget --architecture "$WindowsArch" | Out-String + WriteLog "Downloading $WinGetAppName..." + $wingetDownloadResult = & winget.exe download --id "$WinGetAppId" --exact --download-directory "$appFolderPath" --scope machine --source winget --architecture "$WindowsArch" | Out-String if ($wingetDownloadResult -match "No applicable installer found") { - $wingetDownloadResult = & winget.exe download --name "$WinGetApp" --exact --download-directory "$appFolderPath" --scope machine --source winget | Out-String + $wingetDownloadResult = & winget.exe download --id "$WinGetAppId" --exact --download-directory "$appFolderPath" --scope machine --source winget | Out-String } if ($wingetDownloadResult -notmatch "Installer downloaded") { - WriteLog "$WinGetApp did not successfully download." + WriteLog "$WinGetAppName did not successfully download." Remove-Item -Path $appFolderPath -Recurse -Force return } - WriteLog "$WinGetApp has completed downloading." - Add-Win32SilentInstallCommand -AppFolder $appFolder -AppFolderPath $appFolderPath + WriteLog "$WinGetAppName has completed downloading to $appFolderPath" + $installerPath = Get-ChildItem -Path "$appFolderPath\*" -Exclude "*.yaml", "*.xml" -File -ErrorAction Stop + $installer = Split-Path -Path $installerPath -Leaf + $installerFileExtension = [System.IO.Path]::GetExtension($installer) + $uwpExtensions = @(".appx", ".appxbundle", ".msix", ".msixbundle") + if ($uwpExtensions -contains $installerFileExtension) { + New-Item -Path "$AppsPath\MSStore\$WinGetAppName" -ItemType Directory -Force | Out-Null + Move-Item -Path "$appFolderPath\*" -Destination "$AppsPath\MSStore\$WinGetAppName" -Force + Remove-Item -Path $appFolderPath -Force + $cmdContent = Get-Content -Path "$AppsPath\InstallAppsandSysprep.cmd" + if ($cmdContent -match 'set "INSTALL_STOREAPPS=false"') { + WriteLog "Setting INSTALL_STOREAPPS flag to true in InstallAppsandSysprep.cmd file." + $updatedcmdContent = $cmdContent -replace 'set "INSTALL_STOREAPPS=false"', 'set "INSTALL_STOREAPPS=true"' + Set-Content -Path "$AppsPath\InstallAppsandSysprep.cmd" -Value $updatedcmdContent + } + # Since a Win32 app was not received, returning false to not increment line number for silent install command + return $false + } + else { + Add-Win32SilentInstallCommand -AppFolder $appFolder -AppFolderPath $appFolderPath -LineNumber $LineNumber + } } function Get-StoreApp { param ( - [string]$StoreApp + [string]$StoreAppName, + [string]$StoreAppId ) - $wingetSearchResult = & winget.exe search --name --exact "$StoreApp" --accept-source-agreements --source msstore + $wingetSearchResult = & winget.exe search "$StoreAppId" --accept-source-agreements --source msstore if ($wingetSearchResult -contains "No package found matching input criteria.") { - WriteLog "$StoreApp not found in WinGet repository. Skipping download." + WriteLog "$StoreAppName not found in WinGet repository. Skipping download." return } # Skip the header lines and get the line with the app information @@ -1806,22 +1833,22 @@ function Get-StoreApp { # Split the line by whitespace and get the second-to-last item (the Id) $appID = ($appResult -split '\s+')[-2] # Checking app ID to determine if store app is a win32 app - WriteLog "Checking if $StoreApp is a win32 app..." + WriteLog "Checking if $StoreAppName is a win32 app..." if ($appID.StartsWith("XP")) { - WriteLog "$StoreApp is a win32 app. Adding to $AppsPath\win32 folder" - $appFolderPath = Join-Path -Path "$AppsPath\win32" -ChildPath $StoreApp + WriteLog "$StoreAppName is a win32 app. Adding to $AppsPath\win32 folder" + $appFolderPath = Join-Path -Path "$AppsPath\win32" -ChildPath $StoreAppName New-Item -Path $appFolderPath -ItemType Directory -Force | Out-Null $appFolder = Split-Path -Path $appFolderPath -Leaf - WriteLog "Downloading $StoreApp for $WindowsArch architecture..." - $wingetDownloadResult = & winget.exe download --name --exact "$StoreApp" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --architecture "$WindowsArch" --scope machine | Out-String + WriteLog "Downloading $StoreAppName for $WindowsArch architecture..." + $wingetDownloadResult = & winget.exe download "$StoreAppId" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --architecture "$WindowsArch" --scope machine | Out-String if ($wingetDownloadResult -match "No applicable installer found") { WriteLog "No installer found for $WindowsArch architecture. Attempting to download without specifying architecture..." - $wingetDownloadResult = & winget.exe download --name --exact "$StoreApp" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --scope machine | Out-String - if ($wingetDownloadResult -match $StoreApp){ - WriteLog "Downloaded $StoreApp without specifying architecture." + $wingetDownloadResult = & winget.exe download "$StoreAppId" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --scope machine | Out-String + if ($wingetDownloadResult -match $StoreAppName){ + WriteLog "Downloaded $StoreAppName without specifying architecture." } else { - WriteLog "No installer found for $StoreApp. Skipping download." + WriteLog "No installer found for $StoreAppName. Skipping download." Remove-Item -Path $appFolderPath -Recurse -Force return } @@ -1829,19 +1856,19 @@ function Get-StoreApp { Add-Win32SilentInstallCommand -AppFolder $appFolder -AppFolderPath $appFolderPath return } - $appFolderPath = Join-Path -Path "$AppsPath\MSStore" -ChildPath $StoreApp + $appFolderPath = Join-Path -Path "$AppsPath\MSStore" -ChildPath $StoreAppName New-Item -Path $appFolderPath -ItemType Directory -Force | Out-Null # Invoke-Process is not used here because it terminates the script if the exit code of the process is not zero. # WinGet's download command will return a non-zero exit code when downloading store apps, as attempting to download the license file always appears to cause an error. - WriteLog "Downloading $StoreApp and dependencies..." + WriteLog "Downloading $StoreAppName and dependencies..." WriteLog 'MSStore app downloads require authentication with an Entra ID account. You may be prompted twice for credentials, once for the app and another for the license file.' - $wingetDownloadResult = & winget.exe download --name --exact "$StoreApp" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --architecture "$WindowsArch" --scope machine | Out-String + $wingetDownloadResult = & winget.exe download "$StoreAppId" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --architecture "$WindowsArch" --scope machine | Out-String # For some apps, specifying the architecture leads to no results found for the app. In those cases, the command will be run without the architecture parameter. if ($wingetDownloadResult -match "No applicable installer found") { WriteLog "No installer found for $WindowsArch architecture. Attempting to download without specifying architecture..." - $wingetDownloadResult = & winget.exe download --name --exact "$StoreApp" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --scope machine | Out-String - if ($wingetDownloadResult -match $StoreApp){ - WriteLog "Downloaded $StoreApp without specifying architecture." + $wingetDownloadResult = & winget.exe download "$StoreAppId" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --scope machine | Out-String + if ($wingetDownloadResult -match $StoreAppName){ + WriteLog "Downloaded $StoreAppName without specifying architecture." # If $WindowsArch -eq 'ARM64', remove all dependency files that are not ARM64 if ($WindowsArch -eq 'ARM64') { WriteLog 'Windows architecture is ARM64. Removing dependencies that are not ARM64.' @@ -1857,14 +1884,14 @@ function Get-StoreApp { } } else { - WriteLog "No installer found for $StoreApp. Skipping download." + WriteLog "No installer found for $StoreAppName. Skipping download." Remove-Item -Path $appFolderPath -Recurse -Force return } } # Many store apps can be found by winget search, but the download of the apps are unsupported. if ($wingetDownloadResult -match "No applicable Microsoft Store package download information found.") { - WriteLog "No applicable Microsoft Store package download information found for $StoreApp. Skipping download." + WriteLog "No applicable Microsoft Store package download information found for $StoreAppName. Skipping download." Remove-Item -Path $appFolderPath -Recurse -Force return } @@ -1874,7 +1901,7 @@ function Get-StoreApp { $updatedcmdContent = $cmdContent -replace 'set "INSTALL_STOREAPPS=false"', 'set "INSTALL_STOREAPPS=true"' Set-Content -Path "$AppsPath\InstallAppsandSysprep.cmd" -Value $updatedcmdContent } - WriteLog "$StoreApp has completed downloading. Identifying the latest version of $StoreApp." + WriteLog "$StoreAppName has completed downloading. Identifying the latest version of $StoreAppName." $packages = Get-ChildItem -Path "$appFolderPath\*" -Exclude "Dependencies\*", "*.xml", "*.yaml" -File -ErrorAction Stop # WinGet downloads multiple versions of certain store apps. The latest version of the package will be determined based on the date of the file signature. $latestPackage = "" @@ -1890,7 +1917,7 @@ function Get-StoreApp { } } # Removing all packages that are not the latest version - WriteLog "Latest version of $StoreApp has been identified as $latestPackage. Removing old versions of $StoreApp that may have downloaded." + WriteLog "Latest version of $StoreAppName has been identified as $latestPackage. Removing old versions of $StoreAppName that may have downloaded." foreach ($package in $packages) { if ($package.FullName -ne $latestPackage) { try { @@ -1909,19 +1936,19 @@ function Get-Apps { param ( [string]$AppsList ) - $apps = Get-Content -Path $AppsList + $apps = Get-Content -Path $AppsList -Raw | ConvertFrom-Json if (-not $apps) { WriteLog "No apps were specified in AppsList.txt file." return } $wingetApps = @() $storeApps = @() - $apps | ForEach-Object { - if ($_ -like 'winget:*') { - $wingetApps += $_.Substring(7).Trim() - } - elseif ($_ -like 'store:*') { - $storeApps += $_.Substring(6).Trim() + foreach ($app in $apps.apps) { + if ($app.source -eq "winget") { + $wingetApps += $app + } + elseif ($app.source -eq "msstore") { + $storeApps += $app } } $wingetInstalled = Get-ChildItem -Path "$env:LOCALAPPDATA\Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\winget.exe" -ErrorAction SilentlyContinue @@ -1949,8 +1976,10 @@ function Get-Apps { } foreach ($wingetApp in $wingetApps) { try { - Get-WinGetApp -WinGetApp $wingetApp -LineNumber $lineNumber - $lineNumber++ + $result = Get-WinGetApp -WinGetAppName $wingetApp.Name -WinGetAppId $wingetApp.Id -LineNumber $lineNumber + if ($null -eq $result) { + $lineNumber++ + } } catch { WriteLog "Error occurred while processing $wingetApp : $_" @@ -1965,7 +1994,7 @@ function Get-Apps { } foreach ($storeApp in $storeApps) { try { - Get-StoreApp -StoreApp $storeApp + Get-StoreApp -StoreAppName $storeApps.Name -StoreAppId $storeApps.Id } catch { WriteLog "Error occurred while processing $storeApp : $_" @@ -3383,7 +3412,7 @@ if ($InstallApps) { exit } WriteLog "$AppsPath\InstallAppsandSysprep.cmd found" - Get-Apps -AppsList "$AppsPath\AppsList.txt" + Get-Apps -AppsList "$AppsPath\AppList.json" if (-not $InstallOffice) { #Modify InstallAppsandSysprep.cmd to REM out the office install command $CmdContent = Get-Content -Path "$AppsPath\InstallAppsandSysprep.cmd" From 146c1601bd47474b2d9997d8603f2a98ea5f764d Mon Sep 17 00:00:00 2001 From: Zehadi Alam <63765084+zehadialam@users.noreply.github.com> Date: Fri, 5 Jul 2024 00:01:06 -0400 Subject: [PATCH 2/9] Improved handling of store apps that can't be downloaded --- FFUDevelopment/BuildFFUVM.ps1 | 38 ++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/FFUDevelopment/BuildFFUVM.ps1 b/FFUDevelopment/BuildFFUVM.ps1 index f0dadac..d176963 100644 --- a/FFUDevelopment/BuildFFUVM.ps1 +++ b/FFUDevelopment/BuildFFUVM.ps1 @@ -1738,7 +1738,7 @@ function Add-Win32SilentInstallCommand { param ( [string]$AppFolder, [string]$AppFolderPath, - [string]$LineNumber + [int]$LineNumber ) $appName = $AppFolder $installerPath = Get-ChildItem -Path "$appFolderPath\*" -Include "*.exe", "*.msi" -File -ErrorAction Stop @@ -1821,7 +1821,8 @@ function Get-WinGetApp { function Get-StoreApp { param ( [string]$StoreAppName, - [string]$StoreAppId + [string]$StoreAppId, + [int]$LineNumber ) $wingetSearchResult = & winget.exe search "$StoreAppId" --accept-source-agreements --source msstore if ($wingetSearchResult -contains "No package found matching input criteria.") { @@ -1853,21 +1854,33 @@ function Get-StoreApp { return } } - Add-Win32SilentInstallCommand -AppFolder $appFolder -AppFolderPath $appFolderPath - return + Add-Win32SilentInstallCommand -AppFolder $appFolder -AppFolderPath $appFolderPath -LineNumber $LineNumber + # Since a Win32 app was received, returning false to increment line number for silent install command + return $false } $appFolderPath = Join-Path -Path "$AppsPath\MSStore" -ChildPath $StoreAppName New-Item -Path $appFolderPath -ItemType Directory -Force | Out-Null # Invoke-Process is not used here because it terminates the script if the exit code of the process is not zero. # WinGet's download command will return a non-zero exit code when downloading store apps, as attempting to download the license file always appears to cause an error. - WriteLog "Downloading $StoreAppName and dependencies..." + WriteLog "Attempting to download $StoreAppName and dependencies..." WriteLog 'MSStore app downloads require authentication with an Entra ID account. You may be prompted twice for credentials, once for the app and another for the license file.' $wingetDownloadResult = & winget.exe download "$StoreAppId" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --architecture "$WindowsArch" --scope machine | Out-String + if ($wingetDownloadResult -match "The request is not supported") { + WriteLog "The download request is not supported for $StoreAppName. Skipping download." + Remove-Item -Path $appFolderPath -Recurse -Force + return + } + # Many store apps can be found by winget search, but the download of the apps are unsupported. + if ($wingetDownloadResult -match "No applicable Microsoft Store package download information found.") { + WriteLog "No applicable Microsoft Store package download information found for $StoreAppName. Skipping download." + Remove-Item -Path $appFolderPath -Recurse -Force + return + } # For some apps, specifying the architecture leads to no results found for the app. In those cases, the command will be run without the architecture parameter. if ($wingetDownloadResult -match "No applicable installer found") { WriteLog "No installer found for $WindowsArch architecture. Attempting to download without specifying architecture..." $wingetDownloadResult = & winget.exe download "$StoreAppId" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --scope machine | Out-String - if ($wingetDownloadResult -match $StoreAppName){ + if ($wingetDownloadResult -match "Installer downloaded") { WriteLog "Downloaded $StoreAppName without specifying architecture." # If $WindowsArch -eq 'ARM64', remove all dependency files that are not ARM64 if ($WindowsArch -eq 'ARM64') { @@ -1884,17 +1897,11 @@ function Get-StoreApp { } } else { - WriteLog "No installer found for $StoreAppName. Skipping download." + WriteLog "No installer found for $StoreAppName from the msstore source. Skipping download." Remove-Item -Path $appFolderPath -Recurse -Force return } } - # Many store apps can be found by winget search, but the download of the apps are unsupported. - if ($wingetDownloadResult -match "No applicable Microsoft Store package download information found.") { - WriteLog "No applicable Microsoft Store package download information found for $StoreAppName. Skipping download." - Remove-Item -Path $appFolderPath -Recurse -Force - return - } $cmdContent = Get-Content -Path "$AppsPath\InstallAppsandSysprep.cmd" if ($cmdContent -match 'set "INSTALL_STOREAPPS=false"') { WriteLog "Setting INSTALL_STOREAPPS flag to true in InstallAppsandSysprep.cmd file." @@ -1994,7 +2001,10 @@ function Get-Apps { } foreach ($storeApp in $storeApps) { try { - Get-StoreApp -StoreAppName $storeApps.Name -StoreAppId $storeApps.Id + $result = Get-StoreApp -StoreAppName $storeApp.Name -StoreAppId $storeApp.Id -LineNumber $lineNumber + if ($result -eq $false) { + $lineNumber++ + } } catch { WriteLog "Error occurred while processing $storeApp : $_" From 325413de13a09bf1f4a4acbf62744e3b806e5798 Mon Sep 17 00:00:00 2001 From: Zehadi Alam <63765084+zehadialam@users.noreply.github.com> Date: Sun, 7 Jul 2024 22:27:27 -0400 Subject: [PATCH 3/9] Moved code into separate functions, refactored existing functions, fixed logical errors in if-statements --- FFUDevelopment/BuildFFUVM.ps1 | 142 ++++++++++++++-------------------- 1 file changed, 57 insertions(+), 85 deletions(-) diff --git a/FFUDevelopment/BuildFFUVM.ps1 b/FFUDevelopment/BuildFFUVM.ps1 index d176963..0b88fbc 100644 --- a/FFUDevelopment/BuildFFUVM.ps1 +++ b/FFUDevelopment/BuildFFUVM.ps1 @@ -1695,6 +1695,22 @@ function Install-WinGet { } } +function Confirm-WinGetInstallation { + $wingetPath = "$env:LOCALAPPDATA\Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\winget.exe" + if (-not (Test-Path $wingetPath)) { + WriteLog "WinGet is not installed. Downloading preview version of WinGet and its dependencies..." + Install-WinGet -InstallWithDependencies $true + } + elseif (-not (Get-Command winget -ErrorAction SilentlyContinue)) { + WriteLog "WinGet is not on the path. Downloading preview version of WinGet without dependencies..." + Install-WinGet -InstallWithDependencies $false + } + elseif (-not ((& winget.exe --version) -like "*preview*")) { + WriteLog "The preview version of WinGet is not installed. Downloading preview version of WinGet without dependencies..." + Install-WinGet -InstallWithDependencies $false + } +} + function New-WinGetSettings { $wingetSettingsFile = "$env:LOCALAPPDATA\Packages\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\LocalState\settings.json" $wingetSettings = @( @@ -1742,28 +1758,26 @@ function Add-Win32SilentInstallCommand { ) $appName = $AppFolder $installerPath = Get-ChildItem -Path "$appFolderPath\*" -Include "*.exe", "*.msi" -File -ErrorAction Stop - $installer = Split-Path -Path $installerPath -Leaf + if (-not $installerPath) { + WriteLog "No win32 app installers were found. Skipping the inclusion of $AppFolder" + Remove-Item -Path $AppFolderPath -Recurse -Force + return $false + } $yamlFile = Get-ChildItem -Path "$appFolderPath\*" -Include "*.yaml" -File -ErrorAction Stop $yamlContent = Get-Content -Path $yamlFile -Raw - $silentInstallSwitch = [regex]::Match($yamlContent, 'Silent:\s*(.+)').Groups[1].Value - $silentInstallSwitch = $silentInstallSwitch.Replace("'", "").Trim() + $silentInstallSwitch = [regex]::Match($yamlContent, 'Silent:\s*(.+)').Groups[1].Value.Replace("'", "").Trim() if (-not $silentInstallSwitch) { WriteLog "Silent install switch for $appName could not be found. Skipping the inclusion of $appName." Remove-Item -Path $appFolderPath -Recurse -Force - return + return $false } - $installerFileExtension = [System.IO.Path]::GetExtension($installer) - if ($installerFileExtension -eq ".exe") { + $installer = Split-Path -Path $installerPath -Leaf + if ($installerPath.Extension -eq ".exe") { $silentInstallCommand = "`"D:\win32\$appFolder\$installer`" $silentInstallSwitch" } - elseif ($installerFileExtension -eq ".msi") { + elseif ($installerPath.Extension -eq ".msi") { $silentInstallCommand = "msiexec /i `"D:\win32\$appFolder\$installer`" $silentInstallSwitch" } - else { - WriteLog "No win32 app installers were found. Skipping the inclusion of $appName" - Remove-Item -Path $AppFolderPath -Recurse -Force - return - } $cmdFile = "$AppsPath\InstallAppsandSysprep.cmd" $cmdContent = Get-Content -Path $cmdFile $cmdContent = $cmdContent[0..($lineNumber - 2)] + $silentInstallCommand.Trim() + $cmdContent[($lineNumber - 1)..($cmdContent.Length - 1)] @@ -1771,6 +1785,16 @@ function Add-Win32SilentInstallCommand { Set-Content -Path $cmdFile -Value $cmdContent } +function Set-InstallStoreAppsFlag { + $cmdPath = "$AppsPath\InstallAppsandSysprep.cmd" + $cmdContent = Get-Content -Path $cmdPath + if ($cmdContent -match 'set "INSTALL_STOREAPPS=false"') { + WriteLog "Setting INSTALL_STOREAPPS flag to true in InstallAppsandSysprep.cmd file." + $updatedcmdContent = $cmdContent -replace 'set "INSTALL_STOREAPPS=false"', 'set "INSTALL_STOREAPPS=true"' + Set-Content -Path "$AppsPath\InstallAppsandSysprep.cmd" -Value $updatedcmdContent + } +} + function Get-WinGetApp { param ( [string]$WinGetAppName, @@ -1780,11 +1804,11 @@ function Get-WinGetApp { $wingetSearchResult = & winget.exe search --id "$WinGetAppId" --exact --accept-source-agreements --source winget if ($wingetSearchResult -contains "No package found matching input criteria.") { WriteLog "$WinGetAppName not found in WinGet repository. Skipping download." - return + # Return false to not increment line number for silent install command. + return $false } $appFolderPath = Join-Path -Path "$AppsPath\Win32" -ChildPath $WinGetAppName New-Item -Path $appFolderPath -ItemType Directory -Force | Out-Null - $appFolder = Split-Path -Path $appFolderPath -Leaf WriteLog "Downloading $WinGetAppName..." $wingetDownloadResult = & winget.exe download --id "$WinGetAppId" --exact --download-directory "$appFolderPath" --scope machine --source winget --architecture "$WindowsArch" | Out-String if ($wingetDownloadResult -match "No applicable installer found") { @@ -1793,28 +1817,20 @@ function Get-WinGetApp { if ($wingetDownloadResult -notmatch "Installer downloaded") { WriteLog "$WinGetAppName did not successfully download." Remove-Item -Path $appFolderPath -Recurse -Force - return + return $false } WriteLog "$WinGetAppName has completed downloading to $appFolderPath" $installerPath = Get-ChildItem -Path "$appFolderPath\*" -Exclude "*.yaml", "*.xml" -File -ErrorAction Stop - $installer = Split-Path -Path $installerPath -Leaf - $installerFileExtension = [System.IO.Path]::GetExtension($installer) $uwpExtensions = @(".appx", ".appxbundle", ".msix", ".msixbundle") - if ($uwpExtensions -contains $installerFileExtension) { + if ($uwpExtensions -contains $installerPath.Extension) { New-Item -Path "$AppsPath\MSStore\$WinGetAppName" -ItemType Directory -Force | Out-Null Move-Item -Path "$appFolderPath\*" -Destination "$AppsPath\MSStore\$WinGetAppName" -Force Remove-Item -Path $appFolderPath -Force - $cmdContent = Get-Content -Path "$AppsPath\InstallAppsandSysprep.cmd" - if ($cmdContent -match 'set "INSTALL_STOREAPPS=false"') { - WriteLog "Setting INSTALL_STOREAPPS flag to true in InstallAppsandSysprep.cmd file." - $updatedcmdContent = $cmdContent -replace 'set "INSTALL_STOREAPPS=false"', 'set "INSTALL_STOREAPPS=true"' - Set-Content -Path "$AppsPath\InstallAppsandSysprep.cmd" -Value $updatedcmdContent - } - # Since a Win32 app was not received, returning false to not increment line number for silent install command + Set-InstallStoreAppsFlag return $false } else { - Add-Win32SilentInstallCommand -AppFolder $appFolder -AppFolderPath $appFolderPath -LineNumber $LineNumber + Add-Win32SilentInstallCommand -AppFolder $WinGetAppName -AppFolderPath $appFolderPath -LineNumber $LineNumber } } @@ -1839,13 +1855,12 @@ function Get-StoreApp { WriteLog "$StoreAppName is a win32 app. Adding to $AppsPath\win32 folder" $appFolderPath = Join-Path -Path "$AppsPath\win32" -ChildPath $StoreAppName New-Item -Path $appFolderPath -ItemType Directory -Force | Out-Null - $appFolder = Split-Path -Path $appFolderPath -Leaf WriteLog "Downloading $StoreAppName for $WindowsArch architecture..." $wingetDownloadResult = & winget.exe download "$StoreAppId" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --architecture "$WindowsArch" --scope machine | Out-String if ($wingetDownloadResult -match "No applicable installer found") { WriteLog "No installer found for $WindowsArch architecture. Attempting to download without specifying architecture..." $wingetDownloadResult = & winget.exe download "$StoreAppId" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --scope machine | Out-String - if ($wingetDownloadResult -match $StoreAppName){ + if ($wingetDownloadResult -match "Installer downloaded"){ WriteLog "Downloaded $StoreAppName without specifying architecture." } else { @@ -1854,7 +1869,7 @@ function Get-StoreApp { return } } - Add-Win32SilentInstallCommand -AppFolder $appFolder -AppFolderPath $appFolderPath -LineNumber $LineNumber + Add-Win32SilentInstallCommand -AppFolder $StoreAppName -AppFolderPath $appFolderPath -LineNumber $LineNumber # Since a Win32 app was received, returning false to increment line number for silent install command return $false } @@ -1865,22 +1880,11 @@ function Get-StoreApp { WriteLog "Attempting to download $StoreAppName and dependencies..." WriteLog 'MSStore app downloads require authentication with an Entra ID account. You may be prompted twice for credentials, once for the app and another for the license file.' $wingetDownloadResult = & winget.exe download "$StoreAppId" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --architecture "$WindowsArch" --scope machine | Out-String - if ($wingetDownloadResult -match "The request is not supported") { - WriteLog "The download request is not supported for $StoreAppName. Skipping download." - Remove-Item -Path $appFolderPath -Recurse -Force - return - } - # Many store apps can be found by winget search, but the download of the apps are unsupported. - if ($wingetDownloadResult -match "No applicable Microsoft Store package download information found.") { - WriteLog "No applicable Microsoft Store package download information found for $StoreAppName. Skipping download." - Remove-Item -Path $appFolderPath -Recurse -Force - return - } # For some apps, specifying the architecture leads to no results found for the app. In those cases, the command will be run without the architecture parameter. if ($wingetDownloadResult -match "No applicable installer found") { WriteLog "No installer found for $WindowsArch architecture. Attempting to download without specifying architecture..." $wingetDownloadResult = & winget.exe download "$StoreAppId" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --scope machine | Out-String - if ($wingetDownloadResult -match "Installer downloaded") { + if ($wingetDownloadResult -match "Microsoft Store package download completed") { WriteLog "Downloaded $StoreAppName without specifying architecture." # If $WindowsArch -eq 'ARM64', remove all dependency files that are not ARM64 if ($WindowsArch -eq 'ARM64') { @@ -1902,27 +1906,16 @@ function Get-StoreApp { return } } - $cmdContent = Get-Content -Path "$AppsPath\InstallAppsandSysprep.cmd" - if ($cmdContent -match 'set "INSTALL_STOREAPPS=false"') { - WriteLog "Setting INSTALL_STOREAPPS flag to true in InstallAppsandSysprep.cmd file." - $updatedcmdContent = $cmdContent -replace 'set "INSTALL_STOREAPPS=false"', 'set "INSTALL_STOREAPPS=true"' - Set-Content -Path "$AppsPath\InstallAppsandSysprep.cmd" -Value $updatedcmdContent + elseif ($wingetDownloadResult -notmatch "Microsoft Store package download completed") { + WriteLog "Download not supported for $StoreAppName. Skipping download." + Remove-Item -Path $appFolderPath -Recurse -Force + return } + Set-InstallStoreAppsFlag WriteLog "$StoreAppName has completed downloading. Identifying the latest version of $StoreAppName." $packages = Get-ChildItem -Path "$appFolderPath\*" -Exclude "Dependencies\*", "*.xml", "*.yaml" -File -ErrorAction Stop # WinGet downloads multiple versions of certain store apps. The latest version of the package will be determined based on the date of the file signature. - $latestPackage = "" - $latestDate = [datetime]::MinValue - foreach ($package in $packages) { - $signature = Get-AuthenticodeSignature -FilePath $package.FullName - if ($signature.Status -eq 'Valid') { - $signatureDate = $signature.SignerCertificate.NotBefore - if ($signatureDate -gt $latestDate) { - $latestPackage = $package.FullName - $latestDate = $signatureDate - } - } - } + $latestPackage = $packages | Sort-Object { (Get-AuthenticodeSignature $_.FullName).SignerCertificate.NotBefore } -Descending | Select-Object -First 1 # Removing all packages that are not the latest version WriteLog "Latest version of $StoreAppName has been identified as $latestPackage. Removing old versions of $StoreAppName that may have downloaded." foreach ($package in $packages) { @@ -1945,35 +1938,12 @@ function Get-Apps { ) $apps = Get-Content -Path $AppsList -Raw | ConvertFrom-Json if (-not $apps) { - WriteLog "No apps were specified in AppsList.txt file." + WriteLog "No apps were specified in AppList.json file." return } - $wingetApps = @() - $storeApps = @() - foreach ($app in $apps.apps) { - if ($app.source -eq "winget") { - $wingetApps += $app - } - elseif ($app.source -eq "msstore") { - $storeApps += $app - } - } - $wingetInstalled = Get-ChildItem -Path "$env:LOCALAPPDATA\Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\winget.exe" -ErrorAction SilentlyContinue - if (-not $wingetInstalled) { - WriteLog "WinGet is not installed. Downloading preview version of WinGet and its dependencies..." - Install-WinGet -InstallWithDependencies $true - } - $wingetOnPath = Get-Command winget -ErrorAction SilentlyContinue - if (-not $wingetOnPath) { - WriteLog "WinGet is not on the path. Downloading preview version of WinGet without dependencies..." - Install-WinGet -InstallWithDependencies $false - } - $wingetVersion = & winget.exe --version - # Preview release is needed to enable storeDownload experimental feature - if (-not ($wingetVersion -like "*preview*")) { - WriteLog "The preview version of WinGet is not installed. Downloading preview version of WinGet without dependencies..." - Install-WinGet -InstallWithDependencies $false - } + $wingetApps = $apps.apps | Where-Object { $_.source -eq "winget" } + $storeApps = $apps.apps | Where-Object { $_.source -eq "msstore" } + Confirm-WinGetInstallation $lineNumber = 13 $win32Folder = Join-Path -Path $AppsPath -ChildPath "Win32" $storeAppsFolder = Join-Path -Path $AppsPath -ChildPath "MSStore" @@ -1986,6 +1956,7 @@ function Get-Apps { $result = Get-WinGetApp -WinGetAppName $wingetApp.Name -WinGetAppId $wingetApp.Id -LineNumber $lineNumber if ($null -eq $result) { $lineNumber++ + WriteLog "Line number incremented to $lineNumber" } } catch { @@ -2004,6 +1975,7 @@ function Get-Apps { $result = Get-StoreApp -StoreAppName $storeApp.Name -StoreAppId $storeApp.Id -LineNumber $lineNumber if ($result -eq $false) { $lineNumber++ + WriteLog "Line number incremented to $lineNumber" } } catch { From a9afba918557c816ee00088e5f108b77c25dce5a Mon Sep 17 00:00:00 2001 From: Zehadi Alam <63765084+zehadialam@users.noreply.github.com> Date: Thu, 11 Jul 2024 23:32:58 -0400 Subject: [PATCH 4/9] Refactor Install-WinGet and New-WinGetSettings for improved readability --- FFUDevelopment/BuildFFUVM.ps1 | 103 +++++++++++++++++----------------- 1 file changed, 51 insertions(+), 52 deletions(-) diff --git a/FFUDevelopment/BuildFFUVM.ps1 b/FFUDevelopment/BuildFFUVM.ps1 index 0b88fbc..d64bcbc 100644 --- a/FFUDevelopment/BuildFFUVM.ps1 +++ b/FFUDevelopment/BuildFFUVM.ps1 @@ -1659,55 +1659,48 @@ function Get-Office { } function Install-WinGet { - param ( - [bool]$InstallWithDependencies - ) $wingetPreviewLink = "https://aka.ms/getwingetpreview" $wingetPackageDestination = "$env:TEMP\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" - if ($InstallWithDependencies) { - $dependencies = @( - @{ - Source = "https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx" - Destination = "$env:TEMP\Microsoft.VCLibs.x64.14.00.Desktop.appx" - }, - @{ - Source = "https://github.com/microsoft/microsoft-ui-xaml/releases/download/v2.8.6/Microsoft.UI.Xaml.2.8.x64.appx" - Destination = "$env:TEMP\Microsoft.UI.Xaml.2.8.x64.appx" - } - ) - Start-BitsTransferWithRetry -Source $wingetPreviewLink -Destination $wingetPackageDestination - foreach ($dependency in $dependencies) { - Start-BitsTransferWithRetry -Source $dependency.Source -Destination $dependency.Destination - Add-AppxPackage -Path $dependency.Destination - Remove-Item -Path $dependency.Destination -Force -ErrorAction SilentlyContinue + $dependencies = @( + @{ + Source = "https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx" + Destination = "$env:TEMP\Microsoft.VCLibs.x64.14.00.Desktop.appx" + }, + @{ + Source = "https://github.com/microsoft/microsoft-ui-xaml/releases/download/v2.8.6/Microsoft.UI.Xaml.2.8.x64.appx" + Destination = "$env:TEMP\Microsoft.UI.Xaml.2.8.x64.appx" } - Add-AppxPackage -Path $wingetPackageDestination - Remove-Item -Path $wingetPackageDestination -Force -ErrorAction SilentlyContinue - } - else { - # If WinGet was already installed, then installing the dependencies can cause an error if the system has a newer version of the dependencies than the ones downloaded. - WriteLog "Downloading WinGet..." - Start-BitsTransferWithRetry -Source $wingetPreviewLink -Destination $wingetPackageDestination - WriteLog "Installing WinGet..." - Add-AppxPackage -Path $wingetPackageDestination - WriteLog "Removing WinGet installer..." - Remove-Item -Path $wingetPackageDestination -Force -ErrorAction SilentlyContinue + ) + foreach ($dependency in $dependencies) { + $dependencyName = [System.IO.Path]::GetFileName($dependency.Source) + WriteLog "Downloading $dependencyName..." + Start-BitsTransferWithRetry -Source $dependency.Source -Destination $dependency.Destination + WriteLog "Installing $dependencyName..." + Add-AppxPackage -Path $dependency.Destination -ErrorAction SilentlyContinue + WriteLog "Removing $dependencyName..." + Remove-Item -Path $dependency.Destination -Force -ErrorAction SilentlyContinue } + WriteLog "Downloading WinGet..." + Start-BitsTransferWithRetry -Source $wingetPreviewLink -Destination $wingetPackageDestination + WriteLog "Installing WinGet..." + Add-AppxPackage -Path $wingetPackageDestination -ErrorAction SilentlyContinue + WriteLog "Removing WinGet installer..." + Remove-Item -Path $wingetPackageDestination -Force -ErrorAction SilentlyContinue } function Confirm-WinGetInstallation { $wingetPath = "$env:LOCALAPPDATA\Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\winget.exe" if (-not (Test-Path $wingetPath)) { - WriteLog "WinGet is not installed. Downloading preview version of WinGet and its dependencies..." - Install-WinGet -InstallWithDependencies $true + WriteLog "WinGet is not installed. Downloading preview version of WinGet..." + Install-WinGet } elseif (-not (Get-Command winget -ErrorAction SilentlyContinue)) { - WriteLog "WinGet is not on the path. Downloading preview version of WinGet without dependencies..." - Install-WinGet -InstallWithDependencies $false + WriteLog "WinGet is not on the path. Downloading preview version of WinGet..." + Install-WinGet } elseif (-not ((& winget.exe --version) -like "*preview*")) { - WriteLog "The preview version of WinGet is not installed. Downloading preview version of WinGet without dependencies..." - Install-WinGet -InstallWithDependencies $false + WriteLog "The preview version of WinGet is not installed. Downloading preview version of WinGet..." + Install-WinGet } } @@ -1730,24 +1723,21 @@ function New-WinGetSettings { if (Test-Path -Path $wingetSettingsFile -PathType Leaf) { $jsonContent = Get-Content -Path $wingetSettingsFile -Raw # Check if storeDownload feature is already enabled - if ($jsonContent -notmatch '"storeDownload"\s*:\s*true') { - # Back up existing settings.json file - $backupWingetSettingsFile = $wingetSettingsFile + ".bak" - if (-not (Test-Path -Path $backupWingetSettingsFile -PathType Leaf)) { - WriteLog "Backing up existing WinGet settings.json file to $backupWingetSettingsFile" - Copy-Item -Path $wingetSettingsFile -Destination $backupWingetSettingsFile -Force | Out-Null - } - WriteLog "Creating WinGet settings.json file to allow the storeDownload feature. Writing file to $wingetSettingsFile" - $wingetSettingsContent | Out-File -FilePath $wingetSettingsFile -Encoding utf8 -Force - } - else { + WriteLog "Checking if storeDownload feature is enabled in WinGet configuration." + if ($jsonContent -match '"storeDownload"\s*:\s*true') { WriteLog "WinGet's settings.json file is already configured to enable the storeDownload feature." + return + } + # Back up existing settings.json file + WriteLog "The storeDownload feature is not enabled in WinGet configuration." + $backupWingetSettingsFile = $wingetSettingsFile + ".bak" + if (-not (Test-Path -Path $backupWingetSettingsFile -PathType Leaf)) { + WriteLog "Backing up existing WinGet settings.json file to $backupWingetSettingsFile" + Copy-Item -Path $wingetSettingsFile -Destination $backupWingetSettingsFile -Force | Out-Null } } - else { - WriteLog "Creating WinGet settings.json file to allow the storeDownload feature. Writing file to $wingetSettingsFile" - $wingetSettingsContent | Out-File -FilePath $wingetSettingsFile -Encoding utf8 -Force - } + WriteLog "Creating WinGet settings.json file to allow the storeDownload feature. Writing file to $wingetSettingsFile" + $wingetSettingsContent | Out-File -FilePath $wingetSettingsFile -Encoding utf8 -Force } function Add-Win32SilentInstallCommand { @@ -1812,10 +1802,19 @@ function Get-WinGetApp { WriteLog "Downloading $WinGetAppName..." $wingetDownloadResult = & winget.exe download --id "$WinGetAppId" --exact --download-directory "$appFolderPath" --scope machine --source winget --architecture "$WindowsArch" | Out-String if ($wingetDownloadResult -match "No applicable installer found") { + WriteLog "No installer found for $WindowsArch architecture. Attempting to download without specifying architecture..." $wingetDownloadResult = & winget.exe download --id "$WinGetAppId" --exact --download-directory "$appFolderPath" --scope machine --source winget | Out-String + if ($wingetDownloadResult -match "Installer downloaded") { + WriteLog "Downloaded $WinGetAppName without specifying architecture." + } + else { + WriteLog "No installer found for $WinGetAppName. Skipping download." + Remove-Item -Path $appFolderPath -Recurse -Force + return $false + } } if ($wingetDownloadResult -notmatch "Installer downloaded") { - WriteLog "$WinGetAppName did not successfully download." + WriteLog "No installer found for $WinGetAppName. Skipping download." Remove-Item -Path $appFolderPath -Recurse -Force return $false } From f7f52903a44563af92713eb518d9442babf2b56d Mon Sep 17 00:00:00 2001 From: Zehadi Alam <63765084+zehadialam@users.noreply.github.com> Date: Fri, 12 Jul 2024 00:38:48 -0400 Subject: [PATCH 5/9] Refactor Get-StoreApp for improved readability --- FFUDevelopment/BuildFFUVM.ps1 | 94 ++++++++++++++++------------------- 1 file changed, 44 insertions(+), 50 deletions(-) diff --git a/FFUDevelopment/BuildFFUVM.ps1 b/FFUDevelopment/BuildFFUVM.ps1 index d64bcbc..407e9a2 100644 --- a/FFUDevelopment/BuildFFUVM.ps1 +++ b/FFUDevelopment/BuildFFUVM.ps1 @@ -1850,67 +1850,61 @@ function Get-StoreApp { $appID = ($appResult -split '\s+')[-2] # Checking app ID to determine if store app is a win32 app WriteLog "Checking if $StoreAppName is a win32 app..." - if ($appID.StartsWith("XP")) { + $appIsWin32 = $appID.StartsWith("XP") + if ($appIsWin32) { WriteLog "$StoreAppName is a win32 app. Adding to $AppsPath\win32 folder" $appFolderPath = Join-Path -Path "$AppsPath\win32" -ChildPath $StoreAppName - New-Item -Path $appFolderPath -ItemType Directory -Force | Out-Null - WriteLog "Downloading $StoreAppName for $WindowsArch architecture..." - $wingetDownloadResult = & winget.exe download "$StoreAppId" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --architecture "$WindowsArch" --scope machine | Out-String - if ($wingetDownloadResult -match "No applicable installer found") { - WriteLog "No installer found for $WindowsArch architecture. Attempting to download without specifying architecture..." - $wingetDownloadResult = & winget.exe download "$StoreAppId" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --scope machine | Out-String - if ($wingetDownloadResult -match "Installer downloaded"){ - WriteLog "Downloaded $StoreAppName without specifying architecture." - } - else { - WriteLog "No installer found for $StoreAppName. Skipping download." - Remove-Item -Path $appFolderPath -Recurse -Force - return - } + } + else { + $appFolderPath = Join-Path -Path "$AppsPath\MSStore" -ChildPath $StoreAppName + } + New-Item -Path $appFolderPath -ItemType Directory -Force | Out-Null + WriteLog "Downloading $StoreAppName for $WindowsArch architecture..." + $downloadParams = @( + "download", "$StoreAppId", + "--download-directory", "$appFolderPath", + "--accept-package-agreements", + "--accept-source-agreements", + "--source", "msstore", + "--scope", "machine", + "--architecture", "$WindowsArch" + ) + WriteLog 'MSStore app downloads require authentication with an Entra ID account. You may be prompted twice for credentials, once for the app and another for the license file.' + WriteLog "Attempting to download $StoreAppName and dependencies for $WindowsArch architecture..." + $wingetDownloadResult = & winget.exe @downloadParams | Out-String + # For some apps, specifying the architecture leads to no results found for the app. In those cases, the command will be run without the architecture parameter. + if ($wingetDownloadResult -match "No applicable installer found") { + WriteLog "No installer found for $WindowsArch architecture. Attempting to download without specifying architecture..." + $downloadParams = $downloadParams | Where-Object { $_ -notmatch "--architecture" -and $_ -notmatch "$WindowsArch" } + $wingetDownloadResult = & winget.exe @downloadParams | Out-String + if ($wingetDownloadResult -match "Microsoft Store package download completed") { + WriteLog "Downloaded $StoreAppName without specifying architecture." } + } + if ($wingetDownloadResult -notmatch "Installer downloaded|Microsoft Store package download completed") { + WriteLog "Download not supported for $StoreAppName. Skipping download." + Remove-Item -Path $appFolderPath -Recurse -Force + return + } + if ($appIsWin32) { Add-Win32SilentInstallCommand -AppFolder $StoreAppName -AppFolderPath $appFolderPath -LineNumber $LineNumber # Since a Win32 app was received, returning false to increment line number for silent install command return $false } - $appFolderPath = Join-Path -Path "$AppsPath\MSStore" -ChildPath $StoreAppName - New-Item -Path $appFolderPath -ItemType Directory -Force | Out-Null - # Invoke-Process is not used here because it terminates the script if the exit code of the process is not zero. - # WinGet's download command will return a non-zero exit code when downloading store apps, as attempting to download the license file always appears to cause an error. - WriteLog "Attempting to download $StoreAppName and dependencies..." - WriteLog 'MSStore app downloads require authentication with an Entra ID account. You may be prompted twice for credentials, once for the app and another for the license file.' - $wingetDownloadResult = & winget.exe download "$StoreAppId" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --architecture "$WindowsArch" --scope machine | Out-String - # For some apps, specifying the architecture leads to no results found for the app. In those cases, the command will be run without the architecture parameter. - if ($wingetDownloadResult -match "No applicable installer found") { - WriteLog "No installer found for $WindowsArch architecture. Attempting to download without specifying architecture..." - $wingetDownloadResult = & winget.exe download "$StoreAppId" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --scope machine | Out-String - if ($wingetDownloadResult -match "Microsoft Store package download completed") { - WriteLog "Downloaded $StoreAppName without specifying architecture." - # If $WindowsArch -eq 'ARM64', remove all dependency files that are not ARM64 - if ($WindowsArch -eq 'ARM64') { - WriteLog 'Windows architecture is ARM64. Removing dependencies that are not ARM64.' - $dependencies = Get-ChildItem -Path "$appFolderPath\Dependencies" -ErrorAction SilentlyContinue - if ($dependencies) { - foreach ($dependency in $dependencies) { - if ($dependency.Name -notmatch 'ARM64') { - WriteLog "Removing dependency file $($dependency.FullName)" - Remove-Item -Path $dependency.FullName -Recurse -Force - } - } + Set-InstallStoreAppsFlag + # If $WindowsArch -eq 'ARM64', remove all dependency files that are not ARM64 + if ($WindowsArch -eq 'ARM64') { + WriteLog 'Windows architecture is ARM64. Removing dependencies that are not ARM64.' + $dependencies = Get-ChildItem -Path "$appFolderPath\Dependencies" -ErrorAction SilentlyContinue + if ($dependencies) { + foreach ($dependency in $dependencies) { + if ($dependency.Name -notmatch 'ARM64') { + WriteLog "Removing dependency file $($dependency.FullName)" + Remove-Item -Path $dependency.FullName -Recurse -Force } } } - else { - WriteLog "No installer found for $StoreAppName from the msstore source. Skipping download." - Remove-Item -Path $appFolderPath -Recurse -Force - return - } } - elseif ($wingetDownloadResult -notmatch "Microsoft Store package download completed") { - WriteLog "Download not supported for $StoreAppName. Skipping download." - Remove-Item -Path $appFolderPath -Recurse -Force - return - } - Set-InstallStoreAppsFlag WriteLog "$StoreAppName has completed downloading. Identifying the latest version of $StoreAppName." $packages = Get-ChildItem -Path "$appFolderPath\*" -Exclude "Dependencies\*", "*.xml", "*.yaml" -File -ErrorAction Stop # WinGet downloads multiple versions of certain store apps. The latest version of the package will be determined based on the date of the file signature. From 1a444d8e0faa0cde465fe886df24a9d0556ec2b5 Mon Sep 17 00:00:00 2001 From: Zehadi Alam <63765084+zehadialam@users.noreply.github.com> Date: Fri, 12 Jul 2024 22:40:43 -0400 Subject: [PATCH 6/9] Refactor Install-WinGet function and add architecture parameter and stable WinGet download, modify Confirm-WinGetInstallation to check for stable release, refactor Get-WinGetApp for improved maintainability and readability --- FFUDevelopment/BuildFFUVM.ps1 | 87 ++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/FFUDevelopment/BuildFFUVM.ps1 b/FFUDevelopment/BuildFFUVM.ps1 index 407e9a2..f7e108c 100644 --- a/FFUDevelopment/BuildFFUVM.ps1 +++ b/FFUDevelopment/BuildFFUVM.ps1 @@ -1659,48 +1659,44 @@ function Get-Office { } function Install-WinGet { - $wingetPreviewLink = "https://aka.ms/getwingetpreview" - $wingetPackageDestination = "$env:TEMP\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" - $dependencies = @( - @{ - Source = "https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx" - Destination = "$env:TEMP\Microsoft.VCLibs.x64.14.00.Desktop.appx" - }, - @{ - Source = "https://github.com/microsoft/microsoft-ui-xaml/releases/download/v2.8.6/Microsoft.UI.Xaml.2.8.x64.appx" - Destination = "$env:TEMP\Microsoft.UI.Xaml.2.8.x64.appx" - } + param ( + [string]$Architecture ) - foreach ($dependency in $dependencies) { - $dependencyName = [System.IO.Path]::GetFileName($dependency.Source) - WriteLog "Downloading $dependencyName..." - Start-BitsTransferWithRetry -Source $dependency.Source -Destination $dependency.Destination - WriteLog "Installing $dependencyName..." - Add-AppxPackage -Path $dependency.Destination -ErrorAction SilentlyContinue - WriteLog "Removing $dependencyName..." - Remove-Item -Path $dependency.Destination -Force -ErrorAction SilentlyContinue + $packages = @( + @{Name = "VCLibs"; Url = "https://aka.ms/Microsoft.VCLibs.$Architecture.14.00.Desktop.appx"; File = "Microsoft.VCLibs.$Architecture.14.00.Desktop.appx"}, + @{Name = "UIXaml"; Url = "https://github.com/microsoft/microsoft-ui-xaml/releases/download/v2.8.6/Microsoft.UI.Xaml.2.8.$Architecture.appx"; File = "Microsoft.UI.Xaml.2.8.$Architecture.appx"}, + @{Name = "WinGet"; Url = "https://aka.ms/getwinget"; File = "Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"} + ) + foreach ($package in $packages) { + $destination = Join-Path -Path $env:TEMP -ChildPath $package.File + WriteLog "Downloading $($package.Name) from $($package.Url) to $destination" + Start-BitsTransferWithRetry -Source $package.Url -Destination $destination + WriteLog "Installing $($package.Name)..." + Add-AppxPackage -Path $destination -ErrorAction SilentlyContinue + WriteLog "Removing $($package.Name)..." + Remove-Item -Path $destination -Force -ErrorAction SilentlyContinue } - WriteLog "Downloading WinGet..." - Start-BitsTransferWithRetry -Source $wingetPreviewLink -Destination $wingetPackageDestination - WriteLog "Installing WinGet..." - Add-AppxPackage -Path $wingetPackageDestination -ErrorAction SilentlyContinue - WriteLog "Removing WinGet installer..." - Remove-Item -Path $wingetPackageDestination -Force -ErrorAction SilentlyContinue + WriteLog "WinGet installation complete." } function Confirm-WinGetInstallation { $wingetPath = "$env:LOCALAPPDATA\Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\winget.exe" - if (-not (Test-Path $wingetPath)) { - WriteLog "WinGet is not installed. Downloading preview version of WinGet..." - Install-WinGet + $minVersion = [version]"1.8.1911" + if (-not (Test-Path -Path $wingetPath -PathType Leaf)) { + WriteLog "WinGet is not installed. Downloading WinGet..." + Install-WinGet -Architecture $WindowsArch + return } - elseif (-not (Get-Command winget -ErrorAction SilentlyContinue)) { - WriteLog "WinGet is not on the path. Downloading preview version of WinGet..." - Install-WinGet - } - elseif (-not ((& winget.exe --version) -like "*preview*")) { - WriteLog "The preview version of WinGet is not installed. Downloading preview version of WinGet..." - Install-WinGet + if (-not (Get-Command -Name winget -ErrorAction SilentlyContinue)) { + WriteLog "WinGet is not on the path. Downloading WinGet..." + Install-WinGet -Architecture $WindowsArch + return + } + $wingetVersion = & winget.exe --version + if ($wingetVersion -match 'v?(\d+\.\d+\.\d+)' -and [version]$matches[1] -lt $minVersion) { + WriteLog "The installed version of WinGet $($matches[1]) does not support downloading MSStore apps. Downloading the latest version of WinGet..." + Install-WinGet -Architecture $WindowsArch + return } } @@ -1800,18 +1796,25 @@ function Get-WinGetApp { $appFolderPath = Join-Path -Path "$AppsPath\Win32" -ChildPath $WinGetAppName New-Item -Path $appFolderPath -ItemType Directory -Force | Out-Null WriteLog "Downloading $WinGetAppName..." - $wingetDownloadResult = & winget.exe download --id "$WinGetAppId" --exact --download-directory "$appFolderPath" --scope machine --source winget --architecture "$WindowsArch" | Out-String + $downloadParams = @( + "download", + "--id", "$WinGetAppId", + "--exact", + "--download-directory", "$appFolderPath", + "--accept-package-agreements", + "--accept-source-agreements", + "--source", "winget", + "--scope", "machine", + "--architecture", "$WindowsArch" + ) + $wingetDownloadResult = & winget.exe @downloadParams | Out-String if ($wingetDownloadResult -match "No applicable installer found") { WriteLog "No installer found for $WindowsArch architecture. Attempting to download without specifying architecture..." - $wingetDownloadResult = & winget.exe download --id "$WinGetAppId" --exact --download-directory "$appFolderPath" --scope machine --source winget | Out-String + $downloadParams = $downloadParams | Where-Object { $_ -notmatch "--architecture" -and $_ -notmatch "$WindowsArch" } + $wingetDownloadResult = & winget.exe @downloadParams | Out-String if ($wingetDownloadResult -match "Installer downloaded") { WriteLog "Downloaded $WinGetAppName without specifying architecture." } - else { - WriteLog "No installer found for $WinGetAppName. Skipping download." - Remove-Item -Path $appFolderPath -Recurse -Force - return $false - } } if ($wingetDownloadResult -notmatch "Installer downloaded") { WriteLog "No installer found for $WinGetAppName. Skipping download." From 191c30dd659ec83e629c6a40dcc05db6f8ab4c27 Mon Sep 17 00:00:00 2001 From: Zehadi Alam <63765084+zehadialam@users.noreply.github.com> Date: Fri, 12 Jul 2024 22:43:10 -0400 Subject: [PATCH 7/9] Remove New-WinGetSettings, since stable release of WinGet now supports MSStore app downloads --- FFUDevelopment/BuildFFUVM.ps1 | 37 ----------------------------------- 1 file changed, 37 deletions(-) diff --git a/FFUDevelopment/BuildFFUVM.ps1 b/FFUDevelopment/BuildFFUVM.ps1 index f7e108c..8578c47 100644 --- a/FFUDevelopment/BuildFFUVM.ps1 +++ b/FFUDevelopment/BuildFFUVM.ps1 @@ -1700,42 +1700,6 @@ function Confirm-WinGetInstallation { } } -function New-WinGetSettings { - $wingetSettingsFile = "$env:LOCALAPPDATA\Packages\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\LocalState\settings.json" - $wingetSettings = @( - '{' - ' "$schema": "https://aka.ms/winget-settings.schema.json",' - ' ' - ' // For documentation on these settings, see: https://aka.ms/winget-settings' - ' "experimentalFeatures": {' - ' "storeDownload": true' - ' },' - ' "logging": {' - ' "level": "verbose"' - ' }' - '}' - ) - $wingetSettingsContent = $wingetSettings -join "`n" - if (Test-Path -Path $wingetSettingsFile -PathType Leaf) { - $jsonContent = Get-Content -Path $wingetSettingsFile -Raw - # Check if storeDownload feature is already enabled - WriteLog "Checking if storeDownload feature is enabled in WinGet configuration." - if ($jsonContent -match '"storeDownload"\s*:\s*true') { - WriteLog "WinGet's settings.json file is already configured to enable the storeDownload feature." - return - } - # Back up existing settings.json file - WriteLog "The storeDownload feature is not enabled in WinGet configuration." - $backupWingetSettingsFile = $wingetSettingsFile + ".bak" - if (-not (Test-Path -Path $backupWingetSettingsFile -PathType Leaf)) { - WriteLog "Backing up existing WinGet settings.json file to $backupWingetSettingsFile" - Copy-Item -Path $wingetSettingsFile -Destination $backupWingetSettingsFile -Force | Out-Null - } - } - WriteLog "Creating WinGet settings.json file to allow the storeDownload feature. Writing file to $wingetSettingsFile" - $wingetSettingsContent | Out-File -FilePath $wingetSettingsFile -Encoding utf8 -Force -} - function Add-Win32SilentInstallCommand { param ( [string]$AppFolder, @@ -1962,7 +1926,6 @@ function Get-Apps { } } if ($storeApps) { - New-WinGetSettings if (-not (Test-Path -Path $storeAppsFolder -PathType Container)) { New-Item -Path $storeAppsFolder -ItemType Directory -Force | Out-Null } From ab58b27a1dc4639e5025c76bd38ac9275f703991 Mon Sep 17 00:00:00 2001 From: Zehadi Alam <63765084+zehadialam@users.noreply.github.com> Date: Fri, 12 Jul 2024 23:40:59 -0400 Subject: [PATCH 8/9] Removed checking of appId using result parsing, since appId is already a parameter, added command to remove unprovisioned Notepad++ package, which breaks Sysprep --- FFUDevelopment/Apps/AppList.json | 5 +++++ FFUDevelopment/Apps/InstallAppsandSysprep.cmd | 5 +++++ FFUDevelopment/BuildFFUVM.ps1 | 8 ++------ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/FFUDevelopment/Apps/AppList.json b/FFUDevelopment/Apps/AppList.json index ea7e1d7..a33c2b9 100644 --- a/FFUDevelopment/Apps/AppList.json +++ b/FFUDevelopment/Apps/AppList.json @@ -9,6 +9,11 @@ "name": "Company Portal", "id": "9WZDNCRFJ3PZ", "source": "msstore" + }, + { + "name": "Microsoft Teams", + "id": "Microsoft.Teams", + "source": "winget" } ] } \ No newline at end of file diff --git a/FFUDevelopment/Apps/InstallAppsandSysprep.cmd b/FFUDevelopment/Apps/InstallAppsandSysprep.cmd index 1fefc88..8b39b03 100644 --- a/FFUDevelopment/Apps/InstallAppsandSysprep.cmd +++ b/FFUDevelopment/Apps/InstallAppsandSysprep.cmd @@ -51,6 +51,11 @@ for /d %%D in ("%basepath%\*") do ( ) :remaining endlocal +for /r "D:\" %%G in (.) do ( + if exist "%%G\Notepad++" ( + powershell -Command "Remove-AppxPackage -Package NotepadPlusPlus_1.0.0.0_neutral__7njy0v32s6xk6" + ) +) REM The below lines will remove the unattend.xml that gets the machine into audit mode. If not removed, the OS will get stuck booting to audit mode each time. REM Also kills the sysprep process in order to automate sysprep generalize del c:\windows\panther\unattend\unattend.xml /F /Q diff --git a/FFUDevelopment/BuildFFUVM.ps1 b/FFUDevelopment/BuildFFUVM.ps1 index 8578c47..b09e4d9 100644 --- a/FFUDevelopment/BuildFFUVM.ps1 +++ b/FFUDevelopment/BuildFFUVM.ps1 @@ -1811,18 +1811,14 @@ function Get-StoreApp { WriteLog "$StoreAppName not found in WinGet repository. Skipping download." return } - # Skip the header lines and get the line with the app information - $appResult = $wingetSearchResult | Select-Object -Skip 2 | Select-Object -First 1 - # Split the line by whitespace and get the second-to-last item (the Id) - $appID = ($appResult -split '\s+')[-2] - # Checking app ID to determine if store app is a win32 app WriteLog "Checking if $StoreAppName is a win32 app..." - $appIsWin32 = $appID.StartsWith("XP") + $appIsWin32 = $StoreAppId.StartsWith("XP") if ($appIsWin32) { WriteLog "$StoreAppName is a win32 app. Adding to $AppsPath\win32 folder" $appFolderPath = Join-Path -Path "$AppsPath\win32" -ChildPath $StoreAppName } else { + WriteLog "$StoreAppName is not a win32 app." $appFolderPath = Join-Path -Path "$AppsPath\MSStore" -ChildPath $StoreAppName } New-Item -Path $appFolderPath -ItemType Directory -Force | Out-Null From ddf9c1f98664e96731491c9a34bd9d2c4279a6a0 Mon Sep 17 00:00:00 2001 From: Zehadi Alam <63765084+zehadialam@users.noreply.github.com> Date: Fri, 12 Jul 2024 23:46:01 -0400 Subject: [PATCH 9/9] Fix Get-Apps function parameter name --- FFUDevelopment/BuildFFUVM.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/FFUDevelopment/BuildFFUVM.ps1 b/FFUDevelopment/BuildFFUVM.ps1 index b09e4d9..45711c2 100644 --- a/FFUDevelopment/BuildFFUVM.ps1 +++ b/FFUDevelopment/BuildFFUVM.ps1 @@ -1890,9 +1890,9 @@ function Get-StoreApp { function Get-Apps { param ( - [string]$AppsList + [string]$AppList ) - $apps = Get-Content -Path $AppsList -Raw | ConvertFrom-Json + $apps = Get-Content -Path $AppList -Raw | ConvertFrom-Json if (-not $apps) { WriteLog "No apps were specified in AppList.json file." return @@ -3349,7 +3349,7 @@ if ($InstallApps) { exit } WriteLog "$AppsPath\InstallAppsandSysprep.cmd found" - Get-Apps -AppsList "$AppsPath\AppList.json" + Get-Apps -AppList "$AppsPath\AppList.json" if (-not $InstallOffice) { #Modify InstallAppsandSysprep.cmd to REM out the office install command $CmdContent = Get-Content -Path "$AppsPath\InstallAppsandSysprep.cmd"