在 Ubuntu 18.04 上安装 Microsoft Powershell

如果您是最近负责管理基于 Linux 的 Ubuntu 服务器的 Windows 管理员,您可能会发现使用 Microsoft Powershell 可能有助于轻松过渡到 Linux,并提高您的工作效率。 如果您是一位有兴趣探索 Powershell 提供的选项的 Linux 管理员,那么本教程也适合您。

什么是 Powershell?

Powershell 是最初由 Microsoft 为 Windows 操作系统开发的基于 .net 的配置管理和自动化工具。 2018 年 8 月,微软开源了这款软件,并凭借其跨平台能力,为其在 Linux 上的可用性铺平了道路。

Linux 上的 Powershell 允许您结合 Powershell 语言以及所有常用的 Linux 和 GNU shell 工具以及用于处理 cmdlet 的可靠框架

先决条件

在安装 Powershell 之前,您需要做一些事情。

  • 确保您以 root 用户身份访问服务器
  • 您还需要使用“wget”工具。 如果你需要安装这个工具。

安装

步骤 1:检索 Microsoft 存储库 GPG 密钥

GPG 是一种加密系统,允许用户安全地交换加密的电子邮件或其他信息。 GPG 的另一个特性是它能够“加密签名”文件。 当使用 GPG 以这种方式对文件进行签名时,作者可以分发该文件和相关的签名密钥。 然后,您可以使用 GPG 密钥来验证在下载文件之前没有人篡改文件中的数据。 在我们的案例中,此过程用于确保没有人使用恶意软件或其他恶意软件修改我们的 Powershell。 要检索密钥,我们将运行以下命令。

root@host [~]# cd /usr/src/
root@host:/usr/src# wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb

步骤 2:安装 Microsoft GPG 密钥

接下来,我们将使用 dpkg 命令手动安装在步骤 1 中下载的 Debian 软件包。

root@host:/usr/src# sudo dpkg -i packages-microsoft-prod.deb
Selecting previously unselected package packages-microsoft-prod.
(Reading database ... 257148 files and directories currently installed.)
Preparing to unpack packages-microsoft-prod.deb ...
Unpacking packages-microsoft-prod (1.0-ubuntu18.04.2) ...
Setting up packages-microsoft-prod (1.0-ubuntu18.04.2) ...
root@host:/usr/src#

第 3 步:更新 Apt 的软件包列表

现在,我们将更新我们的存储库列表,以确保我们拥有最新的可用软件包列表。

root@host:/usr/src# apt-get updateHit:1 https://us.archive.ubuntu.com/ubuntu eoan InRelease
Hit:2 https://archive.canonical.com/ubuntu eoan InRelease
Get:3 https://security.ubuntu.com/ubuntu eoan-security InRelease [97.5 kB]
Get:4 https://us.archive.ubuntu.com/ubuntu eoan-updates InRelease [97.5 kB]
Get:5 https://packages.microsoft.com/ubuntu/18.04/prod bionic InRelease [4,002 B]
Hit:6 https://archive.ubuntu.com/ubuntu eoan InRelease
Get:7 https://us.archive.ubuntu.com/ubuntu eoan-backports InRelease [88.8 kB]
...
...
...
Reading package lists... Done
root@host:/usr/src# 

第 4 步:启用“universe”存储库

可以从多个软件存储库安装 Ubuntu 软件。 要安装 Powershell,我们必须启用 Universe 存储库。

add-apt-repository universe

第 5 步:安装 Powershell

您现在已准备好运行命令来安装 Powershell。 ‘-y’ 标志只是简单地回答 apt-get 在安装过程中通常提出的所有问题。

root@host:/usr/src# apt-get install -y powershell
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  liblttng-ust-ctl4 liblttng-ust0 liburcu6
The following NEW packages will be installed:
  liblttng-ust-ctl4 liblttng-ust0 liburcu6 powershell
0 upgraded, 4 newly installed, 0 to remove and 42 not upgraded.
Need to get 57.7 MB of archives.
After this operation, 158 MB of additional disk space will be used.
root@host:/usr/src#

第 6 步:运行 Powershell

现在已安装 Powershell。 您可以使用“pwsh”命令从命令行运行它。

root@host:/usr/src# pwsh
PowerShell 6.2.4
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS /usr/src>

您会注意到命令提示符已更改。

root@host:/usr/src#

PS /usr/src>

输入“dir”命令为我们提供了以下输出。

PS /usr/src> dir


    Directory: /usr/src

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----            1/8/20  6:51 AM                linux-headers-5.3.0-26
d-----            1/8/20  6:51 AM                linux-headers-5.3.0-26-generic
d-----           1/29/20  6:57 AM                linux-headers-5.3.0-29
d-----           1/29/20  6:57 AM                linux-headers-5.3.0-29-generic
d-----          10/29/19  8:17 AM                nvidia-430.50
d-----           1/11/19  3:19 PM                packages
d-----            1/8/20 11:26 AM                virtualbox-6.0.14
------            1/2/19  6:49 PM           3132 packages-microsoft-prod.deb

PS /usr/src>

卸载 Powershell

如果您不再需要 Powershell 软件,可以使用以下命令将其卸载。

PS /usr/src> apt-get remove powershell
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  docutils-common liblttng-ust-ctl4 liblttng-ust0 liburcu6 mypy pylint python3-astroid python3-asttokens python3-bs4
  python3-docutils python3-html5lib python3-isort python3-jedi python3-lazy-object-proxy python3-logilab-common python3-lxml
  python3-mccabe python3-mypy python3-mypy-extensions python3-parso python3-psutil python3-pygments python3-pyperclip
  python3-roman python3-serial python3-soupsieve python3-typed-ast python3-typing-extensions python3-webencodings python3-wrapt
  sgml-base xml-core xsel
Use 'apt autoremove' to remove them.
The following packages will be REMOVED:
  powershell
0 upgraded, 0 newly installed, 1 to remove and 42 not upgraded.
After this operation, 157 MB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 257789 files and directories currently installed.)
Removing powershell (6.2.4-1.ubuntu.18.04) ...
Processing triggers for man-db (2.8.7-3) ...
PS /usr/src> exit
root@host:/usr/src#

使用 Snap 安装 Powershell

用于 Linux 的 PowerShell Core 也在 Snap 商店中发布,用于简单安装和持续更新

使用 Snap 安装

root@host:/usr/src# snap install powershell --classic
powershell 6.2.4 from Microsoft PowerShell✓ installed
root@host:/usr/src#
root@host:/usr/src# powershell
PowerShell 6.2.4
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS /usr/src> 
PS /usr/src> help

TOPIC
    PowerShell Help System

SHORT DESCRIPTION
    Displays help about PowerShell cmdlets and concepts. 

LONG DESCRIPTION
    PowerShell Help describes PowerShell cmdlets,
    functions, scripts, and modules, and explains concepts, including
    the elements of the PowerShell language.

    PowerShell does not include help files, but you can read the
    help topics online, or use the Update-Help cmdlet to download help files
    to your computer and then use the Get-Help cmdlet to display the help
    topics at the command line.

    You can also use the Update-Help cmdlet to download updated help files
    as they are released so that your local help content is never obsolete. 

    Without help files, Get-Help displays auto-generated help for cmdlets, 
    functions, and scripts.


  ONLINE HELP    
    You can find help for PowerShell online in the TechNet Library
    beginning at https://go.microsoft.com/fwlink/?LinkID=108518. 

<Type 'q' to exit.>

PS /usr/src> exit
root@host:/usr/src#

安装完成后,Snap 会自动升级。 您也可以使用以下命令提示手动升级。

PS /usr/src>snap refresh powershell 
snap "powershell" has no updates available
PS /usr/src>

安装完成后,Snap 会自动升级。 您也可以使用以下命令提示手动升级。

使用 Snap 卸载

要使用 snap 删除 Powershell,请运行此命令。

root@host:/usr/src# snap remove powershell

结论

总而言之,Powershell 是您现有工具集的宝贵辅助工具。 它允许我们通过命令行维护和操作计算机、访问数据存储以及轻松修改文件系统。 其成熟的脚本语言允许进一步扩展其整体功能。 因为它是开源软件,所以可以很容易地 fork on GitHub.

点击下方即可 Save 50.00 美元!!!

您想在下次购买、升级或每月付款时快速节省 50.00 美元吗? Liquid Web 的推荐朋友计划是赚取额外现金以购买您一直在寻找的物品、服务或额外物品的绝佳方式。 只需单击下面的优惠券链接以添加您的姓名和电子邮件地址即可! 你在!

要了解更多信息,请立即致电 800.580.4985,或打开 聊天 或与我们联系,与我们知识渊博的解决方案或经验丰富的托管顾问交谈,了解如何立即利用此计划!