当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 在 Linux 发行版上安装 PowerShell Core

作者:精品下载站 日期:2024-12-14 23:19:59 浏览:14 分类:玩电脑

在 Linux 发行版上安装 PowerShell Core


如今,PowerShell 是一种开放的跨平台 shell 和脚本语言,可以在 Windows、Linux、macOS 和其他平台上安装和使用。在本文中,我们将展示如何在流行的 Linux 发行版(CentOS、RHEL、Debian、Kali、Mint、Ubuntu)上安装 PowerShell Core。本文可能有助于熟悉 PowerShell 的 Windows 管理员将他们的经验带入 Linux。

  • Microsoft 官方存储库中提供了针对不同平台编译的 PowerShell 包的完整列表:https://github.com/PowerShell/PowerShell/releases

  • 您可以在此处找到有关不同 Linux 发行版和版本中 PowerShell 支持的更多信息:https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-linux?view=powershell-7.2

目前,几乎所有默认 Linux 存储库都缺少 PowerShell 软件包。因此,要在 Linux 中安装 PowerShell,您必须先下载 Microsoft 存储库包,导入密钥,然后使用 Linux 包管理器安装 PowerShell。

请注意,Linux 上只能安装 PowerShell Core(基于 .NET Core)。您无法在 Linux 中安装经典 Windows PowerShell 5.1。在撰写本文时,可用的最新 PowerShell Core 版本是 PowerShell Core 7.2 LTS。

如何在 Ubuntu 20.04 和 Linux Mint 20 中安装 PowerShell Core?

在 Ubuntu 或 Linux Mint 中安装 PowerShell 之前,您需要更新软件包列表:

sudo apt-get update -y

下载 Microsoft 存储库的 GPG 密钥:

wget -q https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb -O packages-microsoft-prod.deb

安装 Microsoft GPG 密钥:

sudo dpkg -i packages-microsoft-prod.deb

使用packages.microsoft.com 中的包更新包列表:

sudo apt-get update

安装 PowerShell:

sudo apt-get install powershell -y

[玩转系统] 在 Linux 发行版上安装 PowerShell Core

PowerShell 包将由 apt 包管理器自动更新:

sudo apt update

如果您的计算机上安装了以前的 PowerShell Core 版本,它将被删除。

您还可以手动下载并安装 PowerShell DEB 包:

wget -q https://github.com/PowerShell/PowerShell/releases/download/v7.2.0/powershell-lts_7.2.0-1.deb_amd64.deb
sudo dpkg -i powershell_7.2.0-1.deb_amd64.deb

解决依赖关系并完成安装:

sudo apt-get install -f

在 Debian 11 上安装 PowerShell Core

要在 Debian 11 Bullseye 中安装 PowerShell Core 7.0+,请首先下载 Microsoft 存储库的 GPG 密钥:

wget https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb

然后添加您下载的 GPG 密钥:

sudo dpkg -i packages-microsoft-prod.deb

更新包管理器中的应用程序列表:

sudo apt update

安装 PowerShell:

sudo apt install -y powershell

[玩转系统] 在 Linux 发行版上安装 PowerShell Core

当您使用以下命令运行软件包更新时,PowerShell 将自动更新:

sudo apt update

如果您只想更新 PowerShell 包:

sudo apt install powershell

在 Red Hat Enterprise Linux (RHEL) 和 CentOS 上安装 PowerShell 很热吗?

Linux CentOS 和 Red Hat Enterprise Linux (RHEL) 中的 PowerShell、Oracle Linux 安装方式相同:

将 Microsoft RedHat 存储库添加到 YUM:

curl https://packages.microsoft.com/config/rhel/8/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo

使用 yum(或 dnf)包管理器安装最新的可用 PowerShell Core 版本:

sudo yum install -y powershell

[玩转系统] 在 Linux 发行版上安装 PowerShell Core

您可以使用以下命令更新 PoSh 包:

sudo yum update powershell

此外,您还可以从 RPM 文件安装 PowerShell:

sudo yum install https://github.com/PowerShell/PowerShell/releases/download/v7.2.0/powershell-lts-7.2.0-1.rh.x86_64.rpm

Kali Linux 中的 PowerShell Core 7.x 安装

要在 Kali 中安装 PowerShell,只需运行以下命令即可:

apt update && apt -y install powershell

使用 Snap 在 Linux 上安装 PowerShell

您可以使用snap在不同的Linux发行版中安装PowerShell。对于没有官方 PoSh 支持的 Linux 发行版,建议使用此安装方法。

使用snap安装PowerShell的命令是:

sudo snap install powershell --classic

在 snap 中,PowerShell 会自动更新,但您也可以手动更新:

sudo snap refresh powershell

如何在 Linux 中删除 PowerShell?

我们来学习一下不同Linux发行版中删除PowerShell的命令:

  • 要在 Ubuntu/Linux Mint 中删除 PowerShell Core:

    sudo apt remove powershell
  • 要在 Debian 中删除 PowerShell:

    sudo apt remove powershell
  • 要在 Kali Linux 中删除 PowerShell:

    sudo apt -y remove powershell
  • 要在 CentOS 或 RHEL 中删除 PowerShell:

    sudo yum remove powershell
  • 要使用 Snap 删除 PowerShell:

    sudo snap remove powershell

如何在Linux上运行和使用PowerShell Core?

要在 Linux 上运行 PowerShell 命令 shell,请运行以下命令:

pwsh

您可以使用以下命令检查 Ubuntu 主机上安装的 PowerShell 版本:

$PSVersionTable

键入 PowerShell 命令时,可以通过 TAB 键使用代码自动完成功能。

在我们的示例中,安装了 PowerShell Core 7.2.0:

PSVersion 7.2.0
PSEdition Core
OS Linux 5.4.0-81-generic #91-Ubuntu SMP Thu Jul 15 19:09:17 UTC 2021
Platform Unix

[玩转系统] 在 Linux 发行版上安装 PowerShell Core

您可以看到,PowerShell shell 中的命令需要比 bash 中的命令多几倍的时间才能完成。您可以比较命令执行时间:

time pwsh -Command Get-History
time bash -c history

[玩转系统] 在 Linux 发行版上安装 PowerShell Core

默认情况下,一些内置的 PowerShell 模块在 Linux 上可用:

Get-Module -ListAvailable
  • Microsoft.PowerShell.Archive
  • Microsoft.PowerShell.Host
  • Microsoft.PowerShell.Management
  • Microsoft.PowerShell.Security
  • Microsoft.PowerShell.Utility
  • PackageManagement
  • PowerShellGet
  • PSReadLine
  • ThreadJob

[玩转系统] 在 Linux 发行版上安装 PowerShell Core

您可以安装其他模块,包括 VMWare PowerCLI。

要获取当前日期:

Get-date

检查主机正常运行时间:

get-Uptime

列出当前目录的内容:

Dir

或者 :

Get-ChildItem

[玩转系统] 在 Linux 发行版上安装 PowerShell Core

请注意,Linux 上的 PowerShell 命令不区分大小写。

要显示 PowerShell 命令历史记录:

History

要获取有关该命令的帮助:

Get-help Get-History

要从 bash 运行 PowerShell 脚本:

pwsh /home/sysops/CheckSpace.ps1

我们将在下一篇文章中详细介绍使用 PowerShell 进行 Linux 管理和自动化的功能和场景。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯