|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Microsoft Intune Portal Installer |
| 4 | +# |
| 5 | +# Installs Microsoft Intune Portal and its dependencies (including Microsoft Edge) |
| 6 | +# on supported Linux distributions. |
| 7 | +# |
| 8 | +# Supported distributions: |
| 9 | +# - Ubuntu 22.04, 24.04, 26.04 |
| 10 | +# - RHEL/AlmaLinux 8, 9, 10 |
| 11 | +# |
| 12 | +# Usage: |
| 13 | +# ./installer.sh [OPTIONS] |
| 14 | +# |
| 15 | +# Options: |
| 16 | +# --insiders-fast Use the insiders-fast channel instead of prod |
| 17 | +# --local-package <path> Install from a local .deb/.rpm file instead of the repo |
| 18 | +# --verbose Show detailed output to the terminal (in addition to the log) |
| 19 | +# -h, --help Show this help message |
| 20 | +# |
| 21 | + |
| 22 | +set -eu${DEBUG:+x}o pipefail |
| 23 | + |
| 24 | +# Handle --help before elevating to root |
| 25 | +for arg in "$@"; do |
| 26 | + if [[ "$arg" == "-h" || "$arg" == "--help" ]]; then |
| 27 | + sed -n '/^# Usage:/,/^[^#]/p' "$0" | head -n -1 | sed 's/^# \?//' |
| 28 | + exit 0 |
| 29 | + fi |
| 30 | +done |
| 31 | + |
| 32 | +# Elevate to root if not already running as root (before arg parsing so $@ is intact) |
| 33 | +if [[ $EUID -ne 0 ]]; then |
| 34 | + exec sudo bash "$0" "$@" |
| 35 | +fi |
| 36 | + |
| 37 | +# --- Defaults --- |
| 38 | +CHANNEL="prod" |
| 39 | +LOCAL_PACKAGE="" |
| 40 | +VERBOSE=false |
| 41 | +# Use the invoking user's home directory, not root's |
| 42 | +USER_HOME="${SUDO_USER:+$(eval echo ~"$SUDO_USER")}" |
| 43 | +LOG_FILE="${USER_HOME:-$HOME}/intune-installer.log" |
| 44 | + |
| 45 | +# --- Functions --- |
| 46 | + |
| 47 | +usage() { |
| 48 | + sed -n '/^# Usage:/,/^[^#]/p' "$0" | head -n -1 | sed 's/^# \?//' |
| 49 | + exit 0 |
| 50 | +} |
| 51 | + |
| 52 | +log() { |
| 53 | + echo "$1" >&7 |
| 54 | +} |
| 55 | + |
| 56 | +die() { |
| 57 | + # Write to original stdout (fd 7) if available, otherwise stderr |
| 58 | + if { true >&7; } 2>/dev/null; then |
| 59 | + echo "Error: $1" >&7 |
| 60 | + fi |
| 61 | + echo "Error: $1" >&2 |
| 62 | + exit 1 |
| 63 | +} |
| 64 | + |
| 65 | +detect_arch() { |
| 66 | + local arch |
| 67 | + arch=$(dpkg --print-architecture 2>/dev/null || rpm --eval '%{_arch}' 2>/dev/null || uname -m) |
| 68 | + case "$arch" in |
| 69 | + x86_64) echo "amd64" ;; |
| 70 | + aarch64) echo "arm64" ;; |
| 71 | + *) echo "$arch" ;; |
| 72 | + esac |
| 73 | +} |
| 74 | + |
| 75 | +# Remove duplicate Edge repo files that Edge's postinst script may create. |
| 76 | +# We maintain a single microsoft-edge.list — any others are duplicates. |
| 77 | +cleanup_edge_repo_duplicates() { |
| 78 | + local sources_dir="/etc/apt/sources.list.d" |
| 79 | + for f in "$sources_dir"/microsoft-edge-*.list; do |
| 80 | + [ -f "$f" ] || continue |
| 81 | + sudo rm -f "$f" |
| 82 | + done |
| 83 | +} |
| 84 | + |
| 85 | +# --- Argument Parsing --- |
| 86 | + |
| 87 | +while [[ $# -gt 0 ]]; do |
| 88 | + case "$1" in |
| 89 | + --insiders-fast) |
| 90 | + CHANNEL="insiders-fast" |
| 91 | + shift |
| 92 | + ;; |
| 93 | + --local-package) |
| 94 | + [[ $# -ge 2 ]] || die "--local-package requires a file path argument" |
| 95 | + LOCAL_PACKAGE="$2" |
| 96 | + shift 2 |
| 97 | + ;; |
| 98 | + --verbose) |
| 99 | + VERBOSE=true |
| 100 | + shift |
| 101 | + ;; |
| 102 | + -h|--help) |
| 103 | + usage |
| 104 | + ;; |
| 105 | + *) |
| 106 | + die "Unknown option: $1 (see --help)" |
| 107 | + ;; |
| 108 | + esac |
| 109 | +done |
| 110 | + |
| 111 | +if [[ -n "$LOCAL_PACKAGE" && ! -f "$LOCAL_PACKAGE" ]]; then |
| 112 | + die "Local package not found: $LOCAL_PACKAGE" |
| 113 | +fi |
| 114 | + |
| 115 | +# --- Pre-flight --- |
| 116 | + |
| 117 | +# Read distro information |
| 118 | +. /etc/os-release |
| 119 | +DISTRO="$ID" |
| 120 | +RELEASE="$VERSION_ID" |
| 121 | +ARCH=$(detect_arch) |
| 122 | + |
| 123 | +# Print summary |
| 124 | +echo "=============================================" |
| 125 | +echo " Microsoft Intune Portal Installer" |
| 126 | +echo "=============================================" |
| 127 | +echo " Distro: $DISTRO $RELEASE" |
| 128 | +echo " Architecture: $ARCH" |
| 129 | +echo " Channel: $CHANNEL" |
| 130 | +if [[ -n "$LOCAL_PACKAGE" ]]; then |
| 131 | + echo " Local package: $LOCAL_PACKAGE" |
| 132 | +fi |
| 133 | +echo " Log file: $LOG_FILE" |
| 134 | +echo "=============================================" |
| 135 | +echo "" |
| 136 | + |
| 137 | +# Redirect output to log file, keeping fd 7 for user-facing messages |
| 138 | +exec 7>&1 |
| 139 | +if $VERBOSE; then |
| 140 | + exec &> >(tee -a "$LOG_FILE") |
| 141 | +else |
| 142 | + exec &>"$LOG_FILE" |
| 143 | +fi |
| 144 | + |
| 145 | +# --- Distro-specific installation --- |
| 146 | + |
| 147 | +case "$DISTRO" in |
| 148 | +"ubuntu") |
| 149 | + # Validate supported release |
| 150 | + if [[ "$RELEASE" != "22.04" && "$RELEASE" != "24.04" && "$RELEASE" != "26.04" ]]; then |
| 151 | + die "Ubuntu $RELEASE is not supported. Supported versions: 22.04, 24.04, 26.04" |
| 152 | + fi |
| 153 | + |
| 154 | + CODENAME="$VERSION_CODENAME" |
| 155 | + |
| 156 | + log "Installing prerequisites..." |
| 157 | + |
| 158 | + sudo apt-get -y install curl gpg |
| 159 | + |
| 160 | + # Import Microsoft GPG key (idempotent) |
| 161 | + if [[ ! -f /usr/share/keyrings/microsoft.gpg ]]; then |
| 162 | + curl -fsSL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > "$HOME/microsoft.gpg" |
| 163 | + sudo install -o root -g root -m 644 "$HOME/microsoft.gpg" /usr/share/keyrings/ |
| 164 | + rm -f "$HOME/microsoft.gpg" |
| 165 | + fi |
| 166 | + |
| 167 | + # Clean up any pre-existing Edge repo files to avoid duplicates |
| 168 | + cleanup_edge_repo_duplicates |
| 169 | + |
| 170 | + # Configure repo for portal and dependencies |
| 171 | + # For insiders-fast, the URL path stays as "prod" and "insiders-fast" is the apt component |
| 172 | + if [[ "$CHANNEL" == "insiders-fast" ]]; then |
| 173 | + APT_COMPONENT="insiders-fast" |
| 174 | + else |
| 175 | + APT_COMPONENT="$CODENAME" |
| 176 | + fi |
| 177 | + echo "deb [arch=$ARCH signed-by=/usr/share/keyrings/microsoft.gpg] https://packages.microsoft.com/ubuntu/$RELEASE/prod $APT_COMPONENT main" \ |
| 178 | + | sudo tee /etc/apt/sources.list.d/microsoft-$CHANNEL.list > /dev/null |
| 179 | + |
| 180 | + # Configure repo for Edge |
| 181 | + echo "deb [arch=$ARCH signed-by=/usr/share/keyrings/microsoft.gpg] https://packages.microsoft.com/repos/edge stable main" \ |
| 182 | + | sudo tee /etc/apt/sources.list.d/microsoft-edge.list > /dev/null |
| 183 | + |
| 184 | + log "Updating package information..." |
| 185 | + sudo apt-get update |
| 186 | + |
| 187 | + # Install Edge if not already present |
| 188 | + if ! command -v microsoft-edge &>/dev/null; then |
| 189 | + log "Installing Microsoft Edge..." |
| 190 | + sudo apt-get -y install microsoft-edge-stable |
| 191 | + |
| 192 | + # Edge's postinst may add duplicate repo files — clean them up |
| 193 | + cleanup_edge_repo_duplicates |
| 194 | + else |
| 195 | + log "Microsoft Edge is already installed." |
| 196 | + fi |
| 197 | + |
| 198 | + # Install Intune Portal |
| 199 | + log "Installing Intune Portal..." |
| 200 | + if [[ -n "$LOCAL_PACKAGE" ]]; then |
| 201 | + log "Using local package: $LOCAL_PACKAGE" |
| 202 | + sudo apt-get -y --allow-downgrades install "$(readlink -f "$LOCAL_PACKAGE")" |
| 203 | + else |
| 204 | + sudo apt-get -y install intune-portal |
| 205 | + fi |
| 206 | + ;; |
| 207 | + |
| 208 | +"rhel"|"almalinux") |
| 209 | + MAJOR="${RELEASE%%.*}" |
| 210 | + |
| 211 | + # Validate supported release |
| 212 | + if [[ "$MAJOR" != "8" && "$MAJOR" != "9" && "$MAJOR" != "10" ]]; then |
| 213 | + die "$DISTRO $RELEASE is not supported. Supported major versions: 8, 9, 10" |
| 214 | + fi |
| 215 | + |
| 216 | + log "Installing prerequisites..." |
| 217 | + |
| 218 | + # RHEL 10+ uses a newer Microsoft GPG signing key |
| 219 | + if [[ "$MAJOR" -ge 10 ]]; then |
| 220 | + MS_GPG_KEY="https://packages.microsoft.com/rhel/$MAJOR/$CHANNEL/repodata/repomd.xml.key" |
| 221 | + # RHEL 10 requires EPEL for webkitgtk6.0 dependency |
| 222 | + rpm -q epel-release || sudo dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-10.noarch.rpm |
| 223 | + else |
| 224 | + MS_GPG_KEY="https://packages.microsoft.com/keys/microsoft.asc" |
| 225 | + fi |
| 226 | + |
| 227 | + # For portal and dependencies |
| 228 | + sudo dnf config-manager --add-repo https://packages.microsoft.com/rhel/$MAJOR/prod |
| 229 | + |
| 230 | + # Import Microsoft GPG key |
| 231 | + sudo rpm --import "$MS_GPG_KEY" |
| 232 | + |
| 233 | + # Configure repo for portal and dependencies |
| 234 | + sudo tee /etc/yum.repos.d/microsoft-${CHANNEL}.repo > /dev/null <<EOF |
| 235 | +[microsoft-${CHANNEL}] |
| 236 | +name=Microsoft $CHANNEL - RHEL $MAJOR |
| 237 | +baseurl=https://packages.microsoft.com/rhel/$MAJOR/$CHANNEL |
| 238 | +enabled=1 |
| 239 | +gpgcheck=1 |
| 240 | +gpgkey=$MS_GPG_KEY |
| 241 | +EOF |
| 242 | + |
| 243 | + # Configure repo for Edge |
| 244 | + sudo tee /etc/yum.repos.d/microsoft-edge.repo > /dev/null <<EOF |
| 245 | +[microsoft-edge] |
| 246 | +name=Microsoft Edge |
| 247 | +baseurl=https://packages.microsoft.com/yumrepos/edge |
| 248 | +enabled=1 |
| 249 | +gpgcheck=1 |
| 250 | +gpgkey=https://packages.microsoft.com/keys/microsoft.asc |
| 251 | +EOF |
| 252 | + |
| 253 | + log "Updating package information..." |
| 254 | + |
| 255 | + # Install Edge if not already present |
| 256 | + if ! command -v microsoft-edge &>/dev/null; then |
| 257 | + log "Installing Microsoft Edge..." |
| 258 | + sudo dnf -y install microsoft-edge-stable |
| 259 | + else |
| 260 | + log "Microsoft Edge is already installed." |
| 261 | + fi |
| 262 | + |
| 263 | + # Install Intune Portal |
| 264 | + log "Installing Intune Portal..." |
| 265 | + if [[ -n "$LOCAL_PACKAGE" ]]; then |
| 266 | + log "Using local package: $LOCAL_PACKAGE" |
| 267 | + sudo dnf -y install "$(readlink -f "$LOCAL_PACKAGE")" |
| 268 | + else |
| 269 | + sudo dnf -y install intune-portal |
| 270 | + fi |
| 271 | + ;; |
| 272 | + |
| 273 | +*) |
| 274 | + die "$DISTRO is not a supported distribution. Supported: Ubuntu 22.04/24.04/26.04, RHEL/AlmaLinux 8/9/10" |
| 275 | + ;; |
| 276 | +esac |
| 277 | + |
| 278 | +# --- Done --- |
| 279 | +log "" |
| 280 | +log "Installation complete. Log file: $LOG_FILE" |
0 commit comments