在微軟愛上Linux(廣為人知的“微軟愛Linux”)之後,原本僅限於Windows的組件PowerShell於2016年8月18日開源並實現跨平台,可在Linux和Mac OS上使用。
PowerShell是由微軟開發的任務自動化和配置管理系統。它由一個命令行解釋器(shell)和基於.NET Framework的腳本語言組成。
它提供了完全訪問COM(組件對象模型)和WMI(Windows管理規範),從而允許系統管理員執行管理任務,無論是本地還是遠程的Windows系統,以及WS-Management和CIM(公共信息模型),實現對遠程Linux系統和網絡設備的管理。
在此框架下,管理任務基本上由特定的.NET類執行,這些類被稱為cmdlets(發音為命令-lets)。
類似於Linux中的shell腳本,用戶可以通過遵循特定規則將一組cmdlets存儲在文件中來構建腳本或可執行文件。這些腳本可以作為獨立的命令行實用程序或工具使用。
目錄
在Linux系統上安裝PowerShell
在Linux上安裝PowerShell,我們將使用官方的Microsoft儲存庫,這將允許我們通過最流行的Linux包管理工具進行安裝,例如apt-get或apt以及yum或dnf。
在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 Stream、Rocky和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]

在Powershell中處理文件和目錄
1. 使用以下兩種方法創建一個新的空文件:
new-item tecmint.tex OR “”>tecmint.tex
然後向文件添加內容並查看文件內容。
set-content tecmint.tex -value "TecMint Linux How Tos Guides" get-content tecmint.tex

2. 在PowerShell中刪除文件。
remove-item tecmint.tex get-content tecmint.tex

3. 創建一個新目錄。
mkdir tecmint-files cd tecmint-files “”>domains.list ls

4. 執行長列表,顯示文件/目錄的詳細信息,包括模式(文件類型)和最後修改時間。
dir

5. 查看系統上所有正在運行的進程:
get-process

6. 要查看具有特定名稱的單個/一組正在運行的進程的詳細信息,將進程名稱作為參數提供給上述命令,如下所示:
get-process apache2

輸出中單位的含義:
- NPM(K) – 進程正在使用的非頁面內存的量,以千字節為單位。
- PM(K) – 進程正在使用的可頁面內存的量,以千字節為單位。
- WS(K) – 進程的工作集大小,以千字節為單位。工作集由進程最近引用的內存頁組成。
- CPU(s) – 進程在所有處理器上使用的處理器時間量,以秒為單位。
- ID – 進程ID(PID)。
- ProcessName – 進程的名稱。
7. 要了解更多信息,獲取不同任務的所有PowerShell命令列表:
get-command

8. 要學習如何使用命令,查看其幫助頁面(類似於Unix/Linux中的man頁面);在此示例中,您可以獲取Describe命令的幫助:
get-help Describe

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

10. 最后但并非最不重要的,显示命令历史记录(之前运行过的命令列表),如下所示:
history

这就是全部内容!在本文中,我们向您展示了如何在Linux中安装Microsoft的Powershell。对我来说,与传统的Unix/Linux shell相比,Powershell仍有很长的路要走,Unix/Linux shell提供了更好、更令人兴奋和更具生产力的功能,可以从命令行操作机器,尤其是对于编程(脚本编写)目的来说。
访问Powershell Github存储库:https://github.com/PowerShell/PowerShell
不过,您可以尝试一下并在评论中与我们分享您的看法。
Source:
https://www.tecmint.com/install-powershell-in-linux/