MegaBites-AI/Windows-powershell
0372
1#!/bin/bash2 3#Companion code for the blog https://cloudywindows.com4#call this code direction from the web with:5#bash <(wget -O - https://raw.githubusercontent.com/PowerShell/PowerShell/master/tools/installpsh-debian.sh) ARGUMENTS6#bash <(curl -s https://raw.githubusercontent.com/PowerShell/PowerShell/master/tools/installpsh-debian.sh) <ARGUMENTS>7 8#Usage - if you do not have the ability to run scripts directly from the web,9# pull all files in this repo folder and execute, this script10# automatically prefers local copies of sub-scripts11 12#Completely automated install requires a root account or sudo with a password requirement13 14#Switches15# -includeide - installs VSCode and VSCode PowerShell extension (only relevant to machines with desktop environment)16# -interactivetesting - do a quick launch test of VSCode (only relevant when used with -includeide)17# -skip-sudo-check - use sudo without verifying its availability (hard to accurately do on some distros)18# -preview - installs the latest preview release of PowerShell side-by-side with any existing production releases19 20#gitrepo paths are overrideable to run from your own fork or branch for testing or private distribution21 22VERSION="1.2.1"23gitreposubpath="PowerShell/PowerShell/master"24gitreposcriptroot="https://raw.githubusercontent.com/$gitreposubpath/tools"25thisinstallerdistro=debian26repobased=true27gitscriptname="installpsh-debian.psh"28powershellpackageid=powershell29 30echo ;31echo "*** PowerShell Development Environment Installer $VERSION for $thisinstallerdistro"32echo "*** Original script is at: $gitreposcriptroot/$gitscriptname"33echo34echo "*** Arguments used: $*"35 36# Let's quit on interrupt of subcommands37trap '38 trap - INT # restore default INT handler39 echo "Interrupted"40 kill -s INT "$$"41' INT42 43#Verify The Installer Choice (for direct runs of this script)44lowercase(){45 echo "$1" | tr "[:upper:]" "[:lower:]"46}47 48OS=$(lowercase "$(uname)")49if [ "${OS}" == "windowsnt" ]; then50 OS=windows51 DistroBasedOn=windows52elif [ "${OS}" == "darwin" ]; then53 OS=osx54 DistroBasedOn=osx55else56 OS=$(uname)57 if [ "${OS}" == "SunOS" ] ; then58 OS=solaris59 DistroBasedOn=sunos60 elif [ "${OS}" == "AIX" ] ; then61 DistroBasedOn=aix62 elif [ "${OS}" == "Linux" ] ; then63 if [ -f /etc/redhat-release ] ; then64 DistroBasedOn='redhat'65 elif [ -f /etc/system-release ] ; then66 DIST=$(sed s/\ release.*// < /etc/system-release)67 if [[ $DIST == *"Amazon Linux"* ]] ; then68 DistroBasedOn='amazonlinux'69 else70 DistroBasedOn='redhat'71 fi72 elif [ -f /etc/SuSE-release ] ; then73 DistroBasedOn='suse'74 elif [ -f /etc/mandrake-release ] ; then75 DistroBasedOn='mandrake'76 elif [ -f /etc/debian_version ] ; then77 DistroBasedOn='debian'78 fi79 if [ -f /etc/UnitedLinux-release ] ; then80 DIST="${DIST}[$( (tr "\n" ' ' | sed s/VERSION.*//) < /etc/UnitedLinux-release )]"81 DistroBasedOn=unitedlinux82 fi83 OS=$(lowercase "$OS")84 DistroBasedOn=$(lowercase "$DistroBasedOn")85 fi86fi87 88if [ "$DistroBasedOn" != "$thisinstallerdistro" ]; then89 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."90 exit 191fi92 93## Check requirements and prerequisites94 95#Check for sudo if not root96if [[ "${CI}" == "true" ]]; then97 echo "Running on CI (as determined by env var CI set to true), skipping SUDO check."98 set -- "$@" '-skip-sudo-check'99fi100 101SUDO=''102if (( EUID != 0 )); then103 #Check that sudo is available104 if [[ ("'$*'" =~ skip-sudo-check) || ("$(whereis sudo)" == *'/'* && "$(sudo -nv 2>&1)" != 'Sorry, user'*) ]]; then105 SUDO='sudo'106 else107 echo "ERROR: You must either be root or be able to use sudo" >&2108 #exit 5109 fi110fi111 112#Collect any variation details if required for this distro113# shellcheck disable=SC1091114if [[ -f /etc/lsb-release ]]; then115 . /etc/lsb-release116 DISTRIB_ID=$(lowercase "$DISTRIB_ID")117elif [[ -f /etc/debian_version ]]; then118 DISTRIB_ID="debian"119 DISTRIB_RELEASE=$(cat /etc/debian_version)120fi121#END Collect any variation details if required for this distro122 123#If there are known incompatible versions of this distro, put the test, message and script exit here:124 125#END Verify The Installer Choice126 127##END Check requirements and prerequisites128 129echo130echo "*** Installing PowerShell for $DistroBasedOn..."131if ! hash curl 2>/dev/null; then132 echo "curl not found, installing..."133 $SUDO apt-get install -y curl134fi135 136# The executable to test.137PWSH=pwsh138 139if [[ "'$*'" =~ preview ]] ; then140 echo141 echo "-preview was used, the latest preview release will be installed (side-by-side with your production release)"142 powershellpackageid=powershell-preview143 PWSH=pwsh-preview144fi145 146currentversion=$(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)147 148echo "*** Current version on git is: $currentversion, repo version may differ slightly..."149echo "*** Setting up PowerShell repo..."150# Import the public repository GPG keys151curl https://packages.microsoft.com/keys/microsoft.asc | $SUDO apt-key add -152#Add the Repo153if [[ "${DISTRIB_ID}" = "linuxmint" ]]; then154 echo "Attempting to remap linuxmint to an appropriate ubuntu version" >&2155 LINUXMINT_VERSION=${DISTRIB_RELEASE}156 #https://en.wikipedia.org/wiki/Linux_Mint_version_history157 case ${LINUXMINT_VERSION} in158 19*)159 DISTRIB_RELEASE=18.04160 ;;161 18*)162 DISTRIB_RELEASE=16.04163 ;;164 17*)165 DISTRIB_RELEASE=14.04166 ;;167 *)168 echo "ERROR: unsupported linuxmint version (${LINUXMINT_VERSION})." >&2169 echo "Supported versions: 19" >&2170 echo "For additional versions open an issue or pull request at: https://github.com/powershell/powershell" >&2171 exit 1172 ;;173 esac174 echo "Remapping linuxmint version ${LINUXMINT_VERSION} to ubuntu version ${DISTRIB_RELEASE}" >&2175fi176case $DISTRIB_ID in177 ubuntu|linuxmint)178 case $DISTRIB_RELEASE in179 22.04|20.04|18.04|16.10|16.04|15.10|14.04)180 curl https://packages.microsoft.com/config/ubuntu/$DISTRIB_RELEASE/prod.list | $SUDO tee /etc/apt/sources.list.d/microsoft.list181 ;;182 *)183 echo "ERROR: unsupported Ubuntu version ($DISTRIB_RELEASE)." >&2184 echo "Supported versions: 14.04, 15.10, 16.04, 16.10, 18.04, 20.04." >&2185 echo "For additional versions open an issue or pull request at: https://github.com/powershell/powershell" >&2186 exit 1187 ;;188 esac189 ;;190 debian)191 DISTRIB_RELEASE=${DISTRIB_RELEASE%%.*}192 case $DISTRIB_RELEASE in193 8|9|10|11)194 curl https://packages.microsoft.com/config/debian/$DISTRIB_RELEASE/prod.list | $SUDO tee /etc/apt/sources.list.d/microsoft.list195 ;;196 *)197 echo "ERROR: unsupported Debian version ($DISTRIB_RELEASE)." >&2198 echo "Supported versions: 8, 9." >&2199 echo "For additional versions open an issue or pull request at: https://github.com/powershell/powershell" >&2200 exit 1201 ;;202 esac203 ;;204 *)205 echo "ERROR: unsupported Debian-based distribution ($DISTRIB_ID)." >&2206 echo "Supported distributions: Debian, Ubuntu." >&2207 exit 1208 ;;209esac210 211# Update apt-get212$SUDO apt-get update213# Install PowerShell214$SUDO apt-get install -y ${powershellpackageid}215 216# shellcheck disable=SC2016217$PWSH -noprofile -c '"Congratulations! PowerShell is installed at $PSHOME.218Run `"'"$PWSH"'`" to start a PowerShell session."'219 220success=$?221 222if [[ "$success" != 0 ]]; then223 echo "ERROR: PowerShell failed to install!" >&2224 exit "$success"225fi226 227if [[ "'$*'" =~ includeide ]] ; then228 echo229 echo "*** Installing VS Code PowerShell IDE..."230 echo "*** Setting up VS Code repo..."231 curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg232 $SUDO mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg233 $SUDO sh -c 'echo "deb [arch=amd64] http://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'234 $SUDO apt-get update235 $SUDO apt-get install -y code236 237 echo238 echo "*** Installing VS Code PowerShell Extension"239 code --install-extension ms-vscode.PowerShell240 if [[ "'$*'" =~ -interactivetesting ]] ; then241 echo "*** Loading test code in VS Code"242 curl -O ./testpowershell.ps1 https://raw.githubusercontent.com/DarwinJS/CloudyWindowsAutomationCode/master/pshcoredevenv/testpowershell.ps1243 code ./testpowershell.ps1244 fi245fi246 247if [[ "$repobased" == true ]] ; then248 echo249 echo "*** NOTE: Run your regular package manager update cycle to update PowerShell"250fi251echo "*** Install Complete"252 