在Linux中开始使用PowerShell【初学者指南】

在微软爱上Linux(俗称“微软爱Linux”)之后,原本仅限于Windows的组件PowerShell于2016年8月18日开源并实现了跨平台,可在Linux和Mac OS上使用。

PowerShell是微软开发的一款任务自动化和配置管理系统。它由一个命令语言解释器(shell)和基于.NET Framework的脚本语言组成。

它提供对COM组件对象模型)和WMIWindows管理规范)的完全访问,从而允许系统管理员执行本地和远程Windows系统的管理任务,以及通过WS-Management和CIM(公共信息模型)实现对远程Linux系统和网络设备的管理。

在此框架下,管理任务基本上是通过特定的.NET类,称为cmdlets(发音为命令-lets)来执行的。

类似于Linux中的shell脚本,用户可以通过遵循特定规则将一组cmdlets存储在文件中来构建脚本或可执行文件。这些脚本可以作为独立的命令行实用程序或工具使用。

在Linux系统上安装PowerShell

要在Linux中安装PowerShell,我们将使用官方的Microsoft存储库,这将允许我们通过最流行的Linux包管理工具,如apt-getapt以及yumdnf进行安装。

在Ubuntu上安装PowerShell

首先导入公共存储库GPG密钥,然后在APT包源列表中注册Microsoft Ubuntu存储库以安装Powershell

$ sudo apt-get update
$ sudo apt-get install -y wget apt-transport-https software-properties-common
$ wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb"
$ sudo dpkg -i packages-microsoft-prod.deb
$ sudo apt-get update
$ sudo apt-get install -y powershell

在Debian 11上安装PowerShell

PowerShell对于Debian发行版,已发布到包存储库,以便于安装和更新。

$ sudo apt update
$ sudo apt install -y curl gnupg apt-transport-https
$ 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-bullseye-prod bullseye main" > /etc/apt/sources.list.d/microsoft.list'
$ sudo apt update
$ sudo apt install -y powershell

在Debian 10上安装PowerShell

$ wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb
$ sudo dpkg -i packages-microsoft-prod.deb
$ sudo apt-get update
$ sudo apt-get install -y powershell

在RHEL系统上安装PowerShell

适用于基于RHEL的发行版,如CentOS StreamRocky,和AlmaLinux的PowerShell已发布到官方的Microsoft存储库,以便于安装和更新。

---------- On RHEL, CentOS, Rocky & AlmaLinux 9 ---------- 
$ curl https://packages.microsoft.com/config/rhel/9.0/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
$ sudo dnf install --assumeyes powershell

---------- On RHEL, CentOS, Rocky & AlmaLinux 8 ----------
$ curl https://packages.microsoft.com/config/rhel/8/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
$ sudo dnf install --assumeyes powershell

---------- On RHEL/CentOS 7 ----------
$ curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
$ sudo dnf install --assumeyes powershell

如何在Linux中使用Powershell

在本节中,我们将简要介绍Powershell;我们将看到如何启动powershell,运行一些基本命令,并查看如何处理文件、目录和进程。然后深入探讨如何列出所有可用命令,以及显示命令帮助和别名。

要启动Powershell,请键入:

$ pwsh

PowerShell 7.3.3
PS /root> 

您可以使用以下命令检查Powershell版本:

PS /root> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.3
PSEdition                      Core
GitCommitId                    7.3.3
OS                             Linux 5.10.0-9-amd64 #1 SMP Debian 5.10.70-1 (2021-09-30)
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

在Linux上运行一些基本的Powershell命令。

get-date          [# Display current date]
get-uptime        [# Display server uptime]
get-location      [# Display present working directory]
Running PowerShell Commands

在Powershell中处理文件和目录

1.使用以下两种方法创建一个新的空文件:

new-item  tecmint.tex
OR
“”>tecmint.tex

然后向其中添加内容并查看文件内容。

set-content tecmint.tex -value "TecMint Linux How Tos Guides"
get-content tecmint.tex
Create New File in Powershell

2. 在PowerShell中删除一个文件。

remove-item tecmint.tex
get-content tecmint.tex
Delete File in Powershell

3. 创建一个新目录。

mkdir  tecmint-files
cd  tecmint-files
“”>domains.list
ls
Create a Directory in Powershell

4. 执行长列表操作,显示文件/目录的详细信息,包括模式(文件类型)和最后修改时间。

dir
Directory Long Listing in Powershell

5. 查看系统上所有正在运行的进程:

get-process
View Running Processes in Powershell

6. 要查看具有给定名称的单个/一组正在运行的进程的详细信息,请将进程名称作为参数提供给上述命令,如下所示:

get-process apache2
View Specific Process in Powershell

输出中各单位的含义:

  • NPM(K) – 进程正在使用的非分页内存量,以千字节为单位。
  • PM(K) – 进程正在使用的可分页内存量,以千字节为单位。
  • WS(K) – 进程的工作集大小,以千字节为单位。工作集由进程最近引用的内存页组成。
  • CPU(s) – 进程在所有处理器上使用的处理器时间量,以秒为单位。
  • ID – 进程ID(PID)。
  • ProcessName – 进程的名称。

7. 要了解更多信息,获取不同任务的所有PowerShell命令列表:

get-command
List Powershell Commands

8. 要学习如何使用命令,查看其帮助页面(类似于Unix/Linux中的man页面);在本例中,您可以获取有关Describe命令的帮助:

get-help Describe
Powershell Help Manual

9. 查看所有可用的命令别名,请输入:

get-alias
List Powershell Command Aliases

10. 最后但同样重要的是,显示命令历史(您之前运行过的命令列表),如下所示:

history
List Powershell Commands History

就是这样!在本文中,我们向您展示了如何在Linux上安装Microsoft的Powershell。对我来说,与传统的Unix/Linux shell相比,Powershell还有很长的路要走,后者提供了更好、更令人兴奋和更高效的功能,用于从命令行操作机器,并且重要的是,也用于编程(脚本编写)目的。

访问Powershell Github仓库:https://github.com/PowerShell/PowerShell

不过,您可以尝试一下,并在评论中与我们分享您的观点。

Source:
https://www.tecmint.com/install-powershell-in-linux/