1919 run : |
2020 $version = '${{ inputs.version }}'
2121
22- # Try to remove existing PowerShell installation
23- try {
24- # Find existing PowerShell installations to remove
25- Get-Package -Name "PowerShell*" -ErrorAction SilentlyContinue | ForEach-Object {
26- Write-Host "Uninstalling $($_.Name) version $($_.Version)"
27- Uninstall-Package -Name $_.Name -Force -ErrorAction SilentlyContinue
28- }
29- } catch {
30- Write-Host "Error removing existing PowerShell: $_"
31- }
32-
3322 # Get the version to install
3423 if ([string]::IsNullOrWhiteSpace($version)) {
3524 # Get latest version from GitHub API
@@ -40,48 +29,108 @@ runs:
4029 Write-Host "Installing specified PowerShell version: $version"
4130 }
4231
43- # Download and install PowerShell
44- $msiName = "PowerShell-$version-win-x64.msi"
45- $downloadUrl = "https://github.com/PowerShell/PowerShell/releases/download/v$version/$msiName"
32+ # Check if PowerShell is already installed and compare versions
33+ $needToInstall = $true
34+ try {
35+ $currentInstall = Get-Package -Name "PowerShell*" -ErrorAction SilentlyContinue | Select-Object -First 1
36+ if ($currentInstall) {
37+ $currentVersion = $currentInstall.Version
38+ Write-Host "Current PowerShell version: $currentVersion"
39+
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
50+ }
51+ } else {
52+ Write-Host "PowerShell Core not found. Will install version $version"
53+ }
54+ } catch {
55+ Write-Host "Error checking current PowerShell version: $_"
56+ }
4657
47- Write-Host "Downloading from $downloadUrl"
48- Invoke-WebRequest -Uri $downloadUrl -OutFile $msiName
58+ if ($needToInstall) {
59+ # Download and install PowerShell
60+ $msiName = "PowerShell-$version-win-x64.msi"
61+ $downloadUrl = "https://github.com/PowerShell/PowerShell/releases/download/v$version/$msiName"
4962
50- Write-Host "Installing PowerShell $version "
51- Start-Process -FilePath "msiexec.exe" -ArgumentList "/i", $msiName, "/quiet", "/norestart" -Wait
63+ Write-Host "Downloading from $downloadUrl "
64+ Invoke-WebRequest -Uri $downloadUrl -OutFile $msiName
5265
53- # Verify installation
54- $pwshPath = "$env:ProgramFiles\PowerShell\7\pwsh.exe"
55- if (Test-Path $pwshPath) {
56- Write-Host "PowerShell installed successfully:"
57- & $pwshPath -Command '$PSVersionTable'
58- } else {
59- Write-Error "PowerShell installation failed. Could not find $pwshPath"
60- exit 1
66+ Write-Host "Installing PowerShell $version"
67+ Start-Process -FilePath "msiexec.exe" -ArgumentList "/i", $msiName, "/quiet", "/norestart" -Wait
68+
69+ # Verify installation
70+ $pwshPath = "$env:ProgramFiles\PowerShell\7\pwsh.exe"
71+ if (Test-Path $pwshPath) {
72+ Write-Host "PowerShell installed successfully:"
73+ & $pwshPath -Command '$PSVersionTable'
74+ } else {
75+ Write-Error "PowerShell installation failed. Could not find $pwshPath"
76+ exit 1
77+ }
6178 }
6279
6380 - name : Install PowerShell on Ubuntu
6481 if : runner.os == 'Linux'
6582 shell : bash
6683 run : |
67- sudo apt-get remove powershell -y
6884 version='${{ inputs.version }}'
6985 if [ -z "$version" ]; then
86+ # Get latest version if not specified
7087 version=$(curl -s https://api.github.com/repos/PowerShell/PowerShell/releases/latest | grep 'tag_name' | cut -d '"' -f 4)
88+ echo "Latest PowerShell version: $version"
7189 else
7290 version="v$version"
91+ echo "Installing specified PowerShell version: $version"
92+ fi
93+
94+ # Strip the 'v' prefix for version comparison
95+ clean_version=${version#v}
96+
97+ # Check if PowerShell is already installed and get current version
98+ need_to_install=true
99+ if command -v pwsh &> /dev/null; then
100+ current_version=$(pwsh -c '$PSVersionTable.PSVersion.ToString()' | tr -d '\r\n')
101+ echo "Current PowerShell version: $current_version"
102+
103+ # Compare versions
104+ if [ "$(printf '%s\n' "$clean_version" "$current_version" | sort -V | head -n1)" != "$clean_version" ]; then
105+ echo "Current version $current_version is greater than or equal to target version $clean_version. Skipping installation."
106+ need_to_install=false
107+ else
108+ echo "Current version $current_version is lower than target version $clean_version. Will upgrade."
109+ sudo apt-get remove powershell -y
110+ fi
111+ else
112+ echo "PowerShell is not installed. Will install version $clean_version"
113+ fi
114+
115+ if [ "$need_to_install" = true ]; then
116+ wget https://github.com/PowerShell/PowerShell/releases/download/$version/powershell_${clean_version}-1.deb_amd64.deb
117+ sudo dpkg -i powershell_${clean_version}-1.deb_amd64.deb
118+ sudo apt-get install -f
119+
120+ # Verify installation
121+ if command -v pwsh &> /dev/null; then
122+ echo "PowerShell installed successfully:"
123+ pwsh -c '$PSVersionTable'
124+ else
125+ echo "ERROR: PowerShell installation failed."
126+ exit 1
127+ fi
73128 fi
74- wget https://github.com/PowerShell/PowerShell/releases/download/$version/powershell_${version#v}-1.deb_amd64.deb
75- sudo dpkg -i powershell_${version#v}-1.deb_amd64.deb
76- sudo apt-get install -f
77129
78130 - name : Install PowerShell on macOS
79131 if : runner.os == 'macOS'
80132 shell : bash
81133 run : |
82- # Clean up existing PowerShell installation
83- sudo rm -rf /usr/local/microsoft/powershell
84-
85134 # Get version information
86135 version='${{ inputs.version }}'
87136 if [ -z "$version" ]; then
@@ -96,58 +145,79 @@ runs:
96145 # The version without 'v' prefix
97146 clean_version=${version#v}
98147
99- # Try the different naming patterns for the macOS package
100- pkg_patterns=(
101- "powershell-$clean_version-osx-x64.pkg"
102- "powershell-$clean_version-osx-arm64.pkg"
103- "powershell-$clean_version-osx.pkg"
104- )
105-
106- # Determine if running on Apple Silicon or Intel
107- if [ "$(uname -m)" = "arm64" ]; then
108- echo "Detected Apple Silicon (ARM64)"
109- arch="arm64"
148+ # Check if PowerShell is already installed and get current version
149+ need_to_install=true
150+ if [ -f "/usr/local/bin/pwsh" ]; then
151+ current_version=$(/usr/local/bin/pwsh -c '$PSVersionTable.PSVersion.ToString()' | tr -d '\r\n')
152+ echo "Current PowerShell version: $current_version"
153+
154+ # Compare versions
155+ if [ "$(printf '%s\n' "$clean_version" "$current_version" | sort -V | head -n1)" != "$clean_version" ]; then
156+ echo "Current version $current_version is greater than or equal to target version $clean_version. Skipping installation."
157+ need_to_install=false
158+ else
159+ echo "Current version $current_version is lower than target version $clean_version. Will upgrade."
160+ # Clean up existing PowerShell installation
161+ sudo rm -rf /usr/local/microsoft/powershell
162+ fi
110163 else
111- echo "Detected Intel CPU (x64)"
112- arch="x64"
164+ echo "PowerShell is not installed. Will install version $clean_version"
113165 fi
114166
115- # Try to download the appropriate package based on architecture
116- download_success=false
117-
118- for pkg_name in "${pkg_patterns[@]}"; do
119- echo "Trying to download $pkg_name... "
120- url="https://github.com/PowerShell/PowerShell/releases/download/$version/$pkg_name "
121-
122- # Try to download the file
123- if curl -LO $url --fail; then
124- echo "Successfully downloaded $pkg_name"
125- download_success=true
126- break
167+ if [ "$need_to_install" = true ]; then
168+ # Try the different naming patterns for the macOS package
169+ pkg_patterns=(
170+ "powershell-$clean_version-osx-x64.pkg"
171+ "powershell-$clean_version-osx-arm64.pkg "
172+ "powershell-$clean_version-osx.pkg "
173+ )
174+
175+ # Determine if running on Apple Silicon or Intel
176+ if [ "$(uname -m)" = "arm64" ]; then
177+ echo "Detected Apple Silicon (ARM64)"
178+ arch="arm64"
127179 else
128- echo "Failed to download $pkg_name"
180+ echo "Detected Intel CPU (x64)"
181+ arch="x64"
129182 fi
130- done
131-
132- if [ "$download_success" = false ]; then
133- echo "ERROR: Could not download PowerShell package for macOS. Please check version and available packages."
134- exit 1
135- fi
136183
137- # Verify the file exists before installation
138- if [ -f "$pkg_name" ]; then
139- echo "Installing PowerShell package: $pkg_name"
140- sudo installer -pkg "$pkg_name" -target /
184+ # Try to download the appropriate package based on architecture
185+ download_success=false
186+
187+ for pkg_name in "${pkg_patterns[@]}"; do
188+ echo "Trying to download $pkg_name..."
189+ url="https://github.com/PowerShell/PowerShell/releases/download/$version/$pkg_name"
190+
191+ # Try to download the file
192+ if curl -LO $url --fail; then
193+ echo "Successfully downloaded $pkg_name"
194+ download_success=true
195+ break
196+ else
197+ echo "Failed to download $pkg_name"
198+ fi
199+ done
200+
201+ if [ "$download_success" = false ]; then
202+ echo "ERROR: Could not download PowerShell package for macOS. Please check version and available packages."
203+ exit 1
204+ fi
141205
142- # Verify the installation
143- if [ -f "/usr/local/bin/pwsh" ]; then
144- echo "PowerShell installed successfully:"
145- /usr/local/bin/pwsh -Command '$PSVersionTable'
206+ # Verify the file exists before installation
207+ if [ -f "$pkg_name" ]; then
208+ echo "Installing PowerShell package: $pkg_name"
209+ sudo installer -pkg "$pkg_name" -target /
210+
211+ # Verify the installation
212+ if [ -f "/usr/local/bin/pwsh" ]; then
213+ echo "PowerShell installed successfully:"
214+ /usr/local/bin/pwsh -Command '$PSVersionTable'
215+ else
216+ echo "ERROR: PowerShell installation failed. Could not find /usr/local/bin/pwsh"
217+ exit 1
218+ fi
146219 else
147- echo "ERROR: PowerShell installation failed. Could not find /usr/local/bin/pwsh "
220+ echo "ERROR: Downloaded package file not found: $pkg_name "
148221 exit 1
149222 fi
150- else
151- echo "ERROR: Downloaded package file not found: $pkg_name"
152- exit 1
153223 fi
0 commit comments