Skip to content

Commit 08c1681

Browse files
berlintayTaojunshensdwheelerv-aljehuypub
authored
Berlintay patch 1 (#1)
* Publish to live (#10442) * Update docs changes (#10441) * Update TOC (#10443) * Update Installing-PowerShell-on-Linux.md docs: enhance PowerShell installation instructions for all Linux distributions (tested on WSL2) --------- Co-authored-by: Taojunshen <[email protected]> Co-authored-by: Sean Wheeler <[email protected]> Co-authored-by: Alma Jenks <[email protected]> Co-authored-by: Huaping Yu <[email protected]> Co-authored-by: Phil <[email protected]> Co-authored-by: Bowen Yang <[email protected]>
1 parent 76e7822 commit 08c1681

File tree

1 file changed

+208
-0
lines changed

1 file changed

+208
-0
lines changed

reference/docs-conceptual/install/Installing-PowerShell-on-Linux.md

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,214 @@ Ubuntu uses APT (Advanced Package Tool) as a package manager.
4949

5050
For more information, see [Install PowerShell on Ubuntu][18].
5151

52+
## Automated Installation Script
53+
The following script automatically detects your Linux distribution, architecture, and installs the latest stable version of PowerShell. It includes:
54+
- Automatic OS and architecture detection
55+
- Latest stable PowerShell version detection
56+
- Interactive prompts for version confirmation
57+
- Comprehensive error handling and logging
58+
- Support for Ubuntu, Debian, RHEL, CentOS, and Fedora
59+
- Support for AMD64 and ARM64 architectures
60+
61+
>[!NOTE]
62+
>Save this script as `linux_autodetect_install.sh` and enable execution with `chmod +x linux_autodetect_install.sh`
63+
64+
[Download linux_autodetect_install.sh](linux_autodetect_install.sh)
65+
66+
<details>
67+
<summary>Click to view installation script</summary>
68+
69+
```bash
70+
#!/bin/bash
71+
72+
# PowerShell Installation Script for Linux
73+
# Description: Automatically detects system type and installs the latest stable PowerShell release
74+
# Features:
75+
# - OS and architecture detection
76+
# - Latest stable version detection
77+
# - Interactive installation
78+
# - Comprehensive error handling
79+
# - Support for major Linux distributions
80+
# Author: Microsoft Corporation
81+
# License: MIT
82+
83+
set -e
84+
85+
# Constants
86+
TEMP_DIR="/tmp"
87+
LOG_PREFIX="[PowerShell Install]"
88+
89+
# Logging function with standardized format
90+
log() {
91+
local level="$1"
92+
local message="$2"
93+
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ${LOG_PREFIX} ${level}: ${message}"
94+
}
95+
96+
# Error handling function
97+
handle_error() {
98+
local exit_code=$?
99+
log "ERROR" "An error occurred on line $1"
100+
exit "$exit_code"
101+
}
102+
103+
# Set up error handling
104+
trap 'handle_error $LINENO' ERR
105+
106+
# System detection functions
107+
get_os_info() {
108+
if [ -f /etc/os-release ]; then
109+
# Load OS information
110+
. /etc/os-release
111+
OS_NAME="${ID}"
112+
OS_VERSION="${VERSION_ID}"
113+
OS_PRETTY_NAME="${PRETTY_NAME}"
114+
else
115+
log "ERROR" "Cannot detect OS information"
116+
exit 1
117+
}
118+
}
119+
120+
get_architecture() {
121+
local arch
122+
arch=$(uname -m)
123+
case $arch in
124+
x86_64)
125+
ARCH="amd64"
126+
;;
127+
aarch64)
128+
ARCH="arm64"
129+
;;
130+
*)
131+
log "ERROR" "Unsupported architecture: $arch"
132+
exit 1
133+
;;
134+
esac
135+
}
136+
137+
setup_package_manager() {
138+
case $OS_NAME in
139+
ubuntu|debian)
140+
PKG_MGR="apt"
141+
PKG_FORMAT="deb"
142+
INSTALL_CMD="sudo dpkg -i"
143+
DEP_CMD="sudo apt-get install -f -y"
144+
;;
145+
rhel|centos|fedora)
146+
PKG_MGR="dnf"
147+
PKG_FORMAT="rpm"
148+
INSTALL_CMD="sudo rpm -i"
149+
DEP_CMD="sudo dnf install -y"
150+
;;
151+
*)
152+
log "ERROR" "Unsupported distribution: $OS_NAME"
153+
exit 1
154+
;;
155+
esac
156+
}
157+
158+
get_download_url() {
159+
local ps_version_url
160+
local pattern
161+
162+
case $OS_NAME in
163+
ubuntu|debian)
164+
pattern="powershell_.*${ARCH}.deb$"
165+
;;
166+
rhel|centos|fedora)
167+
pattern="powershell-.*${ARCH}.rpm$"
168+
;;
169+
esac
170+
171+
ps_version_url=$(curl -s https://api.github.com/repos/PowerShell/PowerShell/releases |
172+
jq -r --arg pattern "$pattern" '
173+
[.[] | select(.prerelease == false)] |
174+
sort_by(.published_at) |
175+
reverse |
176+
.[0].assets[] |
177+
select(.name | test($pattern)) |
178+
.browser_download_url' |
179+
head -n 1)
180+
181+
if [ -z "$ps_version_url" ] || [[ "$ps_version_url" == *"hashes.sha256"* ]]; then
182+
log "ERROR" "Failed to find PowerShell package for $OS_NAME ($ARCH)"
183+
log "DEBUG" "Pattern used: $pattern"
184+
exit 1
185+
fi
186+
187+
echo "$ps_version_url"
188+
}
189+
190+
# Main installation logic
191+
main() {
192+
log "INFO" "Starting PowerShell installation"
193+
194+
# Detect system information
195+
log "INFO" "Detecting system information"
196+
get_os_info
197+
get_architecture
198+
setup_package_manager
199+
200+
log "INFO" "Detected: $OS_PRETTY_NAME ($ARCH)"
201+
202+
# Check for sudo privileges
203+
if ! sudo -v; then
204+
log "ERROR" "This script requires sudo privileges"
205+
exit 1
206+
fi
207+
208+
# Update and install dependencies
209+
log "INFO" "Installing prerequisites"
210+
case $PKG_MGR in
211+
apt)
212+
sudo apt-get update
213+
sudo apt-get install -y curl jq wget
214+
;;
215+
dnf)
216+
sudo dnf check-update
217+
sudo dnf install -y curl jq wget
218+
;;
219+
esac
220+
221+
# Check existing installation
222+
if command -v pwsh &>/dev/null; then
223+
log "INFO" "PowerShell is already installed:"
224+
pwsh --version
225+
read -p "Continue with new installation? (y/n) " -n 1 -r
226+
echo
227+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
228+
log "INFO" "Installation cancelled by user"
229+
exit 0
230+
fi
231+
fi
232+
233+
# Get appropriate download URL
234+
PS_VERSION_URL=$(get_download_url)
235+
if [[ "$PS_VERSION_URL" == "" ]]; then
236+
log "ERROR" "Could not determine download URL"
237+
exit 1
238+
fi
239+
240+
VERSION=$(echo "$PS_VERSION_URL" | grep -Po '(?<=v)[0-9]+\.[0-9]+\.[0-9]+' || echo "unknown")
241+
242+
# Display version information
243+
log "INFO" "Found PowerShell version: $VERSION"
244+
printf "\n%-50s %-15s\n" "URL" "VERSION"
245+
printf "%-50s %-15s\n" "$PS_VERSION_URL" "$VERSION"
246+
247+
# Confirm installation
248+
read -p "Continue with this version? (y/n) " -n 1 -r
249+
echo
250+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
251+
log "INFO" "Installation cancelled by user"
252+
exit 0
253+
fi
254+
255+
# Download and install
256+
local temp_pkg="${TEMP_DIR}/powershell_${VERSION}_${ARCH}.${PKG_FORMAT}"
257+
log "INFO" "Downloading PowerShell package"
258+
```
259+
52260
## Community supported distributions
53261
54262
PowerShell can be installed on many distributions of Linux that aren't supported by Microsoft. In

0 commit comments

Comments
 (0)