@@ -50,6 +50,38 @@ clean_cache() {
5050 rm -rf /var/cache/dnf/*
5151 fi
5252}
53+ # Function to resolve PowerShell version from Microsoft redirect URLs
54+ resolve_powershell_version () {
55+ local version_tag=" $1 "
56+ local redirect_url=" https://aka.ms/powershell-release?tag=${version_tag} "
57+
58+ # Follow the redirect and extract the version from the final URL
59+ local resolved_url
60+ resolved_url=$( curl -sSL -o /dev/null -w ' %{url_effective}' " ${redirect_url} " )
61+
62+ # Extract version from URL (e.g., https://github.com/PowerShell/PowerShell/releases/tag/v7.4.7 -> 7.4.7)
63+ local resolved_version
64+ resolved_version=$( echo " ${resolved_url} " | grep -oP ' v\K[0-9]+\.[0-9]+\.[0-9]+(-\w+\.\d+)?' || echo " " )
65+
66+ if [ -z " ${resolved_version} " ]; then
67+ # Fallback: fetch version from PowerShell metadata.json via GitHub
68+ local metadata_url=" ${GITHUB_RELEASE_MIRROR:- https:// raw.githubusercontent.com} /PowerShell/PowerShell/master/tools/metadata.json"
69+ local metadata
70+ metadata=$( curl -sSL " ${metadata_url} " 2> /dev/null || echo " " )
71+ case " ${version_tag} " in
72+ lts) resolved_version=$( echo " ${metadata} " | grep -oP ' "LtsReleaseTag":\s*"v\K[^"]+' ) ;;
73+ preview) resolved_version=$( echo " ${metadata} " | grep -oP ' "PreviewReleaseTag":\s*"v\K[^"]+' ) ;;
74+ * ) resolved_version=$( echo " ${metadata} " | grep -oP ' "StableReleaseTag":\s*"v\K[^"]+' ) ;;
75+ esac
76+ fi
77+
78+ if [ -z " ${resolved_version} " ]; then
79+ echo " Failed to resolve version for tag: ${version_tag} " >&2
80+ return 1
81+ fi
82+
83+ echo " ${resolved_version} "
84+ }
5385# Install dependencies for RHEL/CentOS/AlmaLinux (DNF-based systems)
5486install_using_dnf () {
5587 dnf remove -y curl-minimal
@@ -412,7 +444,18 @@ install_using_github() {
412444
413445if ! type pwsh > /dev/null 2>&1 ; then
414446 export DEBIAN_FRONTEND=noninteractive
415-
447+ if [ " ${POWERSHELL_VERSION} " = " lts" ] || [ " ${POWERSHELL_VERSION} " = " stable" ] || [ " ${POWERSHELL_VERSION} " = " preview" ]; then
448+ echo " Resolving PowerShell '${POWERSHELL_VERSION} ' version from Microsoft..."
449+ resolved_version=$( resolve_powershell_version " ${POWERSHELL_VERSION} " )
450+ if [ -n " ${resolved_version} " ]; then
451+ echo " Resolved '${POWERSHELL_VERSION} ' to version: ${resolved_version} "
452+ POWERSHELL_VERSION=" ${resolved_version} "
453+ else
454+ echo " Warning: Could not resolve '${POWERSHELL_VERSION} ' version. Falling back to 'latest'."
455+ POWERSHELL_VERSION=" latest"
456+ fi
457+ fi
458+
416459 # Source /etc/os-release to get OS info
417460 . /etc/os-release
418461 architecture=" $( uname -m) "
0 commit comments