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

This commit is contained in:
Mr.k
2024-04-26 05:03:01 +03:00
parent bdf9c8378c
commit fab91a6e3e

View File

@ -20,13 +20,16 @@ function Test-WinUtilPackageManager {
$status = "not-installed" $status = "not-installed"
if ($winget) { if ($winget) {
# Install Winget if not detected # Get Winget Version and Install it if not detected
$wingetExists = Get-Command -Name winget -ErrorAction SilentlyContinue $wingetVersionFull = ""
$wingetExists = $true
try {
$wingetVersionFull = winget --version
} catch [System.Management.Automation.CommandNotFoundException], [System.Management.Automation.ApplicationFailedException] {
$wingetExists = $false
}
if ($wingetExists) { if ($wingetExists) {
# Check Winget Version
$wingetVersionFull = (winget --version) # Full Version without 'v'.
# Check if Preview Version # Check if Preview Version
if ($wingetVersionFull.Contains("-preview")) { if ($wingetVersionFull.Contains("-preview")) {
$wingetVersion = $wingetVersionFull.Trim("-preview") $wingetVersion = $wingetVersionFull.Trim("-preview")