@@ -17,18 +17,89 @@ POWERSHELL_MODULES="${MODULES:-""}"
1717POWERSHELL_PROFILE_URL=" ${POWERSHELLPROFILEURL} "
1818
1919MICROSOFT_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"
2123POWERSHELL_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
2227GPG_KEY_SERVERS=" keyserver hkp://keyserver.ubuntu.com
2328keyserver hkp://keyserver.ubuntu.com:80
2429keyserver 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+
2636
2737if [ " $( id -u) " -ne 0 ]; then
2838 echo -e ' Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
2939 exit 1
3040fi
3141
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+
32103# Figure out correct version of a three part version number is not passed
33104find_version_from_git_tags () {
34105 local variable_name=$1
@@ -72,19 +143,43 @@ apt_get_update()
72143}
73144
74145# 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+
80173}
81174
82175install_using_apt () {
83176 # Install dependencies
84177 check_packages apt-transport-https curl ca-certificates gnupg2 dirmngr
85178 # Import key safely (new 'signed-by' method rather than deprecated apt-key approach) and install
179+
86180 curl -sSL ${MICROSOFT_GPG_KEYS_URI} | gpg --dearmor > /usr/share/keyrings/microsoft-archive-keyring.gpg
87181 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+
88183
89184 # Update lists
90185 apt-get update -yq
@@ -201,7 +296,8 @@ install_using_github() {
201296 if ! type git > /dev/null 2>&1 ; then
202297 check_packages git
203298 fi
204- if [ " ${architecture} " = " amd64" ]; then
299+
300+ if [ " ${architecture} " = " amd64" ]; then
205301 architecture=" x64"
206302 fi
207303 pwsh_url=" https://github.com/PowerShell/PowerShell"
@@ -210,9 +306,13 @@ install_using_github() {
210306 if grep -q " Not Found" " ${powershell_filename} " ; then
211307 install_prev_pwsh $pwsh_url
212308 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+
213315
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} "
216316 powershell_archive_sha256=" $( cat release.html | tr ' \n' ' ' | sed ' s|<[^>]*>||g' | grep -oP " ${powershell_filename} \s+\K[0-9a-fA-F]{64}" || echo ' ' ) "
217317 if [ -z " ${powershell_archive_sha256} " ]; then
218318 echo " (!) WARNING: Failed to retrieve SHA256 for archive. Skipping validaiton."
@@ -233,14 +333,22 @@ if ! type pwsh >/dev/null 2>&1; then
233333
234334 # Source /etc/os-release to get OS info
235335 . /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
237342
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
239344 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"
243347
348+ else
349+ use_github=" true"
350+ fi
351+
244352 if [ " ${use_github} " = " true" ]; then
245353 echo " Attempting install from GitHub release..."
246354 install_using_github
282390# Clean up
283391rm -rf /var/lib/apt/lists/*
284392
285- echo " Done!"
393+ echo " Done!"
0 commit comments