@@ -22,10 +22,12 @@ runs:
2222 - name : Install PowerShell (Linux)
2323 if : runner.os == 'Linux'
2424 shell : bash
25+ working-directory : ${{ github.action_path }}
2526 env :
2627 REQUESTED_VERSION : ${{ inputs.Version }}
2728 GITHUB_TOKEN : ${{ github.token }}
2829 run : |
30+ # Install-PowerShell
2931 set -e
3032
3133 echo "Requested version: [$REQUESTED_VERSION]"
@@ -50,121 +52,127 @@ runs:
5052 esac
5153
5254 DETECTED_VERSION=$(pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()' 2>/dev/null || true)
55+ if [[ -n "$DETECTED_VERSION" ]]; then
56+ echo "Currently installed PowerShell version: $DETECTED_VERSION"
57+ else
58+ echo "PowerShell is not currently installed"
59+ fi
60+
5361 if [[ "$DETECTED_VERSION" == "$REQUESTED_VERSION" ]]; then
5462 echo "PowerShell $DETECTED_VERSION already installed. Skipping."
5563 exit 0
5664 fi
5765
58- if command -v apt-get >/dev/null; then
59- echo "Using APT package manager (Debian/Ubuntu)..."
66+ # Determine Linux distribution type
67+ ARCH=$(dpkg --print-architecture 2>/dev/null || rpm --eval '%{_arch}' 2>/dev/null || echo "x86_64")
6068
61- if ! grep -q "packages.microsoft.com" /etc/apt/sources.list /etc/apt/sources.list.d/* 2>/dev/null; then
62- wget -qO- https://packages.microsoft.com/keys/microsoft.asc \
63- | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc > /dev/null
64- DIST_CODENAME=$(lsb_release -cs)
65- sudo add-apt-repository \
66- "deb [arch=$(dpkg --print-architecture)] https://packages.microsoft.com/ubuntu/$DIST_CODENAME/prod $DIST_CODENAME main"
67- fi
68-
69- sudo apt-get update -y
70- EXACT_PKG=$(apt-cache madison powershell | awk '{print $3}' \
71- | grep -E "^${REQUESTED_VERSION}(-|$)" | head -n1 || true)
72-
73- if [[ -n "$EXACT_PKG" ]]; then
74- sudo apt-get install -y powershell=$EXACT_PKG
69+ if command -v apt-get >/dev/null || command -v dpkg >/dev/null; then
70+ # Debian/Ubuntu based
71+ echo "Detected Debian/Ubuntu based system..."
72+ DEB_NAME="powershell_${REQUESTED_VERSION}-1.deb_${ARCH}.deb"
73+ URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${DEB_NAME}"
74+ echo "Downloading from: $URL"
75+ wget -q "$URL" -O "$DEB_NAME"
76+ echo "Starting installation of PowerShell [$REQUESTED_VERSION)]..."
77+ sudo dpkg -i "$DEB_NAME" || sudo apt-get -f install -y
78+ echo "Installation complete. PowerShell [$REQUESTED_VERSION] is now available."
79+ elif command -v rpm >/dev/null; then
80+ # RHEL/Fedora/CentOS based
81+ echo "Detected RHEL/Fedora/CentOS based system..."
82+
83+ if [[ "$ARCH" == "aarch64" ]]; then
84+ RPM_NAME="powershell-${REQUESTED_VERSION}-1.rh.${ARCH}.rpm"
7585 else
76- ARCH=$(dpkg --print-architecture)
77- DEB_NAME="powershell_${REQUESTED_VERSION}-1.deb_${ARCH}.deb"
78- URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${DEB_NAME}"
79- wget -q "$URL" -O "$DEB_NAME"
80- sudo dpkg -i "$DEB_NAME" || sudo apt-get -f install -y
86+ RPM_NAME="powershell-${REQUESTED_VERSION}-1.rh.x86_64.rpm"
8187 fi
88+
89+ URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${RPM_NAME}"
90+ echo "Downloading from: $URL"
91+ wget -q "$URL" -O "$RPM_NAME"
92+ echo "Starting installation of PowerShell [$REQUESTED_VERSION)]..."
93+ sudo rpm -i "$RPM_NAME" || sudo yum install -y "$RPM_NAME"
94+ echo "Installation complete. PowerShell [$REQUESTED_VERSION] is now available."
8295 else
83- echo "Unsupported Linux distribution (no apt-get) ."
96+ echo "Unsupported Linux distribution. Cannot determine package format ."
8497 exit 1
8598 fi
99+ echo "PowerShell [$REQUESTED_VERSION] installed successfully."
86100
87101 - name : Install PowerShell (macOS)
88102 if : runner.os == 'macOS'
89103 shell : bash
104+ working-directory : ${{ github.action_path }}
90105 env :
91106 REQUESTED_VERSION : ${{ inputs.Version }}
92107 GITHUB_TOKEN : ${{ github.token }}
93108 run : |
109+ # Install-PowerShell
94110 set -e
95111
96112 echo "Requested version: [$REQUESTED_VERSION]"
97113
98- # Convert to lowercase for comparison (macOS-compatible method)
99- REQ_VER_LOWER=$(echo "$REQUESTED_VERSION" | tr '[:upper:]' '[:lower:]')
100-
101- # Only resolve to latest version if explicitly set to 'latest'
102- if [[ "$REQ_VER_LOWER" == "latest" ]]; then
103- REQUESTED_VERSION=$(
104- curl -s -f \
105- -H "Accept: application/vnd.github+json" \
106- -H "Authorization: Bearer $GITHUB_TOKEN" \
107- -H "X-GitHub-Api-Version: 2022-11-28" \
108- https://api.github.com/repos/PowerShell/PowerShell/releases/latest |
109- jq -r '.tag_name' | sed 's/^v//'
110- )
111- echo "Latest stable PowerShell release detected: $REQUESTED_VERSION"
112- fi
114+ # Only resolve to latest version if explicitly set to 'latest' (case-insensitive)
115+ case "${REQUESTED_VERSION:-}" in
116+ [Ll][Aa][Tt][Ee][Ss][Tt])
117+ REQUESTED_VERSION=$(
118+ curl -s -f \
119+ -H "Accept: application/vnd.github+json" \
120+ -H "Authorization: Bearer $GITHUB_TOKEN" \
121+ -H "X-GitHub-Api-Version: 2022-11-28" \
122+ https://api.github.com/repos/PowerShell/PowerShell/releases/latest |
123+ jq -r '.tag_name' | sed 's/^v//'
124+ )
125+ echo "Latest stable PowerShell release detected: $REQUESTED_VERSION"
126+ ;;
127+ "")
128+ echo "Error: Version input is required (or use 'latest')"
129+ exit 1
130+ ;;
131+ esac
113132
114- # Validate REQUESTED_VERSION is not empty
115- if [[ -z "${REQUESTED_VERSION}" ]]; then
116- echo "Error: Could not determine a valid PowerShell version."
117- exit 1
133+ DETECTED_VERSION=$(pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()' 2>/dev/null || true)
134+ if [[ -n "$DETECTED_VERSION" ]]; then
135+ echo "Currently installed PowerShell version: $DETECTED_VERSION"
136+ else
137+ echo "PowerShell is not currently installed"
118138 fi
119139
120- DETECTED_VERSION=$(pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()' 2>/dev/null || true)
121140 if [[ "$DETECTED_VERSION" == "$REQUESTED_VERSION" ]]; then
122141 echo "PowerShell $DETECTED_VERSION already installed. Skipping."
123142 exit 0
124143 fi
125144
126- # Try Homebrew first (simplified approach)
127- if command -v brew >/dev/null; then
128- echo "Using Homebrew package manager..."
129- # Homebrew handles 'latest' automatically when no version is specified
130- if [[ "$REQ_VER_LOWER" == "latest" ]]; then
131- brew install --cask powershell
132- else
133- # Try specific version formula first
134- if brew install --cask "powershell@$REQUESTED_VERSION" 2>/dev/null; then
135- echo "Successfully installed via Homebrew"
136- else
137- # Fall back to generic formula if versioned one doesn't exist
138- echo "Version-specific formula not found, installing latest via Homebrew"
139- brew install --cask powershell
140- fi
141- fi
142- else
143- # Fall back to direct download
144- echo "Homebrew not available, downloading directly..."
145- ARCH=$(uname -m)
146- case "$ARCH" in
147- "arm64") PKG_NAME="powershell-${REQUESTED_VERSION}-osx-arm64.pkg" ;;
148- *) PKG_NAME="powershell-${REQUESTED_VERSION}-osx-x64.pkg" ;;
149- esac
150-
151- URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${PKG_NAME}"
152- echo "Downloading from: $URL"
153- if ! curl -sSL "$URL" -o "$PKG_NAME"; then
154- echo "Error: Failed to download PowerShell package"
155- exit 1
156- fi
157- sudo installer -pkg "$PKG_NAME" -target /
145+ # Determine architecture and download appropriate package
146+ ARCH=$(uname -m)
147+ case "$ARCH" in
148+ "arm64") PKG_NAME="powershell-${REQUESTED_VERSION}-osx-arm64.pkg" ;;
149+ *) PKG_NAME="powershell-${REQUESTED_VERSION}-osx-x64.pkg" ;;
150+ esac
151+
152+ URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${PKG_NAME}"
153+ echo "Downloading from: $URL"
154+
155+ echo "Starting installation of PowerShell [$REQUESTED_VERSION]..."
156+
157+ if ! curl -sSL "$URL" -o "$PKG_NAME"; then
158+ echo "Error: Failed to download PowerShell package"
159+ exit 1
158160 fi
161+ sudo installer -pkg "$PKG_NAME" -target /
162+
163+ echo "Installation complete. PowerShell [$REQUESTED_VERSION] is now available."
159164
160165 - name : Install PowerShell (Windows)
161166 if : runner.os == 'Windows'
162167 shell : powershell
168+ working-directory : ${{ github.action_path }}
163169 env :
164170 REQUESTED_VERSION : ${{ inputs.Version }}
165171 GITHUB_TOKEN : ${{ github.token }}
166172 run : |
167- Write-Host "Requested version: [$REQUESTED_VERSION]"
173+ # Install-PowerShell
174+ Write-Host "Requested version: [$env:REQUESTED_VERSION]"
175+
168176 # Only resolve to latest version if explicitly set to 'latest' (case-insensitive)
169177 $req = $env:REQUESTED_VERSION
170178 if ($req -and $req.Trim().ToLower() -eq 'latest') {
@@ -185,19 +193,27 @@ runs:
185193
186194 try {
187195 $detected = (pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()')
196+ Write-Host "Currently installed PowerShell version: $detected"
188197 } catch {
198+ Write-Host "PowerShell is not currently installed"
189199 $detected = $null
190200 }
191201
192- Write-Host "Detected PowerShell version: $detected"
193- Write-Host "Requested PowerShell version: $env:REQUESTED_VERSION"
194-
195202 if ($detected -eq $env:REQUESTED_VERSION) {
196203 Write-Host "PowerShell $detected already installed. Skipping."
197204 exit 0
198205 }
199206
200207 $msi = "PowerShell-$($env:REQUESTED_VERSION)-win-x64.msi"
201208 $url = "https://github.com/PowerShell/PowerShell/releases/download/v$($env:REQUESTED_VERSION)/$msi"
202- Invoke-WebRequest -Uri $url -OutFile $msi -UseBasicParsing
209+ Write-Host "Downloading from: $url"
210+
211+ Write-Host "Starting installation of PowerShell [$($env:REQUESTED_VERSION)]..."
212+
213+ if (-not (Invoke-WebRequest -Uri $url -OutFile $msi -UseBasicParsing -PassThru)) {
214+ Write-Host "Error: Failed to download PowerShell package"
215+ exit 1
216+ }
203217 Start-Process msiexec.exe -ArgumentList '/i', $msi, '/quiet', '/norestart' -Wait
218+
219+ Write-Host "Installation complete. PowerShell [$($env:REQUESTED_VERSION)] is now available."
0 commit comments