PowerShell für Linux

Siehe auch

PowerShell von Microsoft ist tatsächlich plattformunabhängig und ein Automatisierungs- und Konfigurationstool/Framework, welches für den Umgang mit strukturierten Daten (z. B. JSON, CSV, XML usw.), REST-APIs und Objektmodellen optimiert ist. Es kommt mit einer Befehlszeilen-Shell, einer Skriptsprache und einem Framework für die Verarbeitung von Cmdlets. Lauffähig auf RHEL 7+.

Links

Installation

Dieses Script zieht die Repo-Datei von Microsoft und installiert PowerShell:

###################################
# Prerequisites

# Get version of RHEL
source /etc/os-release
if [ $(bc<<<"$VERSION_ID < 8") = 1 ]
then majorver=7
elif [ $(bc<<<"$VERSION_ID < 9") = 1 ]
then majorver=8
else majorver=9
fi

# Register the Microsoft RedHat repository
curl -sSL -O https://packages.microsoft.com/config/rhel/$majorver/packages-microsoft-prod.rpm

# Register the Microsoft repository keys
sudo rpm --install packages-microsoft-prod.rpm

# Delete the repository keys after installing
rm packages-microsoft-prod.rpm

# RHEL 7.x uses yum and RHEL 8+ uses dnf
if [ $(bc<<<"$majorver < 8") ]
then
    # Update package index files
    sudo yum update
    # Install PowerShell
    sudo yum -y install powershell
else
    # Update package index files
    sudo dnf update
    # Install PowerShell
    sudo dnf -y install powershell
fi

Auf RHEL 7 liefert die Ausführung von /bin/pwsh eventuell den Fehler:

/bin/pwsh: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /bin/pwsh)
/bin/pwsh: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /bin/pwsh)

Hier ist die vorinstallierte libstdc++.so.6 zu alt und enthält die geforderten Symbole nicht (was sich mit strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX_3 überprüfen lässt). Die Lösung besteht darin, eine libstdc++.so.6.0.25 nachzuinstallieren:

yum install yum-utils

# needs 1 GB disk space
yumdownloader microsoft-mlserver-python-9.4.7

# takes some time
rpm2cpio microsoft-mlserver-python-9.4.7.rpm | cpio --extract --make-directories --preserve-modification-time --verbose ./opt/microsoft/mlserver/9.4.7/runtime/python/lib/libstdc++.so.6.0.25
cp opt/microsoft/mlserver/9.4.7/runtime/python/lib/libstdc++.so.6.0.25 /usr/lib64/
unlink /usr/lib64/libstdc++.so.6
ln -s /usr/lib64/libstdc++.so.6.0.25 /usr/lib64/libstdc++.so.6

Verwendung

Start:

pwsh
PowerShell 7.3.9

PS /root> Write-Output 'Hello, World!'
Hello, World!

PS /root> $wo = 'Hello, World!' | Write-Output

PS /root> Get-Variable wo

Name                           Value
----                           -----
wo                             Hello, World!

PS /root>

PowerShell mit Skript in Datei:

pwsh myscript.ps1

Built on 2024-04-18