Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Berlintay patch 1 (#1) #11600

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 208 additions & 0 deletions reference/docs-conceptual/install/Installing-PowerShell-on-Linux.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,214 @@ Ubuntu uses APT (Advanced Package Tool) as a package manager.

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

## Automated Installation Script
The following script automatically detects your Linux distribution, architecture, and installs the latest stable version of PowerShell. It includes:
- Automatic OS and architecture detection
- Latest stable PowerShell version detection
- Interactive prompts for version confirmation
- Comprehensive error handling and logging
- Support for Ubuntu, Debian, RHEL, CentOS, and Fedora
- Support for AMD64 and ARM64 architectures

>[!NOTE]
>Save this script as `linux_autodetect_install.sh` and enable execution with `chmod +x linux_autodetect_install.sh`

[Download linux_autodetect_install.sh](linux_autodetect_install.sh)

<details>
<summary>Click to view installation script</summary>

```bash
#!/bin/bash

# PowerShell Installation Script for Linux
# Description: Automatically detects system type and installs the latest stable PowerShell release
# Features:
# - OS and architecture detection
# - Latest stable version detection
# - Interactive installation
# - Comprehensive error handling
# - Support for major Linux distributions
# Author: Microsoft Corporation
# License: MIT

set -e

# Constants
TEMP_DIR="/tmp"
LOG_PREFIX="[PowerShell Install]"

# Logging function with standardized format
log() {
local level="$1"
local message="$2"
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ${LOG_PREFIX} ${level}: ${message}"
}

# Error handling function
handle_error() {
local exit_code=$?
log "ERROR" "An error occurred on line $1"
exit "$exit_code"
}

# Set up error handling
trap 'handle_error $LINENO' ERR

# System detection functions
get_os_info() {
if [ -f /etc/os-release ]; then
# Load OS information
. /etc/os-release
OS_NAME="${ID}"
OS_VERSION="${VERSION_ID}"
OS_PRETTY_NAME="${PRETTY_NAME}"
else
log "ERROR" "Cannot detect OS information"
exit 1
}
}

get_architecture() {
local arch
arch=$(uname -m)
case $arch in
x86_64)
ARCH="amd64"
;;
aarch64)
ARCH="arm64"
;;
*)
log "ERROR" "Unsupported architecture: $arch"
exit 1
;;
esac
}

setup_package_manager() {
case $OS_NAME in
ubuntu|debian)
PKG_MGR="apt"
PKG_FORMAT="deb"
INSTALL_CMD="sudo dpkg -i"
DEP_CMD="sudo apt-get install -f -y"
;;
rhel|centos|fedora)
PKG_MGR="dnf"
PKG_FORMAT="rpm"
INSTALL_CMD="sudo rpm -i"
DEP_CMD="sudo dnf install -y"
;;
*)
log "ERROR" "Unsupported distribution: $OS_NAME"
exit 1
;;
esac
}

get_download_url() {
local ps_version_url
local pattern

case $OS_NAME in
ubuntu|debian)
pattern="powershell_.*${ARCH}.deb$"
;;
rhel|centos|fedora)
pattern="powershell-.*${ARCH}.rpm$"
;;
esac

ps_version_url=$(curl -s https://api.github.com/repos/PowerShell/PowerShell/releases |
jq -r --arg pattern "$pattern" '
[.[] | select(.prerelease == false)] |
sort_by(.published_at) |
reverse |
.[0].assets[] |
select(.name | test($pattern)) |
.browser_download_url' |
head -n 1)

if [ -z "$ps_version_url" ] || [[ "$ps_version_url" == *"hashes.sha256"* ]]; then
log "ERROR" "Failed to find PowerShell package for $OS_NAME ($ARCH)"
log "DEBUG" "Pattern used: $pattern"
exit 1
fi

echo "$ps_version_url"
}

# Main installation logic
main() {
log "INFO" "Starting PowerShell installation"

# Detect system information
log "INFO" "Detecting system information"
get_os_info
get_architecture
setup_package_manager

log "INFO" "Detected: $OS_PRETTY_NAME ($ARCH)"

# Check for sudo privileges
if ! sudo -v; then
log "ERROR" "This script requires sudo privileges"
exit 1
fi

# Update and install dependencies
log "INFO" "Installing prerequisites"
case $PKG_MGR in
apt)
sudo apt-get update
sudo apt-get install -y curl jq wget
;;
dnf)
sudo dnf check-update
sudo dnf install -y curl jq wget
;;
esac

# Check existing installation
if command -v pwsh &>/dev/null; then
log "INFO" "PowerShell is already installed:"
pwsh --version
read -p "Continue with new installation? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log "INFO" "Installation cancelled by user"
exit 0
fi
fi

# Get appropriate download URL
PS_VERSION_URL=$(get_download_url)
if [[ "$PS_VERSION_URL" == "" ]]; then
log "ERROR" "Could not determine download URL"
exit 1
fi

VERSION=$(echo "$PS_VERSION_URL" | grep -Po '(?<=v)[0-9]+\.[0-9]+\.[0-9]+' || echo "unknown")

# Display version information
log "INFO" "Found PowerShell version: $VERSION"
printf "\n%-50s %-15s\n" "URL" "VERSION"
printf "%-50s %-15s\n" "$PS_VERSION_URL" "$VERSION"

# Confirm installation
read -p "Continue with this version? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log "INFO" "Installation cancelled by user"
exit 0
fi

# Download and install
local temp_pkg="${TEMP_DIR}/powershell_${VERSION}_${ARCH}.${PKG_FORMAT}"
log "INFO" "Downloading PowerShell package"
```

## Community supported distributions

PowerShell can be installed on many distributions of Linux that aren't supported by Microsoft. In
Expand Down
Loading