Skip to content

Commit df3c67e

Browse files
Enhance version comparison logic in PowerShell installation: parse versions into System.Version objects for accurate comparison and improve error handling during version checks.
1 parent ad96cf2 commit df3c67e

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

action.yml

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,24 @@ runs:
3737
$currentVersion = $currentInstall.Version
3838
Write-Host "Current PowerShell version: $currentVersion"
3939
40-
# Compare versions
41-
if ([System.Version]$currentVersion -ge [System.Version]$version) {
42-
Write-Host "Current version is greater than or equal to target version. Skipping installation."
43-
$needToInstall = $false
44-
} else {
45-
Write-Host "Current version is lower than target version. Will upgrade."
46-
47-
# Uninstall current version
48-
Write-Host "Uninstalling $($currentInstall.Name) version $($currentInstall.Version)"
49-
Uninstall-Package -Name $currentInstall.Name -Force -ErrorAction SilentlyContinue
40+
# Parse versions into System.Version objects for proper comparison
41+
try {
42+
$currentVersionObj = [System.Version]$currentVersion
43+
$targetVersionObj = [System.Version]$version
44+
45+
# Compare versions
46+
if ($currentVersionObj -ge $targetVersionObj) {
47+
Write-Host "Current version $currentVersion is greater than or equal to target version $version. Skipping installation."
48+
$needToInstall = $false
49+
} else {
50+
Write-Host "Current version $currentVersion is lower than target version $version. Will upgrade."
51+
52+
# Uninstall current version
53+
Write-Host "Uninstalling $($currentInstall.Name) version $($currentInstall.Version)"
54+
Uninstall-Package -Name $currentInstall.Name -Force -ErrorAction SilentlyContinue
55+
}
56+
} catch {
57+
Write-Host "Error comparing versions: $_. Will proceed with installation."
5058
}
5159
} else {
5260
Write-Host "PowerShell Core not found. Will install version $version"

0 commit comments

Comments
 (0)