如何在Linux发行版上安装PowerShell

时间:2020-03-05 15:28:07  来源:igfitidea点击:

PowerShell是Microsoft开发的任务自动化和配置管理系统。
它包括一个命令行shell和一个用于处理cmdlet的框架。
此外,它是一种非常强大的面向对象的脚本语言,具有基于.NET Framework的类和方法。

它最初是仅Windows的组件,后来开源并于2015年8月18日实现跨平台,使其可在Linux和Mac OS上使用。
在本文中,我将解释在我们所有最新流行的Linux发行版中安装Powershell的方法。

在CentOS 7.4上安装PowerShell

在安装之前,请确保更新用于编译和安装应用程序的所有软件要求。
我们可以同样运行'“ yum update”'。
有两种安装方法,为方便起见,可以选择两种方法之一。

1.通过直接RPM下载安装PowerShell

更新系统软件后,我们可以从发行版中下载适用于OS发行版的最新Powershell rpm,并通过发出以下命令进行安装:

#yum update.
#yum install -y wget
#wget https://github.com/PowerShell/PowerShell/releases/download/v6.0.0-alpha.15/powershell-6.0.0_alpha.15-1.el7.centos.x86_64.rpm
# yum install -y powershell-6.0.0_alpha.15-1.el7.centos.x86_64.rpm

2.使用Microsoft Official Repo进行安装

另一种方法是启用与OS发行版相对应的Microsoft Official Repo,然后从那里进行安装。
请在下面查看用于注册Microsoft Repo并从中进行安装的命令:

#curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
#yum install -y powershell

安装后,只需在提示符下发出此命令“ pwsh”即可运行Powershell。

[root@centos ~]# pwsh
PowerShell v6.0.0
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/pscore6-docs
Type 'help' to get help.
PS /root> $PSVersionTable
Name Value
---- ----
PSVersion 6.0.0
PSEdition Core
GitCommitId v6.0.0
OS Linux 3.10.0-693.11.6.el7.x86_64 #1 SMP Thu Jan 4 01:06:37 UTC 2016
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
PS /root> cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)

在CentOS 7.4上卸载PowerShell

要从服务器删除此Powershell软件包,可以执行““ yum remove””命令。

#yum remove powerhsell -y

在Ubuntu 17.10上安装PowerShell

如前所述,我们需要更新所有软件要求,以便首先编译和安装应用程序。
其中我们有两种安装方法。
到目前为止,此Ubuntu版本10.10尚未提供Powershell软件包发行版。
但是,此处提供了适用于Ubunu 17.04的最新可用存储库。

1.通过直接下载软件包进行安装

我们可以从Powershell发行版中将Debain软件包下载到Ubuntu服务器,并使用以下命令进行安装:

#apt-get update
#apt-get install wget
# wget https://github.com/PowerShell/PowerShell/releases/download/v6.0.0/powershell_6.0.0-1.ubuntu.17.04_amd64.deb
#dpkg -i powershell_6.0.0-1.ubuntu.17.04_amd64.deb
#apt-get install -f

2.通过Microsoft Official Repo安装

甚至可以从软件包存储库中完成Powershell Core的安装,以便于安装。
我们只需要导入公共存储库密钥并启用正式的Ubuntu Microsoft存储库即可。
请遵循以下命令:

#apt-get install curl
#curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add 
#curl https://packages.microsoft.com/config/ubuntu/17.04/prod.list | sudo tee /etc/apt/sources.list.d/microsoft.list
#apt-get update
#apt-get install -y powershell
After installation, you can run the powershell, by just issuing this command "pwsh" in the prompt.
root@ubuntu:~# pwsh
PowerShell v6.0.0
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/pscore6-docs
Type 'help' to get help.
PS /root> $PSVersionTable
Name Value
---- ----
PSVersion 6.0.0
PSEdition Core
GitCommitId v6.0.0
OS Linux 4.13.0-25-generic #29-Ubuntu SMP Mon Jan 8 21:14:41 UTC 2016
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
PS /root> cat /etc/issue
Ubuntu 17.10 \n \l

在Debian 9.3上安装PowerShell

在安装之前,请确保更新用于编译和安装应用程序的所有软件要求。
与上述情况类似,这里我们也有两种安装方法。

1.通过直接下载软件包进行安装

我们可以从Powershell发行版中将Debain软件包下载到Debian 9服务器上,并使用以下命令进行安装:

#apt-get update
#apt-get install wget
#wget https://github.com/PowerShell/PowerShell/releases/download/v6.0.0/powershell_6.0.0-1.debian.9_amd64.deb
#dpkg -i powershell_6.0.0-1.debian.9_amd64.deb
#apt-get install -f

2.通过Microsoft Official Repo安装

甚至可以从软件包存储库中完成Powershell的安装,以便于安装。
在安装之前,我们需要启用某些系统组件以使正式存储库顺利运行。
我们可以通过执行以下命令来安装这些组件:

#apt-get update
#apt-get install curl gnupg apt-transport-https

接下来,我们需要导入公共存储库密钥并启用Offical Debian Microsoft Repository。
请遵循以下命令:

#curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add 
#sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch main" > /etc/apt/sources.list.d/microsoft.list'
#apt-get update
#apt-get install -y powershell

安装后,只需在提示符下发出此命令“ pwsh”即可启动Powershell。

root@debian:~# pwsh
PowerShell v6.0.0
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/pscore6-docs
Type 'help' to get help.
PS /root>
PS /root> $PSVersionTable
Name Value
---- ----
PSVersion 6.0.0
PSEdition Core
GitCommitId v6.0.0
OS Linux 4.9.0-5-amd64 #1 SMP Debian 4.9.65-3+deb9u2 (2016-01-04)
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
PS /root> cat /etc/debian_version
9.3

在Ubuntu 17.10/Debian 9.3上卸载PowerShell

要从Ubuntu/Debian服务器中删除此Powershell软件包,我们只需执行“ apt remove”命令即可,如下所示:

# apt remove powershell

在Fedora 27上安装PowerShell

首先,我们可以更新安装此应用程序的所有软件要求。
与上述情况类似,在Fedora中也有两种安装Powershell的方法。

1.通过直接下载软件包进行安装

我们可以从Powershell版本将最新的RHEL软件包下载到Fedora服务器。

其中我下载了RPM软件包“ powershell-6.0.0-1.rhel.7.x86_64.rpm”,并使用以下命令进行了安装:

#dnf update
#dnf install compat-openssl10
#dnf install wget
#wget https://github.com/PowerShell/PowerShell/releases/download/v6.0.0/powershell-6.0.0-1.rhel.7.x86_64.rpm
#dnf install powershell-6.0.0-1.rhel.7.x86_64.rpm

2.通过Microsoft Official Repo安装

Powershell的安装也可以从软件包存储库中完成,以方便安装。
我们可以导入公共存储库密钥,并为此启用官方Fedora Microsoft存储库。
请遵循以下命令:

#dnf install curl
#rpm --import https://packages.microsoft.com/keys/microsoft.asc
#curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
#dnf update
#dnf install -y powershell

安装后,只需在提示符下发出此命令“ pwsh”即可运行Powershell。

[root@fedora ~]# pwsh
PowerShell v6.0.0
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/pscore6-docs
Type 'help' to get help.
PS /root> $PSVersionTable
Name Value
---- ----
PSVersion 6.0.0
PSEdition Core
GitCommitId v6.0.0
OS Linux 4.13.9-300.fc27.x86_64 #1 SMP Mon Oct 23 13:41:58 UTC 2016
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
PS /root> cat /etc/fedora-release
Fedora release 27 (Twenty Seven)

在Fedora 27上卸载PowerShell

要从Fedora服务器中删除此Powershell软件包,可以执行“ dnf remove”命令,如下所示:

#dnf remove powershell -y