MegaBites-AI/Windows-powershell
0372
1#!/bin/bash2# Copyright (c) Microsoft Corporation.3# Licensed under the MIT License.4 5#Companion code for the blog https://cloudywindows.com6#call this code direction from the web with:7#bash <(wget -O - https://raw.githubusercontent.com/PowerShell/PowerShell/master/tools/installpsh-amazonlinux.sh) ARGUMENTS8#bash <(curl -s https://raw.githubusercontent.com/PowerShell/PowerShell/master/tools/installpsh-amazonlinux.sh) <ARGUMENTS>9 10#Usage - if you do not have the ability to run scripts directly from the web,11# pull all files in this repo folder and execute, this script12# automatically prefers local copies of sub-scripts13 14#Completely automated install requires a root account or sudo with a password requirement15 16#Switches17# -includeide - ignored, as Amazon Linux does not have a desktop environment18# -interactivetesting - ignored, as Amazon Linux does not have a desktop environment19# -skip-sudo-check - use sudo without verifying its availability (hard to accurately do on some distros)20# -preview - installs the latest preview release of PowerShell side-by-side with any existing production releases21 22#gitrepo paths are overrideable to run from your own fork or branch for testing or private distribution23 24 25VERSION="1.2.0"26gitreposubpath="PowerShell/PowerShell/master"27gitreposcriptroot="https://raw.githubusercontent.com/$gitreposubpath/tools"28thisinstallerdistro=amazonlinux29repobased=false30gitscriptname="installpsh-amazonlinux.psh"31pwshlink=/usr/bin/pwsh32 33echo34echo "*** PowerShell Development Environment Installer $VERSION for $thisinstallerdistro"35echo "*** Original script is at: $gitreposcriptroot/$gitscriptname"36echo37echo "*** Arguments used: $*"38echo39 40# Let's quit on interrupt of subcommands41trap '42 trap - INT # restore default INT handler43 echo "Interrupted"44 kill -s INT "$$"45' INT46 47#Verify The Installer Choice (for direct runs of this script)48lowercase(){49 echo "$1" | tr "[:upper:]" "[:lower:]"50}51 52OS=$(lowercase "$(uname)")53if [ "${OS}" == "windowsnt" ]; then54 OS=windows55 DistroBasedOn=windows56elif [ "${OS}" == "darwin" ]; then57 OS=osx58 DistroBasedOn=osx59else60 OS=$(uname)61 if [ "${OS}" == "SunOS" ] ; then62 OS=solaris63 DistroBasedOn=sunos64 elif [ "${OS}" == "AIX" ] ; then65 DistroBasedOn=aix66 elif [ "${OS}" == "Linux" ] ; then67 if [ -f /etc/redhat-release ] ; then68 DistroBasedOn='redhat'69 elif [ -f /etc/system-release ] ; then70 DIST=$(sed s/\ release.*// < /etc/system-release)71 if [[ $DIST == *"Amazon Linux"* ]] ; then72 DistroBasedOn='amazonlinux'73 else74 DistroBasedOn='redhat'75 fi76 elif [ -f /etc/SuSE-release ] ; then77 DistroBasedOn='suse'78 elif [ -f /etc/mandrake-release ] ; then79 DistroBasedOn='mandrake'80 elif [ -f /etc/debian_version ] ; then81 DistroBasedOn='debian'82 fi83 if [ -f /etc/UnitedLinux-release ] ; then84 DIST="${DIST}[$( (tr "\n" ' ' | sed s/VERSION.*//) < /etc/UnitedLinux-release )]"85 DistroBasedOn=unitedlinux86 fi87 OS=$(lowercase "$OS")88 DistroBasedOn=$(lowercase "$DistroBasedOn")89 fi90fi91 92if [ "$DistroBasedOn" != "$thisinstallerdistro" ]; then93 echo "*** This installer is only for $thisinstallerdistro and you are running $DistroBasedOn, please run \"$gitreposcriptroot\install-powershell.sh\" to see if your distro is supported AND to auto-select the appropriate installer if it is."94 exit 195fi96 97## Check requirements and prerequisites98 99#Check for sudo if not root100if [[ "${CI}" == "true" ]]; then101 echo "Running on CI (as determined by env var CI set to true), skipping SUDO check."102 set -- "$@" '-skip-sudo-check'103fi104 105SUDO=''106if (( EUID != 0 )); then107 #Check that sudo is available108 if [[ ("'$*'" =~ skip-sudo-check) && ("$(whereis sudo)" == *'/'* && "$(sudo -nv 2>&1)" != 'Sorry, user'*) ]]; then109 SUDO='sudo'110 else111 echo "ERROR: You must either be root or be able to use sudo" >&2112 #exit 5113 fi114fi115 116#Collect any variation details if required for this distro117 118#END Verify The Installer Choice119 120echo121echo "*** Installing prerequisites for PowerShell..."122$SUDO yum install -y \123 curl \124 libunwind \125 libicu \126 libcurl \127 openssl \128 libuuid.x86_64 \129 tar \130 gzip \131 && yum clean all132 133##END Check requirements and prerequisites134 135echo136echo "*** Installing PowerShell for $DistroBasedOn..."137 138echo "ATTENTION: As of version 1.2.0 this script no longer uses pre-releases unless the '-preview' switch is used"139 140if [[ "'$*'" =~ preview ]] ; then141 echo142 echo "-preview was used, the latest preview release will be installed (side-by-side with your production release)"143 release=$(curl https://api.github.com/repos/powershell/powershell/releases/latest | sed '/tag_name/!d' | sed s/\"tag_name\"://g | sed s/\"//g | sed s/v// | sed s/,//g | sed s/\ //g)144 pwshlink=/usr/bin/pwsh-preview145else146 echo "Finding the latest production release"147 release=$(curl https://api.github.com/repos/PowerShell/PowerShell/releases | grep -Po '"tag_name":(\d*?,|.*?[^\\]",)' | grep -Po '\d+.\d+.\d+[\da-z.-]*' | grep -v '[a-z]' | sort | tail -n1)148fi149 150#DIRECT DOWNLOAD151package=powershell-${release}-linux-x64.tar.gz152downloadurl=https://github.com/PowerShell/PowerShell/releases/download/v$release/$package153 154echo "Destination file: $package"155echo "Source URL: $downloadurl"156 157curl -L -o "$package" "$downloadurl"158 159if [[ ! -r "$package" ]]; then160 echo "ERROR: $package failed to download! Aborting..." >&2161 exit 1162fi163 164echo "Installing PowerShell to /opt/microsoft/powershell/$release in overwrite mode"165## Create the target folder where powershell will be placed166$SUDO mkdir -p "/opt/microsoft/powershell/$release"167## Expand powershell to the target folder168$SUDO tar zxf "$package" -C "/opt/microsoft/powershell/$release"169 170## Change the mode of 'pwsh' to 'rwxr-xr-x' to allow execution171$SUDO chmod 755 "/opt/microsoft/powershell/$release/pwsh"172## Create the symbolic link that points to powershell173$SUDO ln -sfn "/opt/microsoft/powershell/$release/pwsh" $pwshlink174 175## Add the symbolic link path to /etc/shells176if [ ! -f /etc/shells ] ; then177 echo $pwshlink | $SUDO tee /etc/shells ;178else179 grep -q "^${pwshlink}$" /etc/shells || echo $pwshlink | $SUDO tee --append /etc/shells > /dev/null ;180fi181 182## Remove the downloaded package file183rm -f "$package"184 185# shellcheck disable=SC2016186pwsh -noprofile -c '"Congratulations! PowerShell is installed at $PSHOME.187Run `"pwsh`" to start a PowerShell session."'188 189success=$?190 191if [[ "$success" != 0 ]]; then192 echo "ERROR: PowerShell failed to install!" >&2193 exit "$success"194fi195 196if [[ "'$*'" =~ includeide ]] ; then197 echo198 echo "Amazon Linux does not have a desktop manager to support vscode, ignoring -includeide"199fi200 201if [[ "'$*'" =~ -interactivetesting ]] ; then202 echo203 echo "Amazon Linux does not have a desktop manager to support vscode, ignoring -interactivetesting"204fi205 206if [[ "$repobased" == true ]] ; then207 echo "*** NOTE: Run your regular package manager update cycle to update PowerShell"208else209 echo "*** NOTE: Re-run this script to update PowerShell"210fi211echo "*** Install Complete"212 