@@ -17,18 +17,89 @@ POWERSHELL_MODULES="${MODULES:-""}"
17
17
POWERSHELL_PROFILE_URL=" ${POWERSHELLPROFILEURL} "
18
18
19
19
MICROSOFT_GPG_KEYS_URI=" https://packages.microsoft.com/keys/microsoft.asc"
20
- POWERSHELL_ARCHIVE_ARCHITECTURES=" amd64"
20
+ # MICROSOFT_GPG_KEYS_URI=$(curl https://packages.microsoft.com/keys/microsoft.asc -o /usr/share/keyrings/microsoft-archive-keyring.gpg)
21
+ POWERSHELL_ARCHIVE_ARCHITECTURES_UBUNTU=" amd64"
22
+ POWERSHELL_ARCHIVE_ARCHITECTURES_ALMALINUX=" x86_64"
21
23
POWERSHELL_ARCHIVE_VERSION_CODENAMES=" stretch buster bionic focal bullseye jammy bookworm noble"
24
+
25
+ # These key servers are used to verify the authenticity of packages and repositories.
26
+ # keyservers for ubuntu and almalinux are different so we need to specify both
22
27
GPG_KEY_SERVERS=" keyserver hkp://keyserver.ubuntu.com
23
28
keyserver hkp://keyserver.ubuntu.com:80
24
29
keyserver hkps://keys.openpgp.org
25
- keyserver hkp://keyserver.pgp.com"
30
+ keyserver hkp://keyserver.pgp.com
31
+ keyserver hkp://keyserver.fedoraproject.org
32
+ keyserver hkps://keys.openpgp.org
33
+ keyserver hkp://pgp.mit.edu
34
+ keyserver hkp://keyserver.redhat.com"
35
+
26
36
27
37
if [ " $( id -u) " -ne 0 ]; then
28
38
echo -e ' Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
29
39
exit 1
30
40
fi
31
41
42
+ # Clean up package manager cache
43
+ clean_cache () {
44
+ if [ -d " /var/cache/apt" ]; then
45
+ apt-get clean
46
+ fi
47
+ if [ -d " /var/cache/dnf" ]; then
48
+ rm -rf /var/cache/dnf/*
49
+ fi
50
+ }
51
+ # Install dependencies for RHEL/CentOS/AlmaLinux (DNF-based systems)
52
+ install_using_dnf () {
53
+ dnf remove -y curl-minimal
54
+ dnf install -y curl gnupg2 ca-certificates dnf-plugins-core
55
+ dnf clean all
56
+ dnf makecache
57
+ curl --version
58
+ }
59
+
60
+ # Install PowerShell on RHEL/CentOS/AlmaLinux-based systems (DNF)
61
+ install_powershell_dnf () {
62
+ # Install wget, if not already installed
63
+ dnf install -y wget
64
+
65
+ # Download Microsoft GPG key
66
+ curl https://packages.microsoft.com/keys/microsoft.asc -o /usr/share/keyrings/microsoft-archive-keyring.gpg
67
+ ls -l /usr/share/keyrings/microsoft-archive-keyring.gpg
68
+
69
+ # Install necessary dependencies
70
+ dnf install -y krb5-libs libicu openssl-libs zlib
71
+
72
+ # Add Microsoft PowerShell repository
73
+ curl " https://packages.microsoft.com/config/rhel/9.0/prod.repo" > /etc/yum.repos.d/microsoft.repo
74
+
75
+ # Install PowerShell
76
+ dnf install --assumeyes powershell
77
+ }
78
+
79
+
80
+ # Detect the package manager and OS
81
+ detect_package_manager () {
82
+ if [ -f /etc/os-release ]; then
83
+ . /etc/os-release
84
+ if [[ " $ID " == " ubuntu" || " $ID " == " debian" ]]; then
85
+ echo " Detected Debian/Ubuntu-based system"
86
+ install_using_apt
87
+ install_pwsh
88
+ elif [[ " $ID " == " centos" || " $ID " == " rhel" || " $ID " == " almalinux" ]]; then
89
+ echo " Detected RHEL/CentOS/AlmaLinux-based system"
90
+ install_using_dnf
91
+ install_powershell_dnf
92
+ install_pwsh
93
+ else
94
+ echo " Unsupported Linux distribution: $ID "
95
+ exit 1
96
+ fi
97
+ else
98
+ echo " Could not detect OS"
99
+ exit 1
100
+ fi
101
+ }
102
+
32
103
# Figure out correct version of a three part version number is not passed
33
104
find_version_from_git_tags () {
34
105
local variable_name=$1
@@ -72,19 +143,43 @@ apt_get_update()
72
143
}
73
144
74
145
# Checks if packages are installed and installs them if not
75
- check_packages () {
76
- if ! dpkg -s " $@ " > /dev/null 2>&1 ; then
77
- apt_get_update
78
- apt-get -y install --no-install-recommends " $@ "
79
- fi
146
+ check_packages () {
147
+ if command -v dpkg > /dev/null 2>&1 ; then
148
+ # If dpkg exists, assume APT-based system (Debian/Ubuntu)
149
+ for package in " $@ " ; do
150
+ if ! dpkg -s " $package " > /dev/null 2>&1 ; then
151
+ echo " Package $package not installed. Installing using apt-get..."
152
+ apt-get update
153
+ apt-get install -y --no-install-recommends " $package "
154
+ else
155
+ echo " Package $package is already installed (APT)."
156
+ fi
157
+ done
158
+ elif command -v dnf > /dev/null 2>&1 ; then
159
+ for package in " $@ " ; do
160
+ if ! dnf list installed " $package " > /dev/null 2>&1 ; then
161
+ echo " Package $package not installed. Installing using dnf..."
162
+ dnf install -y " $package "
163
+ else
164
+ echo " Package $package is already installed (DNF)."
165
+ fi
166
+ done
167
+ else
168
+ echo " Unsupported package manager. Neither APT nor DNF found."
169
+ return 1
170
+ fi
171
+
172
+
80
173
}
81
174
82
175
install_using_apt () {
83
176
# Install dependencies
84
177
check_packages apt-transport-https curl ca-certificates gnupg2 dirmngr
85
178
# Import key safely (new 'signed-by' method rather than deprecated apt-key approach) and install
179
+
86
180
curl -sSL ${MICROSOFT_GPG_KEYS_URI} | gpg --dearmor > /usr/share/keyrings/microsoft-archive-keyring.gpg
87
181
echo " deb [arch=$( dpkg --print-architecture) signed-by=/usr/share/keyrings/microsoft-archive-keyring.gpg] https://packages.microsoft.com/repos/microsoft-${ID} -${VERSION_CODENAME} -prod ${VERSION_CODENAME} main" > /etc/apt/sources.list.d/microsoft.list
182
+
88
183
89
184
# Update lists
90
185
apt-get update -yq
@@ -201,7 +296,8 @@ install_using_github() {
201
296
if ! type git > /dev/null 2>&1 ; then
202
297
check_packages git
203
298
fi
204
- if [ " ${architecture} " = " amd64" ]; then
299
+
300
+ if [ " ${architecture} " = " amd64" ]; then
205
301
architecture=" x64"
206
302
fi
207
303
pwsh_url=" https://github.com/PowerShell/PowerShell"
@@ -210,9 +306,13 @@ install_using_github() {
210
306
if grep -q " Not Found" " ${powershell_filename} " ; then
211
307
install_prev_pwsh $pwsh_url
212
308
fi
309
+
310
+ # downlaod the latest version of powershell and extracting the file to powershell directory
311
+ wget https://github.com/PowerShell/PowerShell/releases/download/v${POWERSHELL_VERSION} /${powershell_filename}
312
+ mkdir ~ /powershell
313
+ tar -xvf powershell-${POWERSHELL_VERSION} -linux-x64.tar.gz -C ~ /powershell
314
+
213
315
214
- # Ugly - but only way to get sha256 is to parse release HTML. Remove newlines and tags, then look for filename followed by 64 hex characters.
215
- curl -sSL -o " release.html" " https://github.com/PowerShell/PowerShell/releases/tag/v${POWERSHELL_VERSION} "
216
316
powershell_archive_sha256=" $( cat release.html | tr ' \n' ' ' | sed ' s|<[^>]*>||g' | grep -oP " ${powershell_filename} \s+\K[0-9a-fA-F]{64}" || echo ' ' ) "
217
317
if [ -z " ${powershell_archive_sha256} " ]; then
218
318
echo " (!) WARNING: Failed to retrieve SHA256 for archive. Skipping validaiton."
@@ -233,14 +333,22 @@ if ! type pwsh >/dev/null 2>&1; then
233
333
234
334
# Source /etc/os-release to get OS info
235
335
. /etc/os-release
236
- architecture=" $( dpkg --print-architecture) "
336
+ architecture=" $( uname -m) "
337
+ if [[ " $ID " == " ubuntu" || " $ID " == " debian" ]]; then
338
+ POWERSHELL_ARCHIVE_ARCHITECTURES=" ${POWERSHELL_ARCHIVE_ARCHITECTURES_UBUNTU} "
339
+ elif [[ " $ID " == " centos" || " $ID " == " rhel" || " $ID " == " almalinux" ]]; then
340
+ POWERSHELL_ARCHIVE_ARCHITECTURES=" ${POWERSHELL_ARCHIVE_ARCHITECTURES_ALMALINUX} "
341
+ fi
237
342
238
- if [[ " ${POWERSHELL_ARCHIVE_ARCHITECTURES} " = * " ${architecture } " * ]] && [[ " ${POWERSHELL_ARCHIVE_VERSION_CODENAMES} " = * " ${VERSION_CODENAME} " * ]]; then
343
+ if [[ " ${POWERSHELL_ARCHIVE_ARCHITECTURES} " = * " ${POWERSHELL_ARCHIVE_ARCHITECTURES_UBUNTU } " * ]] && [[ " ${POWERSHELL_ARCHIVE_VERSION_CODENAMES} " = * " ${VERSION_CODENAME} " * ]]; then
239
344
install_using_apt || use_github=" true"
240
- else
241
- use_github=" true"
242
- fi
345
+ elif [[ " ${POWERSHELL_ARCHIVE_ARCHITECTURES} " = * " ${POWERSHELL_ARCHIVE_ARCHITECTURES_ALMALINUX} " * ]]; then
346
+ install_using_dnf && install_powershell_dnf || use_github=" true"
243
347
348
+ else
349
+ use_github=" true"
350
+ fi
351
+
244
352
if [ " ${use_github} " = " true" ]; then
245
353
echo " Attempting install from GitHub release..."
246
354
install_using_github
282
390
# Clean up
283
391
rm -rf /var/lib/apt/lists/*
284
392
285
- echo " Done!"
393
+ echo " Done!"
0 commit comments