在Linux中开始使用PowerShell[新手指南] | 您所在的位置:网站首页 › powershell帮助文档 › 在Linux中开始使用PowerShell[新手指南] |
在微软爱上Linux之后(后来被普遍称为“ Microsoft Loves Linux ”),PowerShell原本是 Windows 专用组件,于 2016 年 8 月 18 日开源跨平台,可在 Linux 上使用和苹果操作系统。 PowerShell是微软开发的任务自动化和配置管理系统。它由命令语言解释器 ( shell ) 和基于.NET Framework 的脚本语言组成。 它提供对COM(组件对象模型)和WMI(Windows Management Instrumentation )的完全访问,从而允许系统管理员在本地和远程 Windows 系统以及 WS-Management 和 CIM(通用信息模型)上执行管理任务,从而实现管理远程 Linux 系统加上网络设备。 在此框架下,管理任务基本上由称为cmdlet (发音为 command-lets)的特定.NET类执行。 与 Linux 中的 shell 脚本类似,用户可以按照特定规则将cmdlet组存储在文件中,从而构建脚本或可执行文件。这些脚本可以用作独立的命令行实用程序或工具。 在 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适用于 Debian 发行版的PowerShell已发布到包存储库,以便于安装和更新。 $ 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 存储库,以便于安装和更新。 ----------在 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 ----------在 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 ----------在 RHEL/CentOS7上---------- $ 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 [#显示当前日期] get-uptime [#显示服务器正常运行时间] get-location [#显示当前工作目录]1.使用以下两种方法创建一个新的空文件: new-item tecmint.tex OR “”>tecmint.tex然后向其中添加内容,查看文件内容。 set-content tecmint.tex -value "TecMint Linux How Tos Guides" get-content tecmint.tex2.在powershell中删除一个文件。 remove-item tecmint.tex get-content tecmint.tex3.创建一个新目录。 mkdir tecmint-files cd tecmint-files “”>domains.list ls4.执行长列表,显示文件/目录的详细信息,包括模式(文件类型)和最后修改时间。 dir5.查看系统上所有正在运行的进程: get-process6.要查看具有给定名称的单个/一组正在运行的进程的详细信息,请将进程名称作为参数提供给上一个命令,如下所示: get-process apache2上面输出中单位的含义: NPM(K) – 进程正在使用的非分页内存量,以千字节为单位。PM(K) – 进程正在使用的可分页内存量,以千字节为单位。WS(K) – 进程工作集的大小,以千字节为单位。工作集由进程最近引用的内存页面组成。CPU(s) – 进程在所有处理器上使用的处理器时间量,以秒为单位。ID——进程 ID (PID)。ProcessName – 进程的名称。7.要了解更多信息,请获取用于不同任务的所有 Powershell 命令的列表: get-command8.要了解如何使用命令,请查看其帮助页面(类似于 Unix/Linux 中的手册页);在此示例中,您可以获得Describe命令的帮助: get-help Describe9、查看所有可用的命令别名,键入: get-alias10.最后但同样重要的是,显示命令历史记录(您之前运行过的命令列表),如下所示: history就这样!目前,在本文中,我们向您展示了如何在 Linux 中安装Microsoft 的 Powershell。对我来说,Powershell 与传统的 Unix/Linux shell 相比还有很长的路要走,后者提供了更好、更令人兴奋和高效的功能,可以从命令行操作机器,重要的是,用于编程(脚本)目的以及。 |
CopyRight 2018-2019 实验室设备网 版权所有 |