Skip to content

Commit 3fba8cc

Browse files
authored
Merge pull request #67 from JonathanPitre/5.2
5.2.0 Update
2 parents 8c83ea9 + 916fccc commit 3fba8cc

File tree

1 file changed

+66
-39
lines changed

1 file changed

+66
-39
lines changed

winget-install.ps1

Lines changed: 66 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<#PSScriptInfo
22
3-
.VERSION 5.1.0
3+
.VERSION 5.2.0
44
55
.GUID 3b581edb-5d90-4fa1-ba15-4f2377275463
66
@@ -58,22 +58,23 @@
5858
[Version 5.0.4] - Fixed bug with UpdateSelf function. Fixed bug when installing that may cause NuGet prompt to not be suppressed. Introduced Install-NuGetIfRequired function.
5959
[Version 5.0.5] - Fixed exit code issue. Fixes #52.
6060
[Version 5.0.6] - Fixed installation issue on Server 2022 by changing installation method to same as Server 2019. Fixes #62.
61-
[Version 5.0.7] - Added the literal %LOCALAPPDATA% path to the user environment PATH to prevent issues when usernames or user profile paths change, or when using non-Latin characters. Fixes #45. Added support to catch Get-CimInstance errors, lately occuring in Windows Sandbox. Removed Server 2022 changes introduced in version 5.0.6. Register winget command in all OS versions except Server 2019. Fixes #57.
61+
[Version 5.0.7] - Added the literal %LOCALAPPDATA% path to the user environment PATH to prevent issues when usernames or user profile paths change, or when using non-Latin characters. Fixes #45. Added support to catch Get-CimInstance errors, lately occurring in Windows Sandbox. Removed Server 2022 changes introduced in version 5.0.6. Register winget command in all OS versions except Server 2019. Fixes #57.
6262
[Version 5.0.8] - Fixed an issue on Server 2019 where the script failed if the dependency was already installed by adding library/dependency version check functionality. Fixes #61. Thank you to @MatthiasGuelck for the fix.
6363
[Version 5.0.9] - Improved script output. Fixed error messages caused when checking for an existing library/dependency version with multiple installed variants by choosing highest version number of the installed dependency.
6464
[Version 5.1.0] - Added support for installing and using winget under the SYSTEM context. Thanks to @GraphicHealer for the contribution.
65+
[Version 5.2.0] - Added support for installing winget dependencies from winget-cli GitHub repository. Added fix for issue #66 and #65. Fixed version detection for winget dependencies. Thanks to @JonathanPitre for the contribution.
6566
6667
#>
6768

6869
<#
6970
.SYNOPSIS
70-
Downloads and installs the latest version of winget and its dependencies.
71+
Downloads and installs the latest version of winget and its dependencies.
7172
.DESCRIPTION
72-
Downloads and installs the latest version of winget and its dependencies.
73+
Downloads and installs the latest version of winget and its dependencies.
7374
7475
This script is designed to be straightforward and easy to use, removing the hassle of manually downloading, installing, and configuring winget. This function should be run with administrative privileges.
7576
.EXAMPLE
76-
winget-install
77+
winget-install
7778
.PARAMETER Debug
7879
Enables debug mode, which shows additional information for debugging.
7980
.PARAMETER Force
@@ -93,10 +94,10 @@ This script is designed to be straightforward and easy to use, removing the hass
9394
.PARAMETER Help
9495
Displays the full help information for the script.
9596
.NOTES
96-
Version : 5.1.0
97-
Created by : asheroto
97+
Version : 5.2.0
98+
Created by : asheroto
9899
.LINK
99-
Project Site: https://github.com/asheroto/winget-install
100+
Project Site: https://github.com/asheroto/winget-install
100101
#>
101102
[CmdletBinding()]
102103
param (
@@ -112,7 +113,7 @@ param (
112113
)
113114

114115
# Script information
115-
$CurrentVersion = '5.1.0'
116+
$CurrentVersion = '5.2.0'
116117
$RepoOwner = 'asheroto'
117118
$RepoName = 'winget-install'
118119
$PowerShellGalleryName = 'winget-install'
@@ -1029,13 +1030,13 @@ function Get-ManifestVersion {
10291030
[string]$Lib_Path
10301031
)
10311032

1032-
Write-Debug "Checking manifest version of $Lib_Path ..."
1033+
Write-Debug "Checking manifest version of $($Lib_Path)..."
10331034

10341035
# Load ZIP assembly to read the package contents
10351036
Add-Type -AssemblyName System.IO.Compression.FileSystem
10361037
$zip = [System.IO.Compression.ZipFile]::OpenRead($Lib_Path)
10371038

1038-
Write-Debug "Reading AppxManifest.xml from $Lib_Path..."
1039+
Write-Debug "Reading AppxManifest.xml from $($Lib_Path)..."
10391040
# Find and read the AppxManifest.xml
10401041
$entry = $zip.Entries | Where-Object { $_.FullName -eq "AppxManifest.xml" }
10411042

@@ -1068,17 +1069,17 @@ function Get-InstalledLibVersion {
10681069
The name of the library to check for installation and retrieve the version.
10691070
10701071
.EXAMPLE
1071-
Get-InstalledLibVersion -Lib_Name "*VCLibs*"
1072+
Get-InstalledLibVersion -Lib_Name "VCLibs.140.00.UWPDesktop"
10721073
#>
10731074
param(
10741075
[Parameter(Mandatory)]
10751076
[string]$Lib_Name
10761077
)
10771078

1078-
Write-Debug "Checking installed library version of $Lib_Name ..."
1079+
Write-Debug "Checking installed library version of $($Lib_Name)..."
10791080

10801081
# Get the highest version of the installed library
1081-
$InstalledLib = Get-AppxPackage -Name $Lib_Name -ErrorAction SilentlyContinue | Sort-Object Version -Descending | Select-Object -First 1
1082+
$InstalledLib = Get-AppxPackage -Name "*$($Lib_Name)*" -ErrorAction SilentlyContinue | Sort-Object Version -Descending | Select-Object -First 1
10821083

10831084
if ($InstalledLib) {
10841085
$InstalledLibVersion = $InstalledLib.Version
@@ -1107,7 +1108,7 @@ function Install-LibIfRequired {
11071108
The path to the library (ZIP) file containing the AppxManifest.xml.
11081109
11091110
.EXAMPLE
1110-
Install-LibIfRequired -Lib_Name "*VCLibs*" -Lib_Path "C:\path\to\library.zip"
1111+
Install-LibIfRequired -Lib_Name "VCLibs.140.00.UWPDesktop" -Lib_Path "C:\path\to\library.zip"
11111112
#>
11121113
param(
11131114
[Parameter(Mandatory)]
@@ -1124,8 +1125,15 @@ function Install-LibIfRequired {
11241125

11251126
# Compare versions and install if necessary
11261127
if (!$InstalledLibVersion -or !$DownloadedLibVersion -or ($DownloadedLibVersion -gt $InstalledLibVersion)) {
1127-
Write-Debug "Installing library version $DownloadedLibVersion ..."
1128-
Add-AppxPackage -Path $Lib_Path
1128+
if ($RunAsSystem) {
1129+
Write-Debug 'Running as system...'
1130+
Write-Debug "Installing library version $($DownloadedLibVersion)..."
1131+
$null = Add-ProvisionedAppxPackage -Online -SkipLicense -PackagePath $Lib_Path
1132+
} else {
1133+
Write-Debug 'Running as user...'
1134+
Write-Debug "Installing library version $($DownloadedLibVersion)..."
1135+
$null = Add-AppxPackage -Path $Lib_Path
1136+
}
11291137
} else {
11301138
Write-Output "Installed library version is up-to-date or newer. Skipping installation."
11311139
}
@@ -1258,9 +1266,9 @@ try {
12581266

12591267
Write-Output "Installing winget (this takes a minute or two)..."
12601268
if ($Debug) {
1261-
try { Repair-WinGetPackageManager -AllUsers } catch { }
1269+
try { Repair-WinGetPackageManager -AllUsers -Force -Latest } catch { }
12621270
} else {
1263-
try { Repair-WinGetPackageManager -AllUsers *>&1 | Out-Null } catch { }
1271+
try { Repair-WinGetPackageManager -AllUsers -Force -Latest *>&1 | Out-Null } catch { }
12641272
}
12651273
} catch {
12661274
$errorHandled = Handle-Error $_
@@ -1289,32 +1297,51 @@ try {
12891297
Write-Section "Dependencies"
12901298

12911299
try {
1292-
# Download VCLibs
1293-
$VCLibs_Url = "https://aka.ms/Microsoft.VCLibs.${arch}.14.00.Desktop.appx"
1294-
$VCLibs_Path = New-TemporaryFile2
1295-
Write-Output "Downloading VCLibs..."
1296-
Write-Debug "Downloading VCLibs from $VCLibs_Url to $VCLibs_Path`n`n"
1297-
Invoke-WebRequest -Uri $VCLibs_Url -OutFile $VCLibs_Path
1300+
# Download winget dependencies (VCLibs.140.00.UWPDesktop and UI.Xaml.2.8)
1301+
$winget_dependencies_path = New-TemporaryFile2
1302+
$winget_dependencies_url = Get-WingetDownloadUrl -Match 'DesktopAppInstaller_Dependencies.zip'
1303+
Write-Output 'Downloading winget dependencies...'
1304+
Write-Debug "Downloading winget dependencies from $winget_dependencies_url to $winget_dependencies_path`n`n"
1305+
Invoke-WebRequest -Uri $winget_dependencies_url -OutFile $winget_dependencies_path
1306+
1307+
# Load ZIP assembly to read the package contents
1308+
Add-Type -AssemblyName System.IO.Compression.FileSystem
1309+
$zip = [System.IO.Compression.ZipFile]::OpenRead($winget_dependencies_path)
1310+
1311+
$matchingEntries = $zip.Entries | Where-Object { $_.FullName -match ".*$arch.appx" }
1312+
if ($matchingEntries) {
1313+
$matchingEntries | ForEach-Object {
1314+
$destPath = Join-Path ([System.IO.Path]::GetTempPath()) $_.Name
1315+
Write-Debug "Extracting $($_.FullName) to $destPath..."
1316+
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, $destPath, $true)
1317+
# Get the paths to the extracted files
1318+
if ($_.Name -like '*.VCLibs.140.00.UWPDesktop*.appx') {
1319+
$VCLibs_Path = $destPath
1320+
Write-Debug "VCLibs_Path: $VCLibs_Path"
1321+
}
1322+
if ($_.Name -like '*.UI.Xaml.2.8*.appx') {
1323+
$UIXaml_Path = $destPath
1324+
Write-Debug "UIXaml_Path: $UIXaml_Path"
1325+
}
1326+
}
1327+
$zip.Dispose()
1328+
} else {
1329+
Write-Error "Dependency not found inside the file: $winget_dependencies_path"
1330+
}
12981331

1299-
# Install VCLibs
1300-
Write-Output "Installing VCLibs..."
1301-
Install-LibIfRequired -Lib_Name "*VCLibs*" -Lib_Path $VCLibs_Path
1332+
# Install VCLibs.140.00.UWPDesktop
1333+
Write-Output "Installing VCLibs.140.00.UWPDesktop..."
1334+
Install-LibIfRequired -Lib_Name 'VCLibs.140.00.UWPDesktop' -Lib_Path $VCLibs_Path
13021335

13031336
# Line break for readability
13041337
Write-Output ""
13051338

1306-
# Download UI.Xaml
1307-
$UIXaml_Url = "https://github.com/microsoft/microsoft-ui-xaml/releases/download/v2.8.6/Microsoft.UI.Xaml.2.8.${arch}.appx"
1308-
$UIXaml_Path = New-TemporaryFile2
1309-
Write-Output "Downloading UI.Xaml..."
1310-
Write-Debug "Downloading UI.Xaml from $UIXaml_Url to $UIXaml_Path"
1311-
Invoke-WebRequest -Uri $UIXaml_Url -OutFile $UIXaml_Path
1312-
1313-
# Install UI.Xaml
1314-
Write-Output "Installing UI.Xaml..."
1315-
Install-LibIfRequired -Lib_Name "*UI.Xaml*" -Lib_Path $UIXaml_Path
1339+
# Install UI.Xaml.2.8
1340+
Write-Output "Installing UI.Xaml.2.8..."
1341+
Install-LibIfRequired -Lib_Name 'UI.Xaml.2.8' -Lib_Path $UIXaml_Path
13161342

13171343
Write-Debug "Removing temporary files..."
1344+
TryRemove $winget_dependencies_path
13181345
TryRemove $VCLibs_Path
13191346
TryRemove $UIXaml_Path
13201347
} catch {
@@ -1342,7 +1369,7 @@ try {
13421369

13431370
# Download winget
13441371
$winget_path = New-TemporaryFile2
1345-
$winget_url = "https://aka.ms/getwinget"
1372+
$winget_url = Get-WingetDownloadUrl -Match 'Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle'
13461373
Write-Output "Downloading winget..."
13471374
Write-Debug "Downloading winget from $winget_url to $winget_path`n`n"
13481375
Invoke-WebRequest -Uri $winget_url -OutFile $winget_path

0 commit comments

Comments
 (0)