Fix Winget Detection by doing Proper Error Handling using a Try-Catch

* Fix Winget Detection by doing Proper Error Handling using a Try-Catch

* Remove unnecessary Variable Initialization of 'wingetFullVersion'

Co-authored-by: Martin Wiethan <47688561+Marterich@users.noreply.github.com>

* Add Comments to be make The Logic a Lot More Clear to Follow and Understand

* Add Another Catch Statement with some 'Write-Warning' for Extra Information
This commit is contained in:
Mr.k 2024-05-02 00:34:52 +03:00 committed by GitHub
parent ad6dcca76e
commit 60db65b5a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -20,13 +20,22 @@ function Test-WinUtilPackageManager {
$status = "not-installed" $status = "not-installed"
if ($winget) { if ($winget) {
# Install Winget if not detected # Check if Winget is available while getting it's Version if it's available
$wingetExists = Get-Command -Name winget -ErrorAction SilentlyContinue $wingetExists = $true
try {
if ($wingetExists) { $wingetVersionFull = winget --version
# Check Winget Version } catch [System.Management.Automation.CommandNotFoundException], [System.Management.Automation.ApplicationFailedException] {
$wingetVersionFull = (winget --version) # Full Version without 'v'. Write-Warning "Winget was not found due to un-availablity reasons"
$wingetExists = $false
} catch {
Write-Warning "Winget was not found due to un-known reasons, The Stack Trace is:`n$($psitem.Exception.StackTrace)"
$wingetExists = $false
}
# If Winget is available, Parse it's Version and give proper information to Terminal Output.
# If it isn't available, the return of this funtion will be "not-installed", indicating that
# Winget isn't installed/available on The System.
if ($wingetExists) {
# Check if Preview Version # Check if Preview Version
if ($wingetVersionFull.Contains("-preview")) { if ($wingetVersionFull.Contains("-preview")) {
$wingetVersion = $wingetVersionFull.Trim("-preview") $wingetVersion = $wingetVersionFull.Trim("-preview")