easy-devops 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -3
- package/cli/managers/ssl-manager.js +81 -121
- package/cli/menus/dashboard.js +22 -2
- package/core/nginx-conf-generator.js +4 -3
- package/dashboard/lib/nginx-service.js +19 -6
- package/dashboard/routes/domains.js +6 -1
- package/lib/installer/winget-install.ps1 +1810 -0
- package/package.json +1 -1
|
@@ -0,0 +1,1810 @@
|
|
|
1
|
+
<#PSScriptInfo
|
|
2
|
+
|
|
3
|
+
.VERSION 5.3.6
|
|
4
|
+
|
|
5
|
+
.GUID 3b581edb-5d90-4fa1-ba15-4f2377275463
|
|
6
|
+
|
|
7
|
+
.AUTHOR asheroto
|
|
8
|
+
|
|
9
|
+
.COMPANYNAME asheroto
|
|
10
|
+
|
|
11
|
+
.TAGS PowerShell Windows winget win get install installer fix script setup
|
|
12
|
+
|
|
13
|
+
.PROJECTURI https://github.com/asheroto/winget-install
|
|
14
|
+
|
|
15
|
+
.RELEASENOTES
|
|
16
|
+
[Version 0.0.1] - Initial Release.
|
|
17
|
+
[Version 0.0.2] - Implemented function to get the latest version of winget and its license.
|
|
18
|
+
[Version 0.0.3] - Signed file for PSGallery.
|
|
19
|
+
[Version 0.0.4] - Changed URI to grab latest release instead of releases and preleases.
|
|
20
|
+
[Version 0.0.5] - Updated version number of dependencies.
|
|
21
|
+
[Version 1.0.0] - Major refactor code, see release notes for more information.
|
|
22
|
+
[Version 1.0.1] - Fixed minor bug where version 2.8 was hardcoded in URL.
|
|
23
|
+
[Version 1.0.2] - Hardcoded UI Xaml version 2.8.4 as a failsafe in case the API fails. Added CheckForUpdates, Version, Help functions. Various bug fixes.
|
|
24
|
+
[Version 1.0.3] - Added error message to catch block. Fixed bug where appx package was not being installed.
|
|
25
|
+
[Version 1.0.4] - MisterZeus optimized code for readability.
|
|
26
|
+
[Version 2.0.0] - Major refactor. Reverted to UI.Xaml 2.7.3 for stability. Adjusted script to fix install issues due to winget changes (thank you ChrisTitusTech). Added in all architecture support.
|
|
27
|
+
[Version 2.0.1] - Renamed repo and URL references from winget-installer to winget-install. Added extra space after the last line of output.
|
|
28
|
+
[Version 2.0.2] - Adjusted CheckForUpdates to include Install-Script instructions and extra spacing.
|
|
29
|
+
[Version 2.1.0] - Added alternate method/URL for dependencies in case the main URL is down. Fixed licensing issue when winget is installed on Server 2022.
|
|
30
|
+
[Version 2.1.1] - Switched primary/alternate methods. Added Cleanup function to avoid errors when cleaning up temp files. Added output of URL for alternate method. Suppressed Add-AppxProvisionedPackage output. Improved success message. Improved verbiage. Improve PS script comments. Added check if the URL is empty. Moved display of URL beneath the check.
|
|
31
|
+
[Version 3.0.0] - Major changes. Added OS version detection checks - detects OS version, release ID, ensures compatibility. Forces older file installation for Server 2022 to avoid issues after installing. Added DebugMode, DisableCleanup, Force. Renamed CheckForUpdates to CheckForUpdate. Improved output. Improved error handling. Improved comments. Improved code readability. Moved CheckForUpdate into function. Added PowerShellGalleryName. Renamed Get-OSVersion to Get-OSInfo. Moved architecture detection into Get-OSInfo. Renamed Get-NewestLink to Get-WingetDownloadUrl. Have Get-WingetDownloadUrl not get preview releases.
|
|
32
|
+
[Version 3.0.1] - Updated Get-OSInfo function to fix issues when used on non-English systems. Improved error handling of "resources in use" error.
|
|
33
|
+
[Version 3.0.2] - Added winget registration command for Windows 10 machines.
|
|
34
|
+
[Version 3.1.0] - Added support for one-line installation with irm and iex compatible with $Force session variable. Added UpdateSelf command to automatically update the script to the latest version. Created short URL asheroto.com/winget.
|
|
35
|
+
[Version 3.1.1] - Changed winget register command to run on all OS versions.
|
|
36
|
+
[Version 3.2.0] - Added -ForceClose logic to relaunch the script in conhost.exe and automatically end active processes associated with winget that could interfere with the installation. Improved verbiage on winget already installed.
|
|
37
|
+
[Version 3.2.1] - Fixed minor glitch when using -Version or -Help parameters.
|
|
38
|
+
[Version 3.2.2] - Improved script exit functionality.
|
|
39
|
+
[Version 3.2.3] - Improved -ForceClose window handling with x86 PowerShell process.
|
|
40
|
+
[Version 3.2.4] - Improved verbiage for incompatible systems. Added importing Appx module on Windows Server with PowerShell 7+ systems to avoid error message.
|
|
41
|
+
[Version 3.2.5] - Removed pause after script completion. Added optional Wait parameter to force script to wait several seconds for script output.
|
|
42
|
+
[Version 3.2.6] - Improved ExitWithDelay function. Sometimes PowerShell will close the window accidentally, even when using the proper 'exit' command. Adjusted several closures for improved readability. Improved error code checking. Fixed glitch with -Wait param.
|
|
43
|
+
[Version 3.2.7] - Addded ability to install for all users. Added checks for Windows Sandbox and administrative privileges.
|
|
44
|
+
[Version 4.0.0] - Microsoft created some short URLs for winget. Removed a large portion of the script to use short URLs instead. Simplified and refactored. Switched debug param from DebugMode to Debug.
|
|
45
|
+
[Version 4.0.1] - Fixed PowerShell help information.
|
|
46
|
+
[Version 4.0.2] - Adjusted UpdateSelf function to reset PSGallery to original state if it was not trusted. Improved comments.
|
|
47
|
+
[Version 4.0.3] - Updated UI.Xaml package as per winget-cli issue #4208.
|
|
48
|
+
[Version 4.0.4] - Fixed detection for Windows multi-session.
|
|
49
|
+
[Version 4.0.5] - Improved error handling when registering winget.
|
|
50
|
+
[Version 4.1.0] - Support for Windows Server 2019 added by installing Visual C++ Redistributable.
|
|
51
|
+
[Version 4.1.1] - Minor revisions to comments & debug output.
|
|
52
|
+
[Version 4.1.2] - Implemented Visual C++ Redistributable version detection to ensure compatibility with winget.
|
|
53
|
+
[Version 4.1.3] - Added additional debug output for Visual C++ Redistributable version detection.
|
|
54
|
+
[Version 5.0.0] - Completely changed method to use winget-cli Repair-WingetPackageManager. Added environment path detection and addition if needed. Added NoExit parameter to prevent script from exiting after completion. Adjusted permissions of winget folder path for Server 2019. Improved exit handling to avoid PowerShell window closing.
|
|
55
|
+
[Version 5.0.1] - Fixed typo in variable name.
|
|
56
|
+
[Version 5.0.2] - Added detection of NuGet and PowerShell version to determine if package provider installation is needed.
|
|
57
|
+
[Version 5.0.3] - Fixed missing argument in call to Add-ToEnvironmentPath.
|
|
58
|
+
[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.
|
|
59
|
+
[Version 5.0.5] - Fixed exit code issue. Fixes #52.
|
|
60
|
+
[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 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.
|
|
62
|
+
[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.
|
|
63
|
+
[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.
|
|
64
|
+
[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.
|
|
66
|
+
[Version 5.2.1] - Switched SYSTEM account specification to SID identifier to avoid issues in non-US locales. Added avoidance of pre-release versions and dynamically installing dependencies. Thanks to @langermi and @AAGITLTD for the contribution.
|
|
67
|
+
[Version 5.3.0] - Added support for installing specific version of winget. Added try/catch to prevent errors when getting/setting ACLs. Thank you @jantari for the contribution.
|
|
68
|
+
[Version 5.3.1] - Fixed glitch with Get-WinGetFolderPath. Improved detection of Winget version number if specified. Added support for GitHub API token. Thank you @m41kc0d3 for the contribution.
|
|
69
|
+
[Version 5.3.2] - Fixed an incorrect parameter type for Apply-PathPermissionsFixAndAddPath. Thanks to @dblohm7 for the fix.
|
|
70
|
+
[Version 5.3.3] - Fixed missing debug variable causing unexpected error.
|
|
71
|
+
[Version 5.3.4] - Fixed debug variable not defined.
|
|
72
|
+
[Version 5.3.5] - Improved winget detection to verify installation and functionality. Enforced alternate install method on Server 2022. Added winget AppX detection to prevent appx registration errors.
|
|
73
|
+
[Version 5.3.6] - Fixed bug where GitHub token was not being used during an update check. Thanks for @dteusner for the fix.
|
|
74
|
+
|
|
75
|
+
#>
|
|
76
|
+
|
|
77
|
+
<#
|
|
78
|
+
.SYNOPSIS
|
|
79
|
+
Downloads and installs the latest version of winget and its dependencies.
|
|
80
|
+
.DESCRIPTION
|
|
81
|
+
Downloads and installs the latest version of winget and its dependencies.
|
|
82
|
+
|
|
83
|
+
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.
|
|
84
|
+
.EXAMPLE
|
|
85
|
+
winget-install
|
|
86
|
+
.PARAMETER Debug
|
|
87
|
+
Enables debug mode, which shows additional information for debugging.
|
|
88
|
+
.PARAMETER Force
|
|
89
|
+
Ensures installation of winget and its dependencies, even if already present.
|
|
90
|
+
.PARAMETER ForceClose
|
|
91
|
+
Relaunches the script in conhost.exe and automatically ends active processes associated with winget that could interfere with the installation.
|
|
92
|
+
.PARAMETER Wait
|
|
93
|
+
Forces the script to wait several seconds before exiting.
|
|
94
|
+
.PARAMETER NoExit
|
|
95
|
+
Forces the script to wait indefinitely before exiting.
|
|
96
|
+
.PARAMETER UpdateSelf
|
|
97
|
+
Updates the script to the latest version on PSGallery.
|
|
98
|
+
.PARAMETER CheckForUpdate
|
|
99
|
+
Checks if there is an update available for the script.
|
|
100
|
+
.PARAMETER GHtoken
|
|
101
|
+
The GitHub token to access to the GitHub API with higher rate limit.
|
|
102
|
+
.PARAMETER Version
|
|
103
|
+
Displays the version of the script.
|
|
104
|
+
.PARAMETER Help
|
|
105
|
+
Displays the full help information for the script.
|
|
106
|
+
.NOTES
|
|
107
|
+
Version : 5.3.6
|
|
108
|
+
Created by : asheroto
|
|
109
|
+
.LINK
|
|
110
|
+
Project Site: https://github.com/asheroto/winget-install
|
|
111
|
+
#>
|
|
112
|
+
[CmdletBinding()]
|
|
113
|
+
param (
|
|
114
|
+
[switch]$Force,
|
|
115
|
+
[switch]$ForceClose,
|
|
116
|
+
[switch]$AlternateInstallMethod,
|
|
117
|
+
[switch]$CheckForUpdate,
|
|
118
|
+
[switch]$Wait,
|
|
119
|
+
[string]$WingetVersion = 'latest',
|
|
120
|
+
[switch]$NoExit,
|
|
121
|
+
[switch]$UpdateSelf,
|
|
122
|
+
[string]$GHtoken,
|
|
123
|
+
[switch]$Version,
|
|
124
|
+
[switch]$Help
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
# Script information
|
|
128
|
+
$CurrentVersion = '5.3.6'
|
|
129
|
+
$RepoOwner = 'asheroto'
|
|
130
|
+
$RepoName = 'winget-install'
|
|
131
|
+
$PowerShellGalleryName = 'winget-install'
|
|
132
|
+
|
|
133
|
+
# Preferences
|
|
134
|
+
$ProgressPreference = 'SilentlyContinue' # Suppress progress bar (makes downloading super fast)
|
|
135
|
+
$ConfirmPreference = 'None' # Suppress confirmation prompts
|
|
136
|
+
|
|
137
|
+
# Display version if -Version is specified
|
|
138
|
+
if ($Version.IsPresent) {
|
|
139
|
+
$CurrentVersion
|
|
140
|
+
exit 0
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
# Display full help if -Help is specified
|
|
144
|
+
if ($Help) {
|
|
145
|
+
Get-Help -Name $MyInvocation.MyCommand.Source -Full
|
|
146
|
+
exit 0
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
# Display $PSVersionTable and Get-Host if -Verbose is specified
|
|
150
|
+
if ($PSBoundParameters.ContainsKey('Verbose') -and $PSBoundParameters['Verbose']) {
|
|
151
|
+
$PSVersionTable
|
|
152
|
+
Get-Host
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
# Set debug preferences if -Debug is specified
|
|
156
|
+
$Debug = $false
|
|
157
|
+
if ($PSBoundParameters.ContainsKey('Debug') -and $PSBoundParameters['Debug']) {
|
|
158
|
+
$DebugPreference = 'Continue'
|
|
159
|
+
$ConfirmPreference = 'None'
|
|
160
|
+
|
|
161
|
+
# Keep for easy detection later in the script
|
|
162
|
+
$Debug = $true
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
# Check if running as SYSTEM
|
|
166
|
+
$RunAsSystem = $false
|
|
167
|
+
if ([System.Security.Principal.WindowsIdentity]::GetCurrent().User -match "S-1-5-18") {
|
|
168
|
+
Write-Debug "Running as SYSTEM"
|
|
169
|
+
$RunAsSystem = $true
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
# Find the WinGet Executable Loaction (for use when running in SYSTEM context)
|
|
173
|
+
function Find-WinGet {
|
|
174
|
+
try {
|
|
175
|
+
# Get the WinGet path
|
|
176
|
+
$WinGetPathToResolve = Join-Path -Path $ENV:ProgramFiles -ChildPath 'WindowsApps\Microsoft.DesktopAppInstaller_*_*__8wekyb3d8bbwe'
|
|
177
|
+
$ResolveWinGetPath = Resolve-Path -Path $WinGetPathToResolve -ErrorAction Stop | Sort-Object {
|
|
178
|
+
[version]($_.Path -replace '^[^\d]+_((\d+\.)*\d+)_.*', '$1')
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if ($ResolveWinGetPath) {
|
|
182
|
+
# If we have multiple versions - use the latest.
|
|
183
|
+
$WinGetPath = $ResolveWinGetPath[-1].Path
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
$WinGet = Join-Path $WinGetPath 'winget.exe'
|
|
187
|
+
|
|
188
|
+
# Test if WinGet Executable exists
|
|
189
|
+
if (Test-Path -Path $WinGet) {
|
|
190
|
+
return $WinGet
|
|
191
|
+
} else {
|
|
192
|
+
return $null
|
|
193
|
+
}
|
|
194
|
+
} catch {
|
|
195
|
+
Write-Debug "Could not resolve winget path: $($_.Exception.Message)"
|
|
196
|
+
return $null
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function Get-OSInfo {
|
|
201
|
+
<#
|
|
202
|
+
.SYNOPSIS
|
|
203
|
+
Retrieves detailed information about the operating system version and architecture.
|
|
204
|
+
|
|
205
|
+
.DESCRIPTION
|
|
206
|
+
This function queries both the Windows registry and the Win32_OperatingSystem class to gather comprehensive information about the operating system. It returns details such as the release ID, display version, name, type (Workstation/Server), numeric version, edition ID, version (object that includes major, minor, and build numbers), and architecture (OS architecture, not processor architecture).
|
|
207
|
+
|
|
208
|
+
.EXAMPLE
|
|
209
|
+
Get-OSInfo
|
|
210
|
+
|
|
211
|
+
This example retrieves the OS version details of the current system and returns an object with properties like ReleaseId, DisplayVersion, Name, Type, NumericVersion, EditionId, Version, and Architecture.
|
|
212
|
+
|
|
213
|
+
.EXAMPLE
|
|
214
|
+
(Get-OSInfo).Version.Major
|
|
215
|
+
|
|
216
|
+
This example retrieves the major version number of the operating system. The Get-OSInfo function returns an object with a Version property, which itself is an object containing Major, Minor, and Build properties. You can access these sub-properties using dot notation.
|
|
217
|
+
|
|
218
|
+
.EXAMPLE
|
|
219
|
+
$osDetails = Get-OSInfo
|
|
220
|
+
Write-Output "OS Name: $($osDetails.Name)"
|
|
221
|
+
Write-Output "OS Type: $($osDetails.Type)"
|
|
222
|
+
Write-Output "OS Architecture: $($osDetails.Architecture)"
|
|
223
|
+
|
|
224
|
+
This example stores the result of Get-OSInfo in a variable and then accesses various properties to print details about the operating system.
|
|
225
|
+
#>
|
|
226
|
+
[CmdletBinding()]
|
|
227
|
+
param ()
|
|
228
|
+
|
|
229
|
+
try {
|
|
230
|
+
# Get registry values
|
|
231
|
+
$registryValues = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
|
|
232
|
+
$releaseIdValue = $registryValues.ReleaseId
|
|
233
|
+
$displayVersionValue = $registryValues.DisplayVersion
|
|
234
|
+
$nameValue = $registryValues.ProductName
|
|
235
|
+
$editionIdValue = $registryValues.EditionId
|
|
236
|
+
$installationType = $registryValues.InstallationType
|
|
237
|
+
|
|
238
|
+
# Strip out "Server" from the $editionIdValue if it exists
|
|
239
|
+
$editionIdValue = $editionIdValue -replace "Server", ""
|
|
240
|
+
|
|
241
|
+
# Get OS details using Get-CimInstance because the registry key for Name is not always correct with Windows 11
|
|
242
|
+
try {
|
|
243
|
+
$osDetails = Get-CimInstance -ClassName Win32_OperatingSystem -ErrorAction Stop
|
|
244
|
+
} catch {
|
|
245
|
+
throw "Unable to run the command ""Get-CimInstance -ClassName Win32_OperatingSystem"". If you're using Window Sandbox, this may be related to a known issue with winget on Windows Sandbox: https://github.com/microsoft/Windows-Sandbox/issues/67"
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
# Get OS caption
|
|
249
|
+
$nameValue = $osDetails.Caption
|
|
250
|
+
|
|
251
|
+
# Get architecture details of the OS (not the processor)
|
|
252
|
+
# Get only the numbers
|
|
253
|
+
$architecture = ($osDetails.OSArchitecture -replace "[^\d]").Trim()
|
|
254
|
+
|
|
255
|
+
# If 32-bit or 64-bit replace with x32 and x64
|
|
256
|
+
if ($architecture -eq "32") {
|
|
257
|
+
$architecture = "x32"
|
|
258
|
+
} elseif ($architecture -eq "64") {
|
|
259
|
+
$architecture = "x64"
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
# Get OS version details (as version object)
|
|
263
|
+
$versionValue = [System.Environment]::OSVersion.Version
|
|
264
|
+
|
|
265
|
+
# Determine product type
|
|
266
|
+
# Reference: https://learn.microsoft.com/en-us/dotnet/api/microsoft.powershell.commands.producttype?view=powershellsdk-1.1.0
|
|
267
|
+
if ($osDetails.ProductType -eq 1) {
|
|
268
|
+
$typeValue = "Workstation"
|
|
269
|
+
} elseif ($osDetails.ProductType -eq 2 -or $osDetails.ProductType -eq 3) {
|
|
270
|
+
$typeValue = "Server"
|
|
271
|
+
} else {
|
|
272
|
+
$typeValue = "Unknown"
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
# Extract numerical value from Name
|
|
276
|
+
$numericVersion = ($nameValue -replace "[^\d]").Trim()
|
|
277
|
+
|
|
278
|
+
# If the numeric version is 10 or above, and the caption contains "multi-session", consider it a workstation
|
|
279
|
+
if ($numericVersion -ge 10 -and $osDetails.Caption -match "multi-session") {
|
|
280
|
+
$typeValue = "Workstation"
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
# Create and return custom object with the required properties
|
|
284
|
+
$result = [PSCustomObject]@{
|
|
285
|
+
ReleaseId = $releaseIdValue
|
|
286
|
+
DisplayVersion = $displayVersionValue
|
|
287
|
+
Name = $nameValue
|
|
288
|
+
Type = $typeValue
|
|
289
|
+
NumericVersion = $numericVersion
|
|
290
|
+
EditionId = $editionIdValue
|
|
291
|
+
InstallationType = $installationType
|
|
292
|
+
Version = $versionValue
|
|
293
|
+
Architecture = $architecture
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return $result
|
|
297
|
+
} catch {
|
|
298
|
+
Write-Error "Unable to get OS version details.`nError: $_"
|
|
299
|
+
ExitWithDelay 1
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function Get-GitHubRelease {
|
|
304
|
+
<#
|
|
305
|
+
.SYNOPSIS
|
|
306
|
+
Fetches the latest release information of a GitHub repository.
|
|
307
|
+
|
|
308
|
+
.DESCRIPTION
|
|
309
|
+
This function uses the GitHub API to get information about the latest release of a specified repository, including its version and the date it was published.
|
|
310
|
+
|
|
311
|
+
.PARAMETER Owner
|
|
312
|
+
The GitHub username of the repository owner.
|
|
313
|
+
|
|
314
|
+
.PARAMETER Repo
|
|
315
|
+
The name of the repository.
|
|
316
|
+
|
|
317
|
+
.PARAMETER GHtoken
|
|
318
|
+
The GitHub token to access to the GitHub API with higher rate limit.
|
|
319
|
+
|
|
320
|
+
.EXAMPLE
|
|
321
|
+
Get-GitHubRelease -Owner "asheroto" -Repo "winget-install"
|
|
322
|
+
This command retrieves the latest release version and published datetime of the winget-install repository owned by asheroto.
|
|
323
|
+
#>
|
|
324
|
+
[CmdletBinding()]
|
|
325
|
+
param (
|
|
326
|
+
[string]$Owner,
|
|
327
|
+
[string]$Repo,
|
|
328
|
+
[string]$GHtoken
|
|
329
|
+
)
|
|
330
|
+
try {
|
|
331
|
+
$url = "https://api.github.com/repos/$Owner/$Repo/releases/latest"
|
|
332
|
+
if ($GHtoken) {
|
|
333
|
+
$headers = @{
|
|
334
|
+
'Authorization' = "Bearer $GHtoken"
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
$response = Invoke-RestMethod -Uri $url -Headers $headers -ErrorAction Stop
|
|
338
|
+
|
|
339
|
+
$latestVersion = $response.tag_name
|
|
340
|
+
$publishedAt = $response.published_at
|
|
341
|
+
|
|
342
|
+
# Convert UTC time string to local time
|
|
343
|
+
$UtcDateTime = [DateTime]::Parse($publishedAt, [System.Globalization.CultureInfo]::InvariantCulture, [System.Globalization.DateTimeStyles]::RoundtripKind)
|
|
344
|
+
$PublishedLocalDateTime = $UtcDateTime.ToLocalTime()
|
|
345
|
+
|
|
346
|
+
[PSCustomObject]@{
|
|
347
|
+
LatestVersion = $latestVersion
|
|
348
|
+
PublishedDateTime = $PublishedLocalDateTime
|
|
349
|
+
}
|
|
350
|
+
} catch {
|
|
351
|
+
Write-Error "Unable to check for updates.`nError: $_"
|
|
352
|
+
exit 1
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function CheckForUpdate {
|
|
357
|
+
param (
|
|
358
|
+
[string]$RepoOwner,
|
|
359
|
+
[string]$RepoName,
|
|
360
|
+
[version]$CurrentVersion,
|
|
361
|
+
[string]$PowerShellGalleryName,
|
|
362
|
+
[string]$GHtoken
|
|
363
|
+
)
|
|
364
|
+
|
|
365
|
+
$Data = Get-GitHubRelease -Owner $RepoOwner -Repo $RepoName -GHtoken $GHtoken
|
|
366
|
+
|
|
367
|
+
Write-Output ""
|
|
368
|
+
Write-Output ("Repository: {0,-40}" -f "https://github.com/$RepoOwner/$RepoName")
|
|
369
|
+
Write-Output ("Current Version: {0,-40}" -f $CurrentVersion)
|
|
370
|
+
Write-Output ("Latest Version: {0,-40}" -f $Data.LatestVersion)
|
|
371
|
+
Write-Output ("Published at: {0,-40}" -f $Data.PublishedDateTime)
|
|
372
|
+
|
|
373
|
+
if ($Data.LatestVersion -gt $CurrentVersion) {
|
|
374
|
+
Write-Output ("Status: {0,-40}" -f "A new version is available.")
|
|
375
|
+
Write-Output "`nOptions to update:"
|
|
376
|
+
Write-Output "- Download latest release: https://github.com/$RepoOwner/$RepoName/releases"
|
|
377
|
+
if ($PowerShellGalleryName) {
|
|
378
|
+
Write-Output "- Run: $RepoName -UpdateSelf"
|
|
379
|
+
Write-Output "- Run: Install-Script $PowerShellGalleryName -Force"
|
|
380
|
+
}
|
|
381
|
+
} else {
|
|
382
|
+
Write-Output ("Status: {0,-40}" -f "Up to date.")
|
|
383
|
+
}
|
|
384
|
+
exit 0
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function UpdateSelf {
|
|
388
|
+
try {
|
|
389
|
+
# Get PSGallery version of script
|
|
390
|
+
$psGalleryScriptVersion = (Find-Script -Name $PowerShellGalleryName).Version
|
|
391
|
+
|
|
392
|
+
# If the current version is less than the PSGallery version, update the script
|
|
393
|
+
if ($CurrentVersion -lt $psGalleryScriptVersion) {
|
|
394
|
+
Write-Output "Updating script to version $psGalleryScriptVersion..."
|
|
395
|
+
|
|
396
|
+
# Check if running in PowerShell 7 or greater
|
|
397
|
+
Write-Debug "Checking if NuGet PackageProvider is already installed..."
|
|
398
|
+
Install-NuGetIfRequired
|
|
399
|
+
|
|
400
|
+
# Trust the PSGallery if not already trusted
|
|
401
|
+
$psRepoInstallationPolicy = (Get-PSRepository -Name 'PSGallery').InstallationPolicy
|
|
402
|
+
if ($psRepoInstallationPolicy -ne 'Trusted') {
|
|
403
|
+
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted | Out-Null
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
# Update the script
|
|
407
|
+
Install-Script $PowerShellGalleryName -Force
|
|
408
|
+
|
|
409
|
+
# If PSGallery was not trusted, reset it to its original state
|
|
410
|
+
if ($psRepoInstallationPolicy -ne 'Trusted') {
|
|
411
|
+
Set-PSRepository -Name 'PSGallery' -InstallationPolicy $psRepoInstallationPolicy | Out-Null
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
Write-Output "Script updated to version $psGalleryScriptVersion."
|
|
415
|
+
exit 0
|
|
416
|
+
} else {
|
|
417
|
+
Write-Output "Script is already up to date."
|
|
418
|
+
exit 0
|
|
419
|
+
}
|
|
420
|
+
} catch {
|
|
421
|
+
Write-Output "An error occurred: $_"
|
|
422
|
+
exit 1
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function Write-Section($text) {
|
|
427
|
+
<#
|
|
428
|
+
.SYNOPSIS
|
|
429
|
+
Prints a text block surrounded by a section divider for enhanced output readability.
|
|
430
|
+
|
|
431
|
+
.DESCRIPTION
|
|
432
|
+
This function takes a string input and prints it to the console, surrounded by a section divider made of hash characters.
|
|
433
|
+
It is designed to enhance the readability of console output.
|
|
434
|
+
|
|
435
|
+
.PARAMETER text
|
|
436
|
+
The text to be printed within the section divider.
|
|
437
|
+
|
|
438
|
+
.EXAMPLE
|
|
439
|
+
Write-Section "Downloading Files..."
|
|
440
|
+
This command prints the text "Downloading Files..." surrounded by a section divider.
|
|
441
|
+
#>
|
|
442
|
+
Write-Output ""
|
|
443
|
+
Write-Output ("#" * ($text.Length + 4))
|
|
444
|
+
Write-Output "# $text #"
|
|
445
|
+
Write-Output ("#" * ($text.Length + 4))
|
|
446
|
+
Write-Output ""
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function Get-WingetDownloadUrl {
|
|
450
|
+
<#
|
|
451
|
+
.SYNOPSIS
|
|
452
|
+
Retrieves the download URL of the latest release asset that matches a specified pattern from the GitHub repository.
|
|
453
|
+
|
|
454
|
+
.DESCRIPTION
|
|
455
|
+
This function uses the GitHub API to get information about the latest release of the winget-cli repository.
|
|
456
|
+
It then retrieves the download URL for the release asset that matches a specified pattern.
|
|
457
|
+
|
|
458
|
+
.PARAMETER Match
|
|
459
|
+
The pattern to match in the asset names.
|
|
460
|
+
|
|
461
|
+
.EXAMPLE
|
|
462
|
+
Get-WingetDownloadUrl "msixbundle"
|
|
463
|
+
This command retrieves the download URL for the latest release asset with a name that contains "msixbundle".
|
|
464
|
+
#>
|
|
465
|
+
[CmdletBinding()]
|
|
466
|
+
param (
|
|
467
|
+
[Parameter(Mandatory = $true)]
|
|
468
|
+
[string]$Match,
|
|
469
|
+
[string]$WingetVersion = 'latest',
|
|
470
|
+
[string]$GHtoken
|
|
471
|
+
)
|
|
472
|
+
|
|
473
|
+
# Always initialize headers to avoid null argument errors
|
|
474
|
+
$headers = @{}
|
|
475
|
+
if (-not [string]::IsNullOrWhiteSpace($GHtoken)) {
|
|
476
|
+
$headers['Authorization'] = "Bearer $GHtoken"
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
if ([string]::IsNullOrWhiteSpace($WingetVersion) -or $WingetVersion -eq 'latest') {
|
|
480
|
+
Write-Debug "Retrieving latest winget release info..."
|
|
481
|
+
$latestStable = Invoke-RestMethod 'https://api.github.com/repos/microsoft/winget-cli/releases/latest' -Headers $headers -ErrorAction Stop
|
|
482
|
+
$tag = $latestStable.tag_name
|
|
483
|
+
} else {
|
|
484
|
+
Write-Debug "Retrieving winget version $WingetVersion..."
|
|
485
|
+
$tag = $WingetVersion
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
if ($tag -notmatch '^v') {
|
|
489
|
+
$tag = "v$tag"
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
$uri = "https://api.github.com/repos/microsoft/winget-cli/releases/tags/$tag"
|
|
493
|
+
Write-Debug "Fetching release data from $uri"
|
|
494
|
+
$release = Invoke-RestMethod -Uri $uri -Headers $headers -ErrorAction Stop
|
|
495
|
+
|
|
496
|
+
$data = $release.assets | Where-Object { $_.name -match $Match } | Select-Object -First 1
|
|
497
|
+
|
|
498
|
+
if ($null -ne $data -and $null -ne $data.browser_download_url) {
|
|
499
|
+
Write-Debug "Found asset: $($data.name)"
|
|
500
|
+
Write-Debug "Download URL: $($data.browser_download_url)"
|
|
501
|
+
return [string]$data.browser_download_url
|
|
502
|
+
} else {
|
|
503
|
+
throw "Could not get winget release '$tag' or no asset matched '$Match'"
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
function Get-WingetStatus {
|
|
508
|
+
<#
|
|
509
|
+
.SYNOPSIS
|
|
510
|
+
Checks if winget is installed.
|
|
511
|
+
|
|
512
|
+
.DESCRIPTION
|
|
513
|
+
This function checks if winget is installed.
|
|
514
|
+
|
|
515
|
+
.EXAMPLE
|
|
516
|
+
Get-WingetStatus
|
|
517
|
+
#>
|
|
518
|
+
|
|
519
|
+
# Check if winget is installed
|
|
520
|
+
if ($RunAsSystem) {
|
|
521
|
+
$wingetPath = Find-WinGet
|
|
522
|
+
if ($null -ne $wingetPath) {
|
|
523
|
+
$winget = & $wingetPath -v
|
|
524
|
+
} else {
|
|
525
|
+
$winget = $null
|
|
526
|
+
}
|
|
527
|
+
} else {
|
|
528
|
+
$winget = Get-Command -Name winget -ErrorAction SilentlyContinue
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
# If winget is installed, return $true
|
|
532
|
+
if ($null -ne $winget -and $winget -notlike '*failed to run*') {
|
|
533
|
+
return $true
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
# If winget is not installed, return $false
|
|
537
|
+
return $false
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function Handle-Error {
|
|
541
|
+
<#
|
|
542
|
+
.SYNOPSIS
|
|
543
|
+
Handles common errors that may occur during an installation process.
|
|
544
|
+
|
|
545
|
+
.DESCRIPTION
|
|
546
|
+
This function takes an ErrorRecord object and checks for certain known error codes.
|
|
547
|
+
Depending on the error code, it writes appropriate warning messages or throws the error.
|
|
548
|
+
|
|
549
|
+
.PARAMETER ErrorRecord
|
|
550
|
+
The ErrorRecord object that represents the error that was caught. This object contains
|
|
551
|
+
information about the error, including the exception that was thrown.
|
|
552
|
+
|
|
553
|
+
.EXAMPLE
|
|
554
|
+
try {
|
|
555
|
+
# Some code that may throw an error...
|
|
556
|
+
} catch {
|
|
557
|
+
Handle-Error $_
|
|
558
|
+
}
|
|
559
|
+
This example shows how you might use the Handle-Error function in a try-catch block.
|
|
560
|
+
If an error occurs in the try block, the catch block catches it and calls Handle-Error,
|
|
561
|
+
passing the error (represented by the $_ variable) to the function.
|
|
562
|
+
#>
|
|
563
|
+
param($ErrorRecord)
|
|
564
|
+
|
|
565
|
+
# Store current value
|
|
566
|
+
$OriginalErrorActionPreference = $ErrorActionPreference
|
|
567
|
+
|
|
568
|
+
# Set to silently continue
|
|
569
|
+
$ErrorActionPreference = 'SilentlyContinue'
|
|
570
|
+
|
|
571
|
+
# Handle common errors
|
|
572
|
+
# Not returning $ErrorRecord on some errors is intentional
|
|
573
|
+
if ($ErrorRecord.Exception.Message -match '0x80073D06') {
|
|
574
|
+
Write-Warning "Higher version already installed."
|
|
575
|
+
Write-Warning "That's okay, continuing..."
|
|
576
|
+
} elseif ($ErrorRecord.Exception.Message -match '0x80073CF0') {
|
|
577
|
+
Write-Warning "Same version already installed."
|
|
578
|
+
Write-Warning "That's okay, continuing..."
|
|
579
|
+
} elseif ($ErrorRecord.Exception.Message -match '0x80073D02') {
|
|
580
|
+
# Stop execution and return the ErrorRecord so that the calling try/catch block throws the error
|
|
581
|
+
Write-Warning "Resources modified are in-use. Try closing Windows Terminal / PowerShell / Command Prompt and try again."
|
|
582
|
+
Write-Warning "Windows Terminal sometimes has trouble installing winget. If you are using Windows Terminal and the problem persists, run the script with the -ForceClose parameter which will relaunch the script in conhost.exe and automatically end active processes associated with winget that could interfere with the installation. Please note that using the -ForceClose parameter will close the PowerShell window and could break custom scripts that rely on the current PowerShell session."
|
|
583
|
+
return $ErrorRecord
|
|
584
|
+
} elseif ($ErrorRecord.Exception.Message -match '0x80073CF3') {
|
|
585
|
+
# Prerequisite not detected, tell user to run it again
|
|
586
|
+
Write-Warning "Problem with one of the prerequisites."
|
|
587
|
+
Write-Warning "Try running the script again which usually fixes the issue. If the problem persists, try running the script with the -ForceClose parameter which will relaunch the script in conhost.exe and automatically end active processes associated with winget that could interfere with the installation. Please note that using the -ForceClose parameter will close the PowerShell window and could break custom scripts that rely on the current PowerShell session."
|
|
588
|
+
return $ErrorRecord
|
|
589
|
+
} elseif ($ErrorRecord.Exception.Message -match '0x80073CF9') {
|
|
590
|
+
Write-Warning "Registering winget failed with error code 0x80073CF9."
|
|
591
|
+
Write-Warning "This error usually occurs when using the Local System account to install winget. The SYSTEM account is not officially supported by winget and may not work. See the requirements section of the README. If winget is not working, run the installation script again using an Administrator account."
|
|
592
|
+
} elseif ($ErrorRecord.Exception.Message -match 'Unable to connect to the remote server') {
|
|
593
|
+
Write-Warning "Cannot connect to the Internet to download the required files."
|
|
594
|
+
Write-Warning "Try running the script again and make sure you are connected to the Internet."
|
|
595
|
+
Write-Warning "Sometimes the nuget.org server is down, so you may need to try again later."
|
|
596
|
+
return $ErrorRecord
|
|
597
|
+
} elseif ($ErrorRecord.Exception.Message -match "The remote name could not be resolved") {
|
|
598
|
+
Write-Warning "Cannot connect to the Internet to download the required files."
|
|
599
|
+
Write-Warning "Try running the script again and make sure you are connected to the Internet."
|
|
600
|
+
Write-Warning "Make sure DNS is working correctly on your computer."
|
|
601
|
+
} else {
|
|
602
|
+
# For other errors, we should stop the execution and return the ErrorRecord so that the calling try/catch block throws the error
|
|
603
|
+
return $ErrorRecord
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
# Reset to original value
|
|
607
|
+
$ErrorActionPreference = $OriginalErrorActionPreference
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
function Get-CurrentProcess {
|
|
611
|
+
<#
|
|
612
|
+
.SYNOPSIS
|
|
613
|
+
Retrieves the current PowerShell process information.
|
|
614
|
+
|
|
615
|
+
.DESCRIPTION
|
|
616
|
+
The Get-CurrentProcess function identifies the current PowerShell process by temporarily changing the console window title. It then filters the list of running processes to find the one with the matching window title. The function returns a custom object containing the Name and Id of the current process.
|
|
617
|
+
|
|
618
|
+
.EXAMPLE
|
|
619
|
+
PS C:\> $result = Get-CurrentProcess
|
|
620
|
+
PS C:\> Write-Output $result
|
|
621
|
+
|
|
622
|
+
This example demonstrates how to call the Get-CurrentProcess function and store its output in a variable named $result. The output is then displayed using Write-Output.
|
|
623
|
+
|
|
624
|
+
.NOTES
|
|
625
|
+
The function temporarily changes the console window title. Ensure no other scripts or processes are dependent on the window title during execution. The function uses a 1-second sleep to allow time for the window title change to take effect. This may vary based on system performance.
|
|
626
|
+
#>
|
|
627
|
+
$oldTitle = $host.ui.RawUI.WindowTitle
|
|
628
|
+
$tempTitle = ([Guid]::NewGuid())
|
|
629
|
+
$host.ui.RawUI.WindowTitle = $tempTitle
|
|
630
|
+
Start-Sleep 1
|
|
631
|
+
$currentProcess = Get-Process | Where-Object { $_.MainWindowTitle -eq $tempTitle }
|
|
632
|
+
$currentProcess = [PSCustomObject]@{
|
|
633
|
+
Name = $currentProcess.Name
|
|
634
|
+
Id = $currentProcess.Id
|
|
635
|
+
}
|
|
636
|
+
$host.ui.RawUI.WindowTitle = $oldTitle
|
|
637
|
+
return $currentProcess
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
function ExitWithDelay {
|
|
641
|
+
<#
|
|
642
|
+
.SYNOPSIS
|
|
643
|
+
Exits the script with a specified exit code after a specified delay, 10 seconds by default.
|
|
644
|
+
|
|
645
|
+
.DESCRIPTION
|
|
646
|
+
This function takes an exit code as an argument, waits for 10 seconds unless specified, and then exits the script with the given exit code.
|
|
647
|
+
|
|
648
|
+
.PARAMETER ExitCode
|
|
649
|
+
The exit code to use when exiting the script.
|
|
650
|
+
|
|
651
|
+
.EXAMPLE
|
|
652
|
+
ExitWithDelay -ExitCode 1
|
|
653
|
+
Waits for 10 seconds (default) and then exits the script with an exit code of 1.
|
|
654
|
+
|
|
655
|
+
.EXAMPLE
|
|
656
|
+
ExitWithDelay -ExitCode 2 -Seconds 5
|
|
657
|
+
Waits for 5 seconds and then exits the script with an exit code of 2.
|
|
658
|
+
.NOTES
|
|
659
|
+
Use this function to introduce a delay before exiting the script, allowing time for any cleanup or logging activities.
|
|
660
|
+
#>
|
|
661
|
+
|
|
662
|
+
param (
|
|
663
|
+
[int]$ExitCode,
|
|
664
|
+
[int]$Seconds = 10
|
|
665
|
+
)
|
|
666
|
+
|
|
667
|
+
# Debug mode output
|
|
668
|
+
if ($Debug -and $Wait) {
|
|
669
|
+
Write-Warning "Wait specified, waiting several seconds..."
|
|
670
|
+
} elseif ($Debug -and !$Wait) {
|
|
671
|
+
Write-Warning "Wait not specified, exiting immediately..."
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
# If Wait is specified, wait for x seconds before exiting
|
|
675
|
+
if ($Wait) {
|
|
676
|
+
# Waiting for x seconds output
|
|
677
|
+
Write-Output "`nWaiting for $Seconds seconds before exiting..."
|
|
678
|
+
Start-Sleep -Seconds $Seconds
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
# If NoExit is specified, do not exit the script
|
|
682
|
+
if ($NoExit) {
|
|
683
|
+
Write-Output "Script completed. Pausing indefinitely. Press any key to exit..."
|
|
684
|
+
Read-Host
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
# Exit the script with exit code
|
|
688
|
+
if ($MyInvocation.CommandOrigin -eq "Runspace") {
|
|
689
|
+
Break
|
|
690
|
+
} else {
|
|
691
|
+
Exit $ExitCode
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
function Import-GlobalVariable {
|
|
696
|
+
<#
|
|
697
|
+
.SYNOPSIS
|
|
698
|
+
This function checks if a specified global variable exists and imports its value into a script scope variable of the same name.
|
|
699
|
+
|
|
700
|
+
.DESCRIPTION
|
|
701
|
+
The Import-GlobalVariable function allows you to specify the name of a variable. It checks if a global variable with that name exists, and if it does, it imports its value into a script scope variable with the same name.
|
|
702
|
+
|
|
703
|
+
.PARAMETER VariableName
|
|
704
|
+
The name of the variable to check and import if it exists in the global scope.
|
|
705
|
+
|
|
706
|
+
#>
|
|
707
|
+
|
|
708
|
+
[CmdletBinding()]
|
|
709
|
+
param(
|
|
710
|
+
[string]$VariableName
|
|
711
|
+
)
|
|
712
|
+
|
|
713
|
+
# Check if the specified global variable exists; if yes, import its value
|
|
714
|
+
try {
|
|
715
|
+
$globalValue = Get-Variable -Name $VariableName -ValueOnly -Scope Global -ErrorAction Stop
|
|
716
|
+
Set-Variable -Name $VariableName -Value $globalValue -Scope Script
|
|
717
|
+
} catch {
|
|
718
|
+
# If the variable does not exist, do nothing
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
function Test-AdminPrivileges {
|
|
723
|
+
<#
|
|
724
|
+
.SYNOPSIS
|
|
725
|
+
Checks if the script is running with Administrator privileges. Returns $true if running with Administrator privileges, $false otherwise.
|
|
726
|
+
|
|
727
|
+
.DESCRIPTION
|
|
728
|
+
This function checks if the current PowerShell session is running with Administrator privileges by examining the role of the current user. It returns $true if the current user is an Administrator, $false otherwise.
|
|
729
|
+
|
|
730
|
+
.EXAMPLE
|
|
731
|
+
Test-AdminPrivileges
|
|
732
|
+
|
|
733
|
+
.NOTES
|
|
734
|
+
This function is particularly useful for scripts that require elevated permissions to run correctly.
|
|
735
|
+
#>
|
|
736
|
+
if (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
|
737
|
+
return $true
|
|
738
|
+
}
|
|
739
|
+
return $false
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
Function New-TemporaryFile2 {
|
|
743
|
+
<#
|
|
744
|
+
.SYNOPSIS
|
|
745
|
+
Creates a new temporary file.
|
|
746
|
+
|
|
747
|
+
.DESCRIPTION
|
|
748
|
+
This function generates a temporary file and returns the full path of the file. The New-TemporaryFile command is not available in PowerShell 5.1 and earlier so this function is a workaround.
|
|
749
|
+
|
|
750
|
+
.EXAMPLE
|
|
751
|
+
$tempFile = New-TemporaryFile2
|
|
752
|
+
This example creates a new temporary file and stores its path in the $tempFile variable.
|
|
753
|
+
|
|
754
|
+
.OUTPUTS
|
|
755
|
+
String. The full path of the created temporary file.
|
|
756
|
+
#>
|
|
757
|
+
|
|
758
|
+
# Create a temporary file
|
|
759
|
+
$tempPath = [System.IO.Path]::GetTempPath()
|
|
760
|
+
$tempFile = [System.IO.Path]::Combine($tempPath, [System.IO.Path]::GetRandomFileName())
|
|
761
|
+
$null = New-Item -Path $tempFile -ItemType File -Force
|
|
762
|
+
|
|
763
|
+
# Return the path of the temporary file
|
|
764
|
+
return $tempFile
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
function Path-ExistsInEnvironment {
|
|
768
|
+
param (
|
|
769
|
+
[string]$PathToCheck,
|
|
770
|
+
[string]$Scope = 'Both' # Valid values: 'User', 'System', 'Both'
|
|
771
|
+
)
|
|
772
|
+
<#
|
|
773
|
+
.SYNOPSIS
|
|
774
|
+
Checks if the specified path exists in the specified PATH environment variable.
|
|
775
|
+
|
|
776
|
+
.DESCRIPTION
|
|
777
|
+
This function checks if a given path is present in the user, system-wide, or both PATH environment variables.
|
|
778
|
+
|
|
779
|
+
.PARAMETER PathToCheck
|
|
780
|
+
The directory path to check in the environment PATH variable.
|
|
781
|
+
|
|
782
|
+
.PARAMETER Scope
|
|
783
|
+
The scope to check in the environment PATH variable. Valid values are 'User', 'System', or 'Both'. Default is 'Both'.
|
|
784
|
+
|
|
785
|
+
.EXAMPLE
|
|
786
|
+
Path-ExistsInEnvironment -PathToCheck "C:\Program Files\MyApp" -Scope 'User'
|
|
787
|
+
#>
|
|
788
|
+
|
|
789
|
+
$pathExists = $false
|
|
790
|
+
|
|
791
|
+
if ($Scope -eq 'User' -or $Scope -eq 'Both') {
|
|
792
|
+
$userEnvPath = $env:PATH
|
|
793
|
+
if (($userEnvPath -split ';').Contains($PathToCheck)) {
|
|
794
|
+
$pathExists = $true
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
if ($Scope -eq 'System' -or $Scope -eq 'Both') {
|
|
799
|
+
$systemEnvPath = [System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::Machine)
|
|
800
|
+
if (($systemEnvPath -split ';').Contains($PathToCheck)) {
|
|
801
|
+
$pathExists = $true
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
return $pathExists
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
function Add-ToEnvironmentPath {
|
|
809
|
+
param (
|
|
810
|
+
[Parameter(Mandatory = $true)]
|
|
811
|
+
[string]$PathToAdd,
|
|
812
|
+
|
|
813
|
+
[Parameter(Mandatory = $true)]
|
|
814
|
+
[ValidateSet('User', 'System')]
|
|
815
|
+
[string]$Scope
|
|
816
|
+
)
|
|
817
|
+
<#
|
|
818
|
+
.SYNOPSIS
|
|
819
|
+
Adds the specified path to the environment PATH variable.
|
|
820
|
+
|
|
821
|
+
.DESCRIPTION
|
|
822
|
+
This function adds a given path to the specified scope (user or system) and the current process environment PATH variable if it is not already present.
|
|
823
|
+
|
|
824
|
+
.PARAMETER PathToAdd
|
|
825
|
+
The directory path to add to the environment PATH variable.
|
|
826
|
+
|
|
827
|
+
.PARAMETER Scope
|
|
828
|
+
Specifies whether to add the path to the user or system environment PATH variable.
|
|
829
|
+
|
|
830
|
+
.EXAMPLE
|
|
831
|
+
Add-ToEnvironmentPath -PathToAdd "C:\Program Files\MyApp" -Scope 'System'
|
|
832
|
+
#>
|
|
833
|
+
|
|
834
|
+
# Check if the path is already in the environment PATH variable
|
|
835
|
+
if (-not (Path-ExistsInEnvironment -PathToCheck $PathToAdd -Scope $Scope)) {
|
|
836
|
+
if ($Scope -eq 'System') {
|
|
837
|
+
# Get the current system PATH
|
|
838
|
+
$systemEnvPath = [System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::Machine)
|
|
839
|
+
# Add to system PATH
|
|
840
|
+
$systemEnvPath += ";$PathToAdd"
|
|
841
|
+
[System.Environment]::SetEnvironmentVariable('PATH', $systemEnvPath, [System.EnvironmentVariableTarget]::Machine)
|
|
842
|
+
Write-Debug "Adding $PathToAdd to the system PATH."
|
|
843
|
+
} elseif ($Scope -eq 'User') {
|
|
844
|
+
# Get the current user PATH
|
|
845
|
+
$userEnvPath = [System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::User)
|
|
846
|
+
# Add to user PATH
|
|
847
|
+
$userEnvPath += ";$PathToAdd"
|
|
848
|
+
[System.Environment]::SetEnvironmentVariable('PATH', $userEnvPath, [System.EnvironmentVariableTarget]::User)
|
|
849
|
+
Write-Debug "Adding $PathToAdd to the user PATH."
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
# Update the current process environment PATH
|
|
853
|
+
if (-not ($env:PATH -split ';').Contains($PathToAdd)) {
|
|
854
|
+
$env:PATH += ";$PathToAdd"
|
|
855
|
+
Write-Debug "Adding $PathToAdd to the current process environment PATH."
|
|
856
|
+
}
|
|
857
|
+
} else {
|
|
858
|
+
Write-Debug "$PathToAdd is already in the PATH."
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
function Set-PathPermissions {
|
|
863
|
+
param (
|
|
864
|
+
[string]$FolderPath
|
|
865
|
+
)
|
|
866
|
+
<#
|
|
867
|
+
.SYNOPSIS
|
|
868
|
+
Grants full control permissions for the Administrators group on the specified directory path.
|
|
869
|
+
|
|
870
|
+
.DESCRIPTION
|
|
871
|
+
This function sets full control permissions for the Administrators group on the specified directory path.
|
|
872
|
+
Useful for ensuring that administrators have unrestricted access to a given folder.
|
|
873
|
+
|
|
874
|
+
.PARAMETER FolderPath
|
|
875
|
+
The directory path for which to set full control permissions.
|
|
876
|
+
|
|
877
|
+
.EXAMPLE
|
|
878
|
+
Set-PathPermissions -FolderPath "C:\Program Files\MyApp"
|
|
879
|
+
|
|
880
|
+
Sets full control permissions for the Administrators group on "C:\Program Files\MyApp".
|
|
881
|
+
#>
|
|
882
|
+
|
|
883
|
+
Write-Debug "Setting full control permissions for the Administrators group on $FolderPath."
|
|
884
|
+
|
|
885
|
+
# Define the SID for the Administrators group
|
|
886
|
+
$administratorsGroupSid = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-544")
|
|
887
|
+
$administratorsGroup = $administratorsGroupSid.Translate([System.Security.Principal.NTAccount])
|
|
888
|
+
|
|
889
|
+
# Retrieve the current ACL for the folder
|
|
890
|
+
try {
|
|
891
|
+
$acl = Get-Acl -Path $FolderPath -ErrorAction Stop
|
|
892
|
+
} catch {
|
|
893
|
+
Write-Warning "Failed to retrieve ACL for '$FolderPath'. Error: $($_.Exception.Message)"
|
|
894
|
+
return
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
# Define the access rule for full control inheritance
|
|
898
|
+
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
|
|
899
|
+
$administratorsGroup,
|
|
900
|
+
"FullControl",
|
|
901
|
+
"ContainerInherit,ObjectInherit",
|
|
902
|
+
"None",
|
|
903
|
+
"Allow"
|
|
904
|
+
)
|
|
905
|
+
|
|
906
|
+
# Apply the access rule to the ACL and set it on the folder
|
|
907
|
+
try {
|
|
908
|
+
$acl.SetAccessRule($accessRule)
|
|
909
|
+
Set-Acl -Path $FolderPath -AclObject $acl -ErrorAction Stop
|
|
910
|
+
} catch {
|
|
911
|
+
Write-Warning "Failed to apply ACL to '$FolderPath'. Error: $($_.Exception.Message)"
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
function Test-VCRedistInstalled {
|
|
916
|
+
<#
|
|
917
|
+
.SYNOPSIS
|
|
918
|
+
Checks if Visual C++ Redistributable is installed and verifies the major version is 14.
|
|
919
|
+
|
|
920
|
+
.DESCRIPTION
|
|
921
|
+
This function checks the registry and the file system to determine if Visual C++ Redistributable is installed
|
|
922
|
+
and if the major version is 14.
|
|
923
|
+
|
|
924
|
+
.EXAMPLE
|
|
925
|
+
Test-VCRedistInstalled
|
|
926
|
+
#>
|
|
927
|
+
|
|
928
|
+
# Assets
|
|
929
|
+
$64BitOS = [System.Environment]::Is64BitOperatingSystem
|
|
930
|
+
$64BitProcess = [System.Environment]::Is64BitProcess
|
|
931
|
+
|
|
932
|
+
# Require running system native process
|
|
933
|
+
if ($64BitOS -and -not $64BitProcess) {
|
|
934
|
+
Throw 'Please run PowerShell in the system native architecture (x64 PowerShell if x64 Windows).'
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
# Check that uninstall information exists in the registry
|
|
938
|
+
$registryPath = [string]::Format(
|
|
939
|
+
'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\{0}\Microsoft\VisualStudio\14.0\VC\Runtimes\X{1}',
|
|
940
|
+
$(if ($64BitOS -and $64BitProcess) { 'WOW6432Node' } else { '' }),
|
|
941
|
+
$(if ($64BitOS) { '64' } else { '86' })
|
|
942
|
+
)
|
|
943
|
+
|
|
944
|
+
$registryExists = Test-Path -Path $registryPath
|
|
945
|
+
|
|
946
|
+
Write-Debug "Registry Path: $registryPath"
|
|
947
|
+
Write-Debug "Registry Exists: $registryExists"
|
|
948
|
+
|
|
949
|
+
# Check the major version
|
|
950
|
+
$majorVersion = if ($registryExists) {
|
|
951
|
+
(Get-ItemProperty -Path $registryPath -Name 'Major').Major
|
|
952
|
+
} else {
|
|
953
|
+
0
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
# Check the full version
|
|
957
|
+
$version = if ($registryExists) {
|
|
958
|
+
(Get-ItemProperty -Path $registryPath -Name 'Version').Version
|
|
959
|
+
} else {
|
|
960
|
+
0
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
Write-Debug "Major Version: $majorVersion"
|
|
964
|
+
Write-Debug "Version: $version"
|
|
965
|
+
|
|
966
|
+
# Check that one required DLL exists on the file system
|
|
967
|
+
$dllPath = [string]::Format(
|
|
968
|
+
'{0}\system32\concrt140.dll',
|
|
969
|
+
$env:windir
|
|
970
|
+
)
|
|
971
|
+
|
|
972
|
+
$dllExists = [System.IO.File]::Exists($dllPath)
|
|
973
|
+
|
|
974
|
+
Write-Debug "DLL Path: $dllPath"
|
|
975
|
+
Write-Debug "DLL Exists: $dllExists"
|
|
976
|
+
|
|
977
|
+
# Determine if VCRedist is installed and major version is 14
|
|
978
|
+
return $registryExists -and $majorVersion -eq 14 -and $dllExists
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
function TryRemove {
|
|
982
|
+
<#
|
|
983
|
+
.SYNOPSIS
|
|
984
|
+
Tries to remove a specified file if it exists.
|
|
985
|
+
|
|
986
|
+
.DESCRIPTION
|
|
987
|
+
This function checks if the specified file exists and attempts to remove it.
|
|
988
|
+
It will not produce an error if the file does not exist or if the removal fails.
|
|
989
|
+
|
|
990
|
+
.PARAMETER FilePath
|
|
991
|
+
The path to the file that should be removed.
|
|
992
|
+
|
|
993
|
+
.EXAMPLE
|
|
994
|
+
TryRemove -FilePath "C:\path\to\file.txt"
|
|
995
|
+
#>
|
|
996
|
+
|
|
997
|
+
param (
|
|
998
|
+
[Parameter(Mandatory = $true)]
|
|
999
|
+
[string]$FilePath
|
|
1000
|
+
)
|
|
1001
|
+
|
|
1002
|
+
try {
|
|
1003
|
+
if (Test-Path -Path $FilePath) {
|
|
1004
|
+
Remove-Item -Path $FilePath -ErrorAction SilentlyContinue
|
|
1005
|
+
if ($?) {
|
|
1006
|
+
Write-Debug "File '$FilePath' was successfully removed."
|
|
1007
|
+
} else {
|
|
1008
|
+
Write-Debug "Failed to remove the file '$FilePath'."
|
|
1009
|
+
}
|
|
1010
|
+
} else {
|
|
1011
|
+
Write-Debug "File '$FilePath' does not exist."
|
|
1012
|
+
}
|
|
1013
|
+
} catch {
|
|
1014
|
+
Write-Debug "An error occurred while trying to remove the file '$FilePath'."
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
function Install-NuGetIfRequired {
|
|
1019
|
+
<#
|
|
1020
|
+
.SYNOPSIS
|
|
1021
|
+
Checks if the NuGet PackageProvider is installed and installs it if required.
|
|
1022
|
+
|
|
1023
|
+
.DESCRIPTION
|
|
1024
|
+
This function checks whether the NuGet PackageProvider is already installed on the system. If it is not found and the current PowerShell version is less than 7, it attempts to install the NuGet provider using Install-PackageProvider.
|
|
1025
|
+
For PowerShell 7 or greater, it assumes NuGet is available by default and advises reinstallation if NuGet is missing.
|
|
1026
|
+
|
|
1027
|
+
.PARAMETER Debug
|
|
1028
|
+
Enables debug output for additional details during installation.
|
|
1029
|
+
|
|
1030
|
+
.EXAMPLE
|
|
1031
|
+
Install-NuGetIfRequired
|
|
1032
|
+
# Checks for the NuGet provider and installs it if necessary.
|
|
1033
|
+
|
|
1034
|
+
.NOTES
|
|
1035
|
+
This function only attempts to install NuGet if the PowerShell version is less than 7.
|
|
1036
|
+
For PowerShell 7 or greater, NuGet is typically included by default and does not require installation.
|
|
1037
|
+
#>
|
|
1038
|
+
|
|
1039
|
+
# Check if NuGet PackageProvider is already installed, skip package provider installation if found
|
|
1040
|
+
if (-not (Get-PackageProvider -Name NuGet -ListAvailable -ErrorAction SilentlyContinue)) {
|
|
1041
|
+
Write-Debug "NuGet PackageProvider not found."
|
|
1042
|
+
|
|
1043
|
+
# Check if running in PowerShell version less than 7
|
|
1044
|
+
if ($PSVersionTable.PSVersion.Major -lt 7) {
|
|
1045
|
+
# Install NuGet PackageProvider if running PowerShell version less than 7
|
|
1046
|
+
# PowerShell 7 has limited support for installing package providers, but NuGet is available by default in PowerShell 7 so installation is not required
|
|
1047
|
+
|
|
1048
|
+
Write-Debug "Installing NuGet PackageProvider..."
|
|
1049
|
+
|
|
1050
|
+
if ($Debug) {
|
|
1051
|
+
try { Install-PackageProvider -Name "NuGet" -Force -ForceBootstrap -ErrorAction SilentlyContinue } catch { }
|
|
1052
|
+
} else {
|
|
1053
|
+
try { Install-PackageProvider -Name "NuGet" -Force -ForceBootstrap -ErrorAction SilentlyContinue | Out-Null } catch {}
|
|
1054
|
+
}
|
|
1055
|
+
} else {
|
|
1056
|
+
# NuGet should be included by default in PowerShell 7, so if it's not detected, advise reinstallation
|
|
1057
|
+
Write-Warning "NuGet is not detected in PowerShell 7. Consider reinstalling PowerShell 7, as NuGet should be included by default."
|
|
1058
|
+
}
|
|
1059
|
+
} else {
|
|
1060
|
+
# NuGet PackageProvider is already installed
|
|
1061
|
+
Write-Debug "NuGet PackageProvider is already installed. Skipping installation."
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
function Get-ManifestName {
|
|
1066
|
+
<#
|
|
1067
|
+
.SYNOPSIS
|
|
1068
|
+
Retrieves the name of the library from the AppxManifest.xml file from a specified library path.
|
|
1069
|
+
|
|
1070
|
+
.DESCRIPTION
|
|
1071
|
+
This function opens a ZIP file at the specified path, reads the AppxManifest.xml file inside it,
|
|
1072
|
+
and retrieves the name of the library from the manifest.
|
|
1073
|
+
|
|
1074
|
+
.PARAMETER Lib_Path
|
|
1075
|
+
The path to the library (ZIP) file containing the AppxManifest.xml.
|
|
1076
|
+
|
|
1077
|
+
.EXAMPLE
|
|
1078
|
+
Get-ManifestName -Lib_Path "C:\path\to\library.zip"
|
|
1079
|
+
#>
|
|
1080
|
+
|
|
1081
|
+
param(
|
|
1082
|
+
[Parameter(Mandatory)]
|
|
1083
|
+
[string]$Lib_Path
|
|
1084
|
+
)
|
|
1085
|
+
|
|
1086
|
+
Write-Debug "Checking manifest name of $($Lib_Path)..."
|
|
1087
|
+
|
|
1088
|
+
# Load ZIP assembly to read the package contents
|
|
1089
|
+
Add-Type -AssemblyName System.IO.Compression.FileSystem
|
|
1090
|
+
$zip = [System.IO.Compression.ZipFile]::OpenRead($Lib_Path)
|
|
1091
|
+
|
|
1092
|
+
Write-Debug "Reading AppxManifest.xml from $($Lib_Path)..."
|
|
1093
|
+
# Find and read the AppxManifest.xml
|
|
1094
|
+
$entry = $zip.Entries | Where-Object { $_.FullName -eq "AppxManifest.xml" }
|
|
1095
|
+
|
|
1096
|
+
if ($entry) {
|
|
1097
|
+
$stream = $entry.Open()
|
|
1098
|
+
$reader = New-Object System.IO.StreamReader($stream)
|
|
1099
|
+
[xml]$xml = $reader.ReadToEnd()
|
|
1100
|
+
$reader.Close()
|
|
1101
|
+
$zip.Dispose()
|
|
1102
|
+
|
|
1103
|
+
# Output the name from the manifest
|
|
1104
|
+
$DownloadedLibName = $xml.Package.Identity.Name
|
|
1105
|
+
Write-Debug "Downloaded library name: $DownloadedLibName"
|
|
1106
|
+
} else {
|
|
1107
|
+
Write-Error "AppxManifest.xml not found inside the file: $Lib_Path"
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
return $DownloadedLibName
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
function Get-ManifestVersion {
|
|
1114
|
+
<#
|
|
1115
|
+
.SYNOPSIS
|
|
1116
|
+
Retrieves the version of the AppxManifest.xml file from a specified library path.
|
|
1117
|
+
|
|
1118
|
+
.DESCRIPTION
|
|
1119
|
+
This function opens a ZIP file at the specified path, reads the AppxManifest.xml file inside it,
|
|
1120
|
+
and retrieves the version information from the manifest.
|
|
1121
|
+
|
|
1122
|
+
.PARAMETER Lib_Path
|
|
1123
|
+
The path to the library (ZIP) file containing the AppxManifest.xml.
|
|
1124
|
+
|
|
1125
|
+
.EXAMPLE
|
|
1126
|
+
Get-ManifestVersion -Lib_Path "C:\path\to\library.zip"
|
|
1127
|
+
#>
|
|
1128
|
+
|
|
1129
|
+
param(
|
|
1130
|
+
[Parameter(Mandatory)]
|
|
1131
|
+
[string]$Lib_Path
|
|
1132
|
+
)
|
|
1133
|
+
|
|
1134
|
+
Write-Debug "Checking manifest version of $($Lib_Path)..."
|
|
1135
|
+
|
|
1136
|
+
# Load ZIP assembly to read the package contents
|
|
1137
|
+
Add-Type -AssemblyName System.IO.Compression.FileSystem
|
|
1138
|
+
$zip = [System.IO.Compression.ZipFile]::OpenRead($Lib_Path)
|
|
1139
|
+
|
|
1140
|
+
Write-Debug "Reading AppxManifest.xml from $($Lib_Path)..."
|
|
1141
|
+
# Find and read the AppxManifest.xml
|
|
1142
|
+
$entry = $zip.Entries | Where-Object { $_.FullName -eq "AppxManifest.xml" }
|
|
1143
|
+
|
|
1144
|
+
if ($entry) {
|
|
1145
|
+
$stream = $entry.Open()
|
|
1146
|
+
$reader = New-Object System.IO.StreamReader($stream)
|
|
1147
|
+
[xml]$xml = $reader.ReadToEnd()
|
|
1148
|
+
$reader.Close()
|
|
1149
|
+
$zip.Dispose()
|
|
1150
|
+
|
|
1151
|
+
# Output the version from the manifest
|
|
1152
|
+
$DownloadedLibVersion = $xml.Package.Identity.Version
|
|
1153
|
+
Write-Debug "Downloaded library version: $DownloadedLibVersion"
|
|
1154
|
+
} else {
|
|
1155
|
+
Write-Error "AppxManifest.xml not found inside the file: $Lib_Path"
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
return $DownloadedLibVersion
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
function Get-InstalledLibVersion {
|
|
1162
|
+
<#
|
|
1163
|
+
.SYNOPSIS
|
|
1164
|
+
Retrieves the installed version of a specified library.
|
|
1165
|
+
|
|
1166
|
+
.DESCRIPTION
|
|
1167
|
+
This function checks if a specified library is installed on the system and retrieves its version information.
|
|
1168
|
+
|
|
1169
|
+
.PARAMETER Lib_Name
|
|
1170
|
+
The name of the library to check for installation and retrieve the version.
|
|
1171
|
+
|
|
1172
|
+
.EXAMPLE
|
|
1173
|
+
Get-InstalledLibVersion -Lib_Name "VCLibs.140.00.UWPDesktop"
|
|
1174
|
+
#>
|
|
1175
|
+
param(
|
|
1176
|
+
[Parameter(Mandatory)]
|
|
1177
|
+
[string]$Lib_Name
|
|
1178
|
+
)
|
|
1179
|
+
|
|
1180
|
+
Write-Debug "Checking installed library version of $($Lib_Name)..."
|
|
1181
|
+
|
|
1182
|
+
# Get the highest version of the installed library
|
|
1183
|
+
$InstalledLib = Get-AppxPackage -Name "*$($Lib_Name)*" -ErrorAction SilentlyContinue | Sort-Object Version -Descending | Select-Object -First 1
|
|
1184
|
+
|
|
1185
|
+
if ($InstalledLib) {
|
|
1186
|
+
$InstalledLibVersion = $InstalledLib.Version
|
|
1187
|
+
Write-Debug "Installed library version: $InstalledLibVersion"
|
|
1188
|
+
} else {
|
|
1189
|
+
Write-Output "Library is not installed."
|
|
1190
|
+
$InstalledLibVersion = $null
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
return $InstalledLibVersion
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
function Install-LibIfRequired {
|
|
1197
|
+
<#
|
|
1198
|
+
.SyNOPSIS
|
|
1199
|
+
Installs a specified library if it is not already installed or if the downloaded version is newer.
|
|
1200
|
+
|
|
1201
|
+
.DESCRIPTION
|
|
1202
|
+
This function checks if a specified library is installed on the system.
|
|
1203
|
+
If it is not installed or if the downloaded version is newer than the installed version, it installs the library.
|
|
1204
|
+
|
|
1205
|
+
.PARAMETER Lib_Name
|
|
1206
|
+
The name of the library to check for installation and retrieve the version.
|
|
1207
|
+
|
|
1208
|
+
.PARAMETER Lib_Path
|
|
1209
|
+
The path to the library (ZIP) file containing the AppxManifest.xml.
|
|
1210
|
+
|
|
1211
|
+
.EXAMPLE
|
|
1212
|
+
Install-LibIfRequired -Lib_Name "VCLibs.140.00.UWPDesktop" -Lib_Path "C:\path\to\library.zip"
|
|
1213
|
+
#>
|
|
1214
|
+
param(
|
|
1215
|
+
[Parameter(Mandatory)]
|
|
1216
|
+
[string]$Lib_Name,
|
|
1217
|
+
[Parameter(Mandatory)]
|
|
1218
|
+
[string]$Lib_Path
|
|
1219
|
+
)
|
|
1220
|
+
|
|
1221
|
+
# Check the installed version of Lib
|
|
1222
|
+
$InstalledLibVersion = Get-InstalledLibVersion -Lib_Name $Lib_Name
|
|
1223
|
+
|
|
1224
|
+
# Extract version from the downloaded file
|
|
1225
|
+
$DownloadedLibVersion = Get-ManifestVersion -Lib_Path $Lib_Path
|
|
1226
|
+
|
|
1227
|
+
# Compare versions and install if necessary
|
|
1228
|
+
if (!$InstalledLibVersion -or !$DownloadedLibVersion -or ($DownloadedLibVersion -gt $InstalledLibVersion)) {
|
|
1229
|
+
if ($RunAsSystem) {
|
|
1230
|
+
Write-Debug 'Running as system...'
|
|
1231
|
+
Write-Debug "Installing library version $($DownloadedLibVersion)..."
|
|
1232
|
+
$null = Add-ProvisionedAppxPackage -Online -SkipLicense -PackagePath $Lib_Path
|
|
1233
|
+
} else {
|
|
1234
|
+
Write-Debug 'Running as user...'
|
|
1235
|
+
Write-Debug "Installing library version $($DownloadedLibVersion)..."
|
|
1236
|
+
$null = Add-AppxPackage -Path $Lib_Path
|
|
1237
|
+
}
|
|
1238
|
+
} else {
|
|
1239
|
+
Write-Output "Installed library version is up-to-date or newer. Skipping installation."
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
function Apply-PathPermissionsFixAndAddPath {
|
|
1244
|
+
param(
|
|
1245
|
+
[Parameter(Mandatory)]
|
|
1246
|
+
[PSCustomObject]$OSVersion
|
|
1247
|
+
)
|
|
1248
|
+
|
|
1249
|
+
# ============================================================================ #
|
|
1250
|
+
# Fix environment PATH and permissions
|
|
1251
|
+
# ============================================================================ #
|
|
1252
|
+
|
|
1253
|
+
# Fix permissions for winget folder
|
|
1254
|
+
Write-Output "Fixing permissions for winget folder..."
|
|
1255
|
+
|
|
1256
|
+
# Set winget folder path
|
|
1257
|
+
if ($OSVersion.InstallationType -eq "Server Core") {
|
|
1258
|
+
# Set to portable path
|
|
1259
|
+
$WinGetFolderPath = [System.IO.Path]::Combine($env:ProgramFiles, "Microsoft", "winget")
|
|
1260
|
+
} else {
|
|
1261
|
+
# Find winget folder path in Program Files
|
|
1262
|
+
$arch = $OSVersion.Architecture
|
|
1263
|
+
$WinGetFolderPath = (Get-ChildItem -Path ([System.IO.Path]::Combine($env:ProgramFiles, 'WindowsApps')) -Filter "Microsoft.DesktopAppInstaller_*_*${arch}__8wekyb3d8bbwe" | Sort-Object Name | Select-Object -Last 1).FullName
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
# Output
|
|
1267
|
+
Write-Debug "WinGetFolderPath: $WinGetFolderPath"
|
|
1268
|
+
|
|
1269
|
+
if ($null -ne $WinGetFolderPath) {
|
|
1270
|
+
# Fix Permissions by adding Administrators group with FullControl
|
|
1271
|
+
Set-PathPermissions -FolderPath $WinGetFolderPath
|
|
1272
|
+
|
|
1273
|
+
# Add Environment Path
|
|
1274
|
+
Add-ToEnvironmentPath -PathToAdd $WinGetFolderPath -Scope 'System'
|
|
1275
|
+
|
|
1276
|
+
Write-Output "winget folder permissions and PATH updated successfully."
|
|
1277
|
+
Write-Output "A restart or new session may be required for changes to take effect."
|
|
1278
|
+
} else {
|
|
1279
|
+
Write-Warning "winget folder path not found. You may need to manually add winget's folder path to your system PATH environment variable."
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
# Output
|
|
1283
|
+
Write-Output "A restart may be required for winget path to continue to work as expected."
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
# ============================================================================ #
|
|
1287
|
+
# Initial checks
|
|
1288
|
+
# ============================================================================ #
|
|
1289
|
+
|
|
1290
|
+
# Use global variables if specified by user
|
|
1291
|
+
Import-GlobalVariable -VariableName "Debug"
|
|
1292
|
+
Import-GlobalVariable -VariableName "ForceClose"
|
|
1293
|
+
Import-GlobalVariable -VariableName "Force"
|
|
1294
|
+
Import-GlobalVariable -VariableName "AlternateInstallMethod"
|
|
1295
|
+
|
|
1296
|
+
# First heading
|
|
1297
|
+
Write-Output "winget-install $CurrentVersion"
|
|
1298
|
+
|
|
1299
|
+
# Check for updates if -CheckForUpdate is specified
|
|
1300
|
+
if ($CheckForUpdate) { CheckForUpdate -RepoOwner $RepoOwner -RepoName $RepoName -CurrentVersion $CurrentVersion -PowerShellGalleryName $PowerShellGalleryName -GHtoken $GHtoken}
|
|
1301
|
+
|
|
1302
|
+
# Update the script if -UpdateSelf is specified
|
|
1303
|
+
if ($UpdateSelf) { UpdateSelf }
|
|
1304
|
+
|
|
1305
|
+
# Heading
|
|
1306
|
+
Write-Output "To check for updates, run winget-install -CheckForUpdate"
|
|
1307
|
+
Write-Output "To delay script exit, run winget-install -Wait"
|
|
1308
|
+
Write-Output "To force script pausing after execution, run winget-install -NoExit"
|
|
1309
|
+
|
|
1310
|
+
# Check if the current user is an administrator
|
|
1311
|
+
if (-not (Test-AdminPrivileges)) {
|
|
1312
|
+
Write-Warning "winget requires Administrator privileges to install. Please run the script as an Administrator and try again."
|
|
1313
|
+
ExitWithDelay 1
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
# Get OS version
|
|
1317
|
+
$osVersion = Get-OSInfo
|
|
1318
|
+
|
|
1319
|
+
# Get architecture type
|
|
1320
|
+
$arch = $osVersion.Architecture
|
|
1321
|
+
|
|
1322
|
+
# Get current process module name to determine if launched in conhost
|
|
1323
|
+
$currentProcess = Get-CurrentProcess
|
|
1324
|
+
|
|
1325
|
+
# If it's a workstation, make sure it is Windows 10+
|
|
1326
|
+
if ($osVersion.Type -eq "Workstation" -and $osVersion.NumericVersion -lt 10) {
|
|
1327
|
+
Write-Error "winget requires Windows 10 or later on workstations. Your version of Windows is not supported."
|
|
1328
|
+
ExitWithDelay 1
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
# If it's a workstation with Windows 10, make sure it's version 1809 or greater
|
|
1332
|
+
if ($osVersion.Type -eq "Workstation" -and $osVersion.NumericVersion -eq 10 -and $osVersion.ReleaseId -lt 1809) {
|
|
1333
|
+
Write-Error "winget requires Windows 10 version 1809 or later on workstations. Please update Windows to a compatible version."
|
|
1334
|
+
ExitWithDelay 1
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
# If it's a server, it needs to be 2019+
|
|
1338
|
+
if ($osVersion.Type -eq "Server" -and $osVersion.NumericVersion -lt 2019) {
|
|
1339
|
+
Write-Error "winget requires Windows Server 2019 or newer on server platforms. Your version of Windows Server is not supported."
|
|
1340
|
+
ExitWithDelay 1
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
# Check if winget is already installed
|
|
1344
|
+
if (Get-WingetStatus) {
|
|
1345
|
+
if ($Force -eq $false) {
|
|
1346
|
+
Write-Warning "winget is already installed, exiting..."
|
|
1347
|
+
Write-Warning "If you want to reinstall winget, run the script with the -Force parameter."
|
|
1348
|
+
ExitWithDelay 0 5
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1352
|
+
# Check if ForceClose parameter is specified. If terminal detected, so relaunch in conhost
|
|
1353
|
+
if ($ForceClose) {
|
|
1354
|
+
Write-Warning "ForceClose parameter is specified."
|
|
1355
|
+
if ($currentProcess.Name -eq "WindowsTerminal") {
|
|
1356
|
+
Write-Warning "Terminal detected, relaunching in conhost in 10 seconds..."
|
|
1357
|
+
Write-Warning "It may break your custom batch files and ps1 scripts with extra commands!"
|
|
1358
|
+
Start-Sleep -Seconds 10
|
|
1359
|
+
|
|
1360
|
+
# Prepare the command to relaunch
|
|
1361
|
+
$command = "cd '$pwd'; $($MyInvocation.Line)"
|
|
1362
|
+
|
|
1363
|
+
# Append parameters if their corresponding variables are $true and not already in the command
|
|
1364
|
+
if ($Force -and !($command -imatch '\s-Force\b')) { $command += " -Force" }
|
|
1365
|
+
if ($ForceClose -and !($command -imatch '\s-ForceClose\b')) { $command += " -ForceClose" }
|
|
1366
|
+
if ($Debug -and !($command -imatch '\s-Debug\b')) { $command += " -Debug" }
|
|
1367
|
+
|
|
1368
|
+
# Relaunch in conhost
|
|
1369
|
+
if ([Environment]::Is64BitOperatingSystem) {
|
|
1370
|
+
if ([Environment]::Is64BitProcess) {
|
|
1371
|
+
Start-Process -FilePath "conhost.exe" -ArgumentList "powershell -ExecutionPolicy Bypass -Command &{$command}" -Verb RunAs
|
|
1372
|
+
} else {
|
|
1373
|
+
Start-Process -FilePath "$env:windir\sysnative\conhost.exe" -ArgumentList "powershell -ExecutionPolicy Bypass -Command &{$command}" -Verb RunAs
|
|
1374
|
+
}
|
|
1375
|
+
} else {
|
|
1376
|
+
Start-Process -FilePath "conhost.exe" -ArgumentList "powershell -ExecutionPolicy Bypass -Command &{$command}" -Verb RunAs
|
|
1377
|
+
}
|
|
1378
|
+
|
|
1379
|
+
# Stop the current process module
|
|
1380
|
+
Stop-Process -id $currentProcess.Id
|
|
1381
|
+
} else {
|
|
1382
|
+
Write-Warning "Windows Terminal not detected, continuing..."
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1386
|
+
# ============================================================================ #
|
|
1387
|
+
# Beginning of installation process
|
|
1388
|
+
# ============================================================================ #
|
|
1389
|
+
|
|
1390
|
+
try {
|
|
1391
|
+
# ============================================================================ #
|
|
1392
|
+
# Server Core Portable winget Installation (No AppX registration)
|
|
1393
|
+
# ============================================================================ #
|
|
1394
|
+
|
|
1395
|
+
if ($osVersion.Type -eq "Server" -and $osVersion.InstallationType -eq "Server Core") {
|
|
1396
|
+
Write-Output ""
|
|
1397
|
+
Write-Output "Detected Windows Server Core. Performing portable winget extraction."
|
|
1398
|
+
Write-Warning "Server Core portable installation is currently in beta. It will install successfully, but may not function properly due to missing dependencies. This feature is still in active development. For details, see: https://github.com/asheroto/winget-install/issues/53"
|
|
1399
|
+
|
|
1400
|
+
Write-Section "Portable winget"
|
|
1401
|
+
|
|
1402
|
+
$PortableWingetDirectory = Join-Path $env:ProgramFiles "Microsoft\winget"
|
|
1403
|
+
|
|
1404
|
+
# Determine architecture
|
|
1405
|
+
$arch = (Get-OSInfo).Architecture
|
|
1406
|
+
if ($arch -eq "x32") { $arch = "x86" }
|
|
1407
|
+
|
|
1408
|
+
# Define staging and extraction paths
|
|
1409
|
+
$BaseTemp = Join-Path $env:TEMP ("winget_extract_" + [guid]::NewGuid().ToString("N"))
|
|
1410
|
+
$DepsExtract = Join-Path $BaseTemp "deps"
|
|
1411
|
+
$AppxExtract = Join-Path $BaseTemp "appx"
|
|
1412
|
+
$FinalStaging = Join-Path $BaseTemp "staging"
|
|
1413
|
+
|
|
1414
|
+
New-Item -ItemType Directory -Force -Path $DepsExtract, $AppxExtract, $FinalStaging | Out-Null
|
|
1415
|
+
|
|
1416
|
+
Add-Type -AssemblyName System.IO.Compression.FileSystem
|
|
1417
|
+
function Expand-Zip {
|
|
1418
|
+
param(
|
|
1419
|
+
[Parameter(Mandatory = $true)][string]$Source,
|
|
1420
|
+
[Parameter(Mandatory = $true)][string]$Destination
|
|
1421
|
+
)
|
|
1422
|
+
|
|
1423
|
+
if (-not (Test-Path $Destination)) {
|
|
1424
|
+
New-Item -ItemType Directory -Force -Path $Destination | Out-Null
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
# Use ZipArchive with overwrite logic
|
|
1428
|
+
$archive = [System.IO.Compression.ZipFile]::OpenRead($Source)
|
|
1429
|
+
foreach ($entry in $archive.Entries) {
|
|
1430
|
+
$target = Join-Path $Destination $entry.FullName
|
|
1431
|
+
if ($entry.FullName.EndsWith("/")) {
|
|
1432
|
+
New-Item -ItemType Directory -Force -Path $target -ErrorAction SilentlyContinue | Out-Null
|
|
1433
|
+
} else {
|
|
1434
|
+
$folder = Split-Path $target -Parent
|
|
1435
|
+
if (-not (Test-Path $folder)) {
|
|
1436
|
+
New-Item -ItemType Directory -Force -Path $folder | Out-Null
|
|
1437
|
+
}
|
|
1438
|
+
$entryStream = $entry.Open()
|
|
1439
|
+
$targetStream = [System.IO.File]::Create($target)
|
|
1440
|
+
$entryStream.CopyTo($targetStream)
|
|
1441
|
+
$targetStream.Dispose()
|
|
1442
|
+
$entryStream.Dispose()
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
$archive.Dispose()
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
# ------------------------------------------------------------------------ #
|
|
1449
|
+
# Dependencies
|
|
1450
|
+
# ------------------------------------------------------------------------ #
|
|
1451
|
+
Write-Section "Dependencies"
|
|
1452
|
+
Write-Verbose "GHtoken=$GHtoken"
|
|
1453
|
+
|
|
1454
|
+
$DepsZip = Join-Path $BaseTemp "deps.zip"
|
|
1455
|
+
try {
|
|
1456
|
+
$DepsUrl = Get-WingetDownloadUrl -Match 'DesktopAppInstaller_Dependencies.zip' -WingetVersion $WingetVersion
|
|
1457
|
+
} catch {
|
|
1458
|
+
# If we can't get the dependencies of the exact version the user wants, fall back to try to find another version that has them
|
|
1459
|
+
$DepsUrl = Get-WingetDownloadUrl -Match 'DesktopAppInstaller_Dependencies.zip' -GHtoken $GHtoken
|
|
1460
|
+
}
|
|
1461
|
+
|
|
1462
|
+
# Ensure single URL string
|
|
1463
|
+
if ($DepsUrl -is [array]) {
|
|
1464
|
+
$DepsUrl = $DepsUrl | Select-Object -First 1
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
Write-Output "Downloading dependencies from $DepsUrl..."
|
|
1468
|
+
Invoke-WebRequest -Uri ([string]$DepsUrl) -OutFile $DepsZip -UseBasicParsing
|
|
1469
|
+
|
|
1470
|
+
$Zip = [System.IO.Compression.ZipFile]::OpenRead($DepsZip)
|
|
1471
|
+
$Entries = $Zip.Entries | Where-Object { $_.FullName -match "$arch/.*\.appx$" }
|
|
1472
|
+
|
|
1473
|
+
if (-not $Entries) {
|
|
1474
|
+
Write-Error "No dependencies found for $arch."
|
|
1475
|
+
ExitWithDelay 1
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
foreach ($Entry in $Entries) {
|
|
1479
|
+
$Out = Join-Path $DepsExtract $Entry.Name
|
|
1480
|
+
Write-Output "Extracting $($Entry.FullName)..."
|
|
1481
|
+
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($Entry, $Out, $true)
|
|
1482
|
+
Write-Output "Expanding $Out..."
|
|
1483
|
+
Expand-Zip $Out $AppxExtract
|
|
1484
|
+
}
|
|
1485
|
+
$Zip.Dispose()
|
|
1486
|
+
|
|
1487
|
+
# ------------------------------------------------------------------------ #
|
|
1488
|
+
# AppInstaller (winget)
|
|
1489
|
+
# ------------------------------------------------------------------------ #
|
|
1490
|
+
$WingetPkg = Join-Path $BaseTemp "appinstaller.msixbundle"
|
|
1491
|
+
$WingetUrl = Get-WingetDownloadUrl -Match '(AppInstaller|DesktopAppInstaller).*\.(msixbundle|msix)$' -WingetVersion $WingetVersion -GHtoken $GHtoken | Select-Object -First 1
|
|
1492
|
+
if (-not $WingetUrl) { Write-Error "No AppInstaller package found."; ExitWithDelay 1 }
|
|
1493
|
+
|
|
1494
|
+
Write-Output "Downloading AppInstaller package from $WingetUrl..."
|
|
1495
|
+
Invoke-WebRequest -Uri $WingetUrl -OutFile $WingetPkg -UseBasicParsing
|
|
1496
|
+
|
|
1497
|
+
$BundleExtract = Join-Path $BaseTemp "bundle"
|
|
1498
|
+
Expand-Zip $WingetPkg $BundleExtract
|
|
1499
|
+
|
|
1500
|
+
$NestedMsix = Get-ChildItem -Path $BundleExtract -Recurse -Include '*.msix' -ErrorAction SilentlyContinue |
|
|
1501
|
+
Where-Object { $_.Name -match $arch -and $_.Name -notmatch 'stub' } |
|
|
1502
|
+
Select-Object -First 1
|
|
1503
|
+
|
|
1504
|
+
if ($NestedMsix) {
|
|
1505
|
+
Write-Output "Extracting $($NestedMsix.Name)..."
|
|
1506
|
+
Expand-Zip $NestedMsix.FullName $FinalStaging
|
|
1507
|
+
} else {
|
|
1508
|
+
Write-Warning "No matching .msix found for $arch."
|
|
1509
|
+
}
|
|
1510
|
+
|
|
1511
|
+
# ------------------------------------------------------------------------ #
|
|
1512
|
+
# Merge dependency extractions
|
|
1513
|
+
# ------------------------------------------------------------------------ #
|
|
1514
|
+
Write-Output "Merging dependency files..."
|
|
1515
|
+
Copy-Item -Path (Join-Path $AppxExtract '*') -Destination $FinalStaging -Recurse -Force
|
|
1516
|
+
|
|
1517
|
+
# ------------------------------------------------------------------------ #
|
|
1518
|
+
# Copy to final directory
|
|
1519
|
+
# ------------------------------------------------------------------------ #
|
|
1520
|
+
Write-Output "Copying extracted files to $PortableWingetDirectory..."
|
|
1521
|
+
if (-not (Test-Path $PortableWingetDirectory)) {
|
|
1522
|
+
New-Item -ItemType Directory -Force -Path $PortableWingetDirectory | Out-Null
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1525
|
+
Copy-Item -Path (Join-Path $FinalStaging '*') -Destination $PortableWingetDirectory -Recurse -Force
|
|
1526
|
+
|
|
1527
|
+
# ------------------------------------------------------------------------ #
|
|
1528
|
+
# DLL link fix
|
|
1529
|
+
# ------------------------------------------------------------------------ #
|
|
1530
|
+
$GlobalizationDll = Join-Path $PortableWingetDirectory "Windows.Globalization.dll"
|
|
1531
|
+
$UserProfileLink = Join-Path $PortableWingetDirectory "Windows.System.UserProfile.dll"
|
|
1532
|
+
if ((Test-Path $GlobalizationDll) -and (-not (Test-Path $UserProfileLink))) {
|
|
1533
|
+
New-Item -ItemType SymbolicLink -Path $UserProfileLink -Target $GlobalizationDll -Force | Out-Null
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
Write-Output "Portable winget extraction completed successfully."
|
|
1537
|
+
Remove-Item -LiteralPath $BaseTemp -Recurse -Force -ErrorAction SilentlyContinue
|
|
1538
|
+
|
|
1539
|
+
# ============================================================================ #
|
|
1540
|
+
# Apply path permissions and add path
|
|
1541
|
+
# ============================================================================ #
|
|
1542
|
+
Apply-PathPermissionsFixAndAddPath -OSVersion $osVersion
|
|
1543
|
+
}
|
|
1544
|
+
|
|
1545
|
+
# ============================================================================ #
|
|
1546
|
+
# Install using the regular method, Windows 10+
|
|
1547
|
+
# ============================================================================ #
|
|
1548
|
+
|
|
1549
|
+
if (
|
|
1550
|
+
$osVersion.NumericVersion -notin 2019, 2022 -and
|
|
1551
|
+
$osVersion.InstallationType -ne 'Server Core' -and
|
|
1552
|
+
-not $AlternateInstallMethod -and
|
|
1553
|
+
-not $RunAsSystem
|
|
1554
|
+
) {
|
|
1555
|
+
Write-Section "winget"
|
|
1556
|
+
|
|
1557
|
+
# If WingetVersion is specified, warn user it only works when using the -AlternateInstallMethod.
|
|
1558
|
+
if ($WingetVersion -ne 'latest') {
|
|
1559
|
+
Write-Warning "The -WingetVersion parameter is ignored when not using -AlternateInstallMethod. The standard install method always installs the latest version."
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
try {
|
|
1563
|
+
Write-Debug "Checking if NuGet PackageProvider is already installed..."
|
|
1564
|
+
Install-NuGetIfRequired
|
|
1565
|
+
|
|
1566
|
+
Write-Output "Installing Microsoft.WinGet.Client module (latest)..."
|
|
1567
|
+
Install-Module -Name Microsoft.WinGet.Client -Force -AllowClobber -Repository PSGallery -ErrorAction SilentlyContinue *>&1 | Out-Null
|
|
1568
|
+
|
|
1569
|
+
Write-Output "Installing winget (this takes a minute or two)..."
|
|
1570
|
+
try { Repair-WinGetPackageManager -AllUsers -Force -Latest *>&1 | Out-Null } catch { }
|
|
1571
|
+
|
|
1572
|
+
} catch {
|
|
1573
|
+
$errorHandled = Handle-Error $_
|
|
1574
|
+
if ($null -ne $errorHandled) {
|
|
1575
|
+
throw $errorHandled
|
|
1576
|
+
}
|
|
1577
|
+
$errorHandled = $null
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
# Add to environment PATH to avoid issues when usernames or user profile paths change, or when using non-Latin characters (see #45)
|
|
1581
|
+
# Adding with literal %LOCALAPPDATA% to ensure it isn't resolved to the current user's LocalAppData as a fixed path
|
|
1582
|
+
Add-ToEnvironmentPath -PathToAdd "%LOCALAPPDATA%\Microsoft\WindowsApps" -Scope 'User'
|
|
1583
|
+
}
|
|
1584
|
+
|
|
1585
|
+
# ============================================================================ #
|
|
1586
|
+
# Server 2019 or alternate install method only
|
|
1587
|
+
# ============================================================================ #
|
|
1588
|
+
|
|
1589
|
+
if (
|
|
1590
|
+
(
|
|
1591
|
+
$osVersion.Type -eq 'Server' -and
|
|
1592
|
+
$osVersion.NumericVersion -in 2019, 2022 -and
|
|
1593
|
+
$osVersion.InstallationType -ne 'Server Core'
|
|
1594
|
+
) -or
|
|
1595
|
+
$AlternateInstallMethod -or
|
|
1596
|
+
$RunAsSystem
|
|
1597
|
+
) {
|
|
1598
|
+
|
|
1599
|
+
# ============================================================================ #
|
|
1600
|
+
# Dependencies
|
|
1601
|
+
# ============================================================================ #
|
|
1602
|
+
|
|
1603
|
+
Write-Section "Dependencies"
|
|
1604
|
+
Write-Debug "GHtoken=$GHtoken"
|
|
1605
|
+
|
|
1606
|
+
try {
|
|
1607
|
+
# Download winget dependencies (VCLibs.140.00.UWPDesktop and UI.Xaml.2.8)
|
|
1608
|
+
$winget_dependencies_path = New-TemporaryFile2
|
|
1609
|
+
try {
|
|
1610
|
+
$winget_dependencies_url = Get-WingetDownloadUrl -Match 'DesktopAppInstaller_Dependencies.zip' -WingetVersion $WingetVersion
|
|
1611
|
+
} catch {
|
|
1612
|
+
# If we can't get the dependencies of the exact version the user wants, fall back to try to find another version that has them
|
|
1613
|
+
$winget_dependencies_url = Get-WingetDownloadUrl -Match 'DesktopAppInstaller_Dependencies.zip' -GHtoken $GHtoken
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
Write-Output 'Downloading winget dependencies...'
|
|
1617
|
+
Write-Debug "Downloading winget dependencies from $winget_dependencies_url to $winget_dependencies_path`n`n"
|
|
1618
|
+
Invoke-WebRequest -Uri $winget_dependencies_url -OutFile $winget_dependencies_path
|
|
1619
|
+
|
|
1620
|
+
# Load ZIP assembly to read the package contents
|
|
1621
|
+
Add-Type -AssemblyName System.IO.Compression.FileSystem
|
|
1622
|
+
$zip = [System.IO.Compression.ZipFile]::OpenRead($winget_dependencies_path)
|
|
1623
|
+
|
|
1624
|
+
$matchingEntries = $zip.Entries | Where-Object { $_.FullName -match ".*$arch.appx" }
|
|
1625
|
+
if ($matchingEntries) {
|
|
1626
|
+
$matchingEntries | ForEach-Object {
|
|
1627
|
+
$destPath = Join-Path ([System.IO.Path]::GetTempPath()) $_.Name
|
|
1628
|
+
Write-Debug "Extracting $($_.FullName) to $destPath..."
|
|
1629
|
+
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, $destPath, $true)
|
|
1630
|
+
|
|
1631
|
+
$AppName = Get-ManifestName $destPath
|
|
1632
|
+
Write-Output "Installing $AppName..."
|
|
1633
|
+
Install-LibIfRequired -Lib_Name $AppName -Lib_Path $destPath
|
|
1634
|
+
Write-Debug "Removing temporary library file..."
|
|
1635
|
+
TryRemove $destPath
|
|
1636
|
+
}
|
|
1637
|
+
$zip.Dispose()
|
|
1638
|
+
} else {
|
|
1639
|
+
Write-Error "Dependency not found inside the file: $winget_dependencies_path"
|
|
1640
|
+
}
|
|
1641
|
+
|
|
1642
|
+
Write-Debug "Removing temporary files..."
|
|
1643
|
+
TryRemove $winget_dependencies_path
|
|
1644
|
+
|
|
1645
|
+
} catch {
|
|
1646
|
+
$errorHandled = Handle-Error $_
|
|
1647
|
+
if ($null -ne $errorHandled) {
|
|
1648
|
+
throw $errorHandled
|
|
1649
|
+
}
|
|
1650
|
+
$errorHandled = $null
|
|
1651
|
+
}
|
|
1652
|
+
|
|
1653
|
+
# ============================================================================ #
|
|
1654
|
+
# winget
|
|
1655
|
+
# ============================================================================ #
|
|
1656
|
+
|
|
1657
|
+
Write-Section "winget"
|
|
1658
|
+
|
|
1659
|
+
try {
|
|
1660
|
+
|
|
1661
|
+
# Download winget license
|
|
1662
|
+
$winget_license_path = New-TemporaryFile2
|
|
1663
|
+
try {
|
|
1664
|
+
$winget_license_url = Get-WingetDownloadUrl -Match "License1.xml" -WingetVersion $WingetVersion
|
|
1665
|
+
} catch {
|
|
1666
|
+
# If we can't get the license XML of the exact version the user wants, fall back to try to find another version that has it
|
|
1667
|
+
$winget_license_url = Get-WingetDownloadUrl -Match "License1.xml" -GHtoken $GHtoken
|
|
1668
|
+
}
|
|
1669
|
+
Write-Output "Downloading winget license..."
|
|
1670
|
+
Write-Debug "Downloading winget license from $winget_license_url to $winget_license_path`n`n"
|
|
1671
|
+
Invoke-WebRequest -Uri $winget_license_url -OutFile $winget_license_path
|
|
1672
|
+
|
|
1673
|
+
# Download winget
|
|
1674
|
+
$winget_path = New-TemporaryFile2
|
|
1675
|
+
$winget_url = Get-WingetDownloadUrl -Match 'Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle' -WingetVersion $WingetVersion -GHtoken $GHtoken
|
|
1676
|
+
Write-Output "Downloading winget..."
|
|
1677
|
+
Write-Debug "Downloading winget from $winget_url to $winget_path`n`n"
|
|
1678
|
+
Invoke-WebRequest -Uri $winget_url -OutFile $winget_path
|
|
1679
|
+
|
|
1680
|
+
# Install winget
|
|
1681
|
+
Write-Output "Installing winget..."
|
|
1682
|
+
Add-AppxProvisionedPackage -Online -PackagePath $winget_path -LicensePath $winget_license_path | Out-Null
|
|
1683
|
+
|
|
1684
|
+
# Remove temporary files
|
|
1685
|
+
Write-Debug "Removing temporary files..."
|
|
1686
|
+
TryRemove $winget_path
|
|
1687
|
+
TryRemove $winget_license_path
|
|
1688
|
+
} catch {
|
|
1689
|
+
$errorHandled = Handle-Error $_
|
|
1690
|
+
if ($null -ne $errorHandled) {
|
|
1691
|
+
throw $errorHandled
|
|
1692
|
+
}
|
|
1693
|
+
$errorHandled = $null
|
|
1694
|
+
}
|
|
1695
|
+
|
|
1696
|
+
# ============================================================================ #
|
|
1697
|
+
# Visual C++ Redistributable
|
|
1698
|
+
# ============================================================================ #
|
|
1699
|
+
|
|
1700
|
+
Write-Section "Visual C++ Redistributable"
|
|
1701
|
+
|
|
1702
|
+
# Test if Visual C++ Redistributable is not installed
|
|
1703
|
+
if (!(Test-VCRedistInstalled)) {
|
|
1704
|
+
Write-Debug "Visual C++ Redistributable is not installed.`n`n"
|
|
1705
|
+
|
|
1706
|
+
# Define the URL and temporary file path for the download
|
|
1707
|
+
$VCppRedistributable_Url = "https://aka.ms/vs/17/release/vc_redist.${arch}.exe"
|
|
1708
|
+
$VCppRedistributable_Path = New-TemporaryFile2
|
|
1709
|
+
Write-Output "Downloading Visual C++ Redistributable..."
|
|
1710
|
+
Write-Debug "Downloading Visual C++ Redistributable from $VCppRedistributable_Url to $VCppRedistributable_Path`n`n"
|
|
1711
|
+
Invoke-WebRequest -Uri $VCppRedistributable_Url -OutFile $VCppRedistributable_Path
|
|
1712
|
+
|
|
1713
|
+
# Rename file
|
|
1714
|
+
$VCppRedistributableExe_Path = $VCppRedistributable_Path + ".exe"
|
|
1715
|
+
Rename-Item -Path $VCppRedistributable_Path -NewName $VCppRedistributableExe_Path
|
|
1716
|
+
|
|
1717
|
+
# Install Visual C++ Redistributable
|
|
1718
|
+
Write-Output "Installing Visual C++ Redistributable..."
|
|
1719
|
+
Write-Debug "Installing Visual C++ Redistributable from $VCppRedistributableExe_Path`n`n"
|
|
1720
|
+
Start-Process -FilePath $VCppRedistributableExe_Path -ArgumentList "/install", "/quiet", "/norestart" -Wait
|
|
1721
|
+
|
|
1722
|
+
Write-Debug "Removing temporary file..."
|
|
1723
|
+
TryRemove $VCppRedistributableExe_Path
|
|
1724
|
+
} else {
|
|
1725
|
+
Write-Output "Visual C++ Redistributable is already installed."
|
|
1726
|
+
}
|
|
1727
|
+
|
|
1728
|
+
# ============================================================================ #
|
|
1729
|
+
# Fix environment PATH and permissions
|
|
1730
|
+
# ============================================================================ #
|
|
1731
|
+
Apply-PathPermissionsFixAndAddPath -OSVersion $osVersion
|
|
1732
|
+
}
|
|
1733
|
+
|
|
1734
|
+
# ============================================================================ #
|
|
1735
|
+
# Force registration
|
|
1736
|
+
# ============================================================================ #
|
|
1737
|
+
|
|
1738
|
+
$WingetPackageFamilyName = 'Microsoft.DesktopAppInstaller_8wekyb3d8bbwe'
|
|
1739
|
+
|
|
1740
|
+
if (
|
|
1741
|
+
$osVersion.NumericVersion -notin 2019, 2022 -and
|
|
1742
|
+
$osVersion.InstallationType -ne 'Server Core' -and
|
|
1743
|
+
-not $RunAsSystem -and
|
|
1744
|
+
-not $AlternateInstallMethod
|
|
1745
|
+
) {
|
|
1746
|
+
$WingetPackage = Get-AppxPackage -AllUsers -Name $WingetPackageFamilyName -ErrorAction SilentlyContinue
|
|
1747
|
+
|
|
1748
|
+
if ($null -ne $WingetPackage) {
|
|
1749
|
+
Write-Debug 'Registering winget...'
|
|
1750
|
+
try {
|
|
1751
|
+
Add-AppxPackage -RegisterByFamilyName -MainPackage $WingetPackageFamilyName
|
|
1752
|
+
} catch { }
|
|
1753
|
+
} else {
|
|
1754
|
+
Write-Debug 'Winget package not found. Skipping registration.'
|
|
1755
|
+
}
|
|
1756
|
+
} else {
|
|
1757
|
+
Write-Debug 'Not registering winget...'
|
|
1758
|
+
}
|
|
1759
|
+
|
|
1760
|
+
# ============================================================================ #
|
|
1761
|
+
# Done
|
|
1762
|
+
# ============================================================================ #
|
|
1763
|
+
|
|
1764
|
+
Write-Section "Complete"
|
|
1765
|
+
|
|
1766
|
+
Write-Output "winget installed successfully."
|
|
1767
|
+
|
|
1768
|
+
# ============================================================================ #
|
|
1769
|
+
# Finished
|
|
1770
|
+
# ============================================================================ #
|
|
1771
|
+
|
|
1772
|
+
# Timeout before checking winget
|
|
1773
|
+
Write-Output "Checking if winget is installed and working..."
|
|
1774
|
+
Start-Sleep -Seconds 3
|
|
1775
|
+
|
|
1776
|
+
# Check if winget is installed
|
|
1777
|
+
$WingetStatus = Get-WingetStatus
|
|
1778
|
+
|
|
1779
|
+
if ($WingetStatus) {
|
|
1780
|
+
Write-Output 'winget is installed and working. You can go ahead and use it.'
|
|
1781
|
+
if ($RunAsSystem) {
|
|
1782
|
+
Write-Output 'Since this script is running under the SYSTEM context, you may need to restart the computer or session for the winget command to function as expected.'
|
|
1783
|
+
}
|
|
1784
|
+
} else {
|
|
1785
|
+
Write-Debug "Get-WingetStatus: $WingetStatus"
|
|
1786
|
+
Write-Warning 'winget is installed but is not detected as a command. Try using winget now. If it does not work, wait about 1 minute and try again (it is sometimes delayed). Also try restarting your computer.'
|
|
1787
|
+
Write-Warning "If you restart your computer and the command still is not recognized, please read the Troubleshooting section`nof the README: https://github.com/asheroto/winget-install#troubleshooting`n"
|
|
1788
|
+
Write-Warning "Make sure you have the latest version of the script by running this command: $PowerShellGalleryName -CheckForUpdate`n`n"
|
|
1789
|
+
}
|
|
1790
|
+
|
|
1791
|
+
ExitWithDelay 0
|
|
1792
|
+
} catch {
|
|
1793
|
+
# ============================================================================ #
|
|
1794
|
+
# Error handling
|
|
1795
|
+
# ============================================================================ #
|
|
1796
|
+
|
|
1797
|
+
Write-Section "WARNING! An error occurred during installation!"
|
|
1798
|
+
Write-Warning "If messages above don't help and the problem persists, please read the Troubleshooting section`nof the README: https://github.com/asheroto/winget-install#troubleshooting"
|
|
1799
|
+
Write-Warning "Make sure you have the latest version of the script by running this command: $PowerShellGalleryName -CheckForUpdate`n`n"
|
|
1800
|
+
|
|
1801
|
+
# If it's not 0x80073D02 (resources in use), show error
|
|
1802
|
+
if ($_.Exception.Message -notmatch '0x80073D02') {
|
|
1803
|
+
if ($Debug) {
|
|
1804
|
+
Write-Warning "Line number : $($_.InvocationInfo.ScriptLineNumber)"
|
|
1805
|
+
}
|
|
1806
|
+
Write-Warning "Error: $($_.Exception.Message)`n"
|
|
1807
|
+
}
|
|
1808
|
+
|
|
1809
|
+
ExitWithDelay 1
|
|
1810
|
+
}
|