forked from bcdady/MyScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstall_PowerShell_MacOS.sh
executable file
·139 lines (122 loc) · 4.83 KB
/
Install_PowerShell_MacOS.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
############################################################################
# Uninstall / re-install PowerShell on Mac
# per https://github.com/PowerShell/PowerShell/blob/master/docs/installation/macos.md as of 2/18/18
#
# Steps performed by this script, in order:
# * Try to uninstall PowerShell via Homebrew
# * Delete (rm) known PowerShell directories, in case PowerShell was/had been installed via pkg
# * Delete (rm) known PowerShell directories, in case PowerShell was/had been installed via binary
# * Try to re-install PowerShell via Homebrew, after ensuring that Homebrew is updated
############################################################################
# Set UNINSTALL="TRUE" for force deletion of any existing PowerShell directories
UNINSTALL='FALSE'
#PWSHMACURL='https://github.com/PowerShell/PowerShell/releases/download/v6.0.1/powershell-6.0.1-osx-x64.tar.gz'
#PWSHMACVER='6.0.1'
# To install PowerShell Core 6.0+ via Homebrew, local Mac OS must be at least 10.12
req_version='12'
# Detect OS version and confirm OS name
product_name=$(sw_vers -productName)
product_version=$(sw_vers -productVersion)
os_vers=( ${product_version//./ } )
major_version="${os_vers[0]}"
minor_version="${os_vers[1]}"
clear
echo ''
echo '*** Uninstall / re-install PowerShell on Mac ***'
echo "Detected Host OS: $product_name ${major_version}.${minor_version}"
homebrew_version="$(brew --version | grep 'Homebrew >=')"
# To avoid/workaround decimal comparisons, we assume OSX 10.n and only compare minor versions
if [ "${minor_version}" -lt "${req_version}" ]; then
echo "$product_name ${major_version}.${minor_version} is not supported for package management via Homebrew Cask"
else
if [ $homebrew_version ]; then
echo "${homebrew_version}"
echo 'Try to uninstall PowerShell via Homebrew'
brew cask uninstall powershell
else
echo 'Homebrew not found.'
fi
echo '*** Install PowerShell Core on macOS (via Homebrew) ***'
echo ''
echo "Detected Host OS: $product_name ${major_version}.${minor_version}"
homebrew_version="$(brew --version | grep -o "\d\.\d\.\d")"
# To avoid/workaround decimal comparisons, we assume OSX 10.n and only compare minor versions
if [ "${minor_version}" -lt "${req_version}" ]; then
echo " *** Warning: $product_name ${major_version}.${minor_version} not supported for PowerShell package management via Homebrew Cask ***"
fi
echo ''
sleep 2
if [ $UNINSTALL = "TRUE" ]; then
echo "UNINSTALL: $UNINSTALL"
echo 'Delete known PowerShell directories'
sudo rm -rf /usr/local/microsoft /Applications/PowerShell.app
sleep 1
sudo rm -f /usr/local/bin/pwsh /usr/local/share/man/man1/pwsh.1.gz
sleep 1
sudo pkgutil --forget com.microsoft.powershell
sleep 1
sudo rm -rf /usr/local/microsoft
sleep 1
sudo rm -f /usr/local/bin/pwsh
else
echo "UNINSTALL: $UNINSTALL"
fi
if [ $homebrew_version ]; then
echo 'Try to re-install PowerShell via Homebrew'
echo 'brew update'
brew update
echo ''
echo 'brew tap caskroom/cask'
brew tap caskroom/cask
echo ''
echo 'brew cask install powershell'
brew cask install powershell
echo ''
else
Installation via Binary Archive
echo 'Homebrew not found. Proceeding wtih Installation via Binary Archive method'
echo ''
Download the powershell '.tar.gz' archive
temp_file="/tmp/powershell.tar.gz"
if [ -f "$temp_file" ]
then
echo 'powershell.tar.gz downloaded.'
else
echo "Downloading powershell.tar.gz from ${PWSHMACURL}."
curl -L -o /tmp/powershell.tar.gz $PWSHMACURL
fi
Create the target folder where powershell will be placed
target_dir="/usr/local/microsoft/powershell/$PWSHMACVER"
if ! [ -d "$target_dir" ]; then
echo "Confirmed folder ${target_dir} exists."
else
echo "Creating PowerShell folder ${target_dir}."
sudo mkdir -p $target_dir
fi
Expand powershell to the target folder
echo 'Expand powershell archive.'
sudo tar zxf /tmp/powershell.tar.gz -C $target_dir
echo ''
Confirm the powershell archive expanded to the target folder
target_file="$target_dir/pwsh"
if [ -f "$target_file" ]; then
echo "Confirmed ${target_file} exists."
Set execute permissions
echo 'Set execute permissions'
sudo chmod +x "${target_file}"
echo ''
Create the symbolic link that points to pwsh
echo 'Creating symbolic link for pwsh.'
sudo ln -s $target_file /usr/local/bin/pwsh
else
echo "*** WARNING! Something didn't go as expected with expanding the archive. ***"
fi
fi
echo '*** Done! : Now, run $ pwsh to confirm Powershell is installed. ***'
if [ "${minor_version}" -ge "${req_version}" ]; then
echo 'To update PowerShell via Homebrew, run:'
echo ' brew update'
echo ' brew cask upgrade powershell'
echo ''
fi