如何在Linux中安装Windows PowerShell Core 6.0
PowerShell是一种分布式,可扩展,异构的配置和自动框架,由交互式命令行shell和脚本语言组成,适用于Windows操作系统。
它是基于.NET Framework构建的,它允许用户自动执行和简化系统任务。
有关PowerShell的更多详细信息,请参阅以下链接。
在这篇简短的教程中,让我们了解如何在Ubuntu 14.04/16.04/18.04 LTS和CentOS 7 64位服务器版本中安装PowerShell。
在Linux中安装Windows PowerShell Core 6.0
PowerShell可以安装在许多流行的Linux发行版,包括Arch Linux,Debian,Ubuntu,Fedora,CentOS,Suse。
其中我已经包括Debian,Ubuntu和CentOS的安装说明。
在Ubuntu 14.04 LTS上:
下载和注册PowerShell存储库GPG密钥:
$wget -q https://packages.microsoft.com/config/ubuntu/14.04/packages-microsoft-prod.deb
$sudo dpkg -i packages-microsoft-prod.deb
更新软件源列表:
$sudo apt-get update
然后,使用命令安装PowerShell:
$sudo apt-get install -y powershell
在Ubuntu 16.04 LTS上:
添加PowerShell存储库GPG密钥:
$wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb
$sudo dpkg -i packages-microsoft-prod.deb
更新软件源列表:
$sudo apt-get update
然后,使用命令安装PowerShell:
$sudo apt-get install -y powershell
在Ubuntu 18.04 LTS上:
注册PowerShell存储库GPG密钥:
$wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
$sudo dpkg -i packages-microsoft-prod.deb
更新存储库列表并安装PowerShell:
$sudo apt-get update
$sudo apt-get install -y powershell
在Debian 8:
$sudo apt-get install curl 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-jessie-prod jessie main" > /etc/apt/sources.list.d/microsoft.list'
$sudo apt-get update
$sudo apt-get install -y powershell
在Debian 9:
$sudo apt-get install 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-stretch-prod stretch main" > /etc/apt/sources.list.d/microsoft.list'
$sudo apt-get update
$sudo apt-get install -y powershell
在CentOS 7:
将PowerShell存储库添加为root用户:
# curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
安装PowerShell:
# yum install -y powershell
对于其他分布,请参阅官方安装说明。
我们现在已经安装了PowerShell。
接下来,我们将看到如何实时使用它。
Powershell入门
请注意,PowerShell for Linux仍在开发阶段,因此我们遇到了一些错误。
如果有任何错误,请加入PowerShell社区教程 (在本文末尾提供该链接)并获得帮助。
安装PowerShell后,运行以下命令以输入PowerShell控制台/会话。
pwsh
这就是PowerShell控制台在我的Ubuntu 18.04 LTS服务器中的样子。
PowerShell 6.1.2 Copyright (c) Microsoft Corporation. All rights reserved. https://aka.ms/pscore6-docs Type 'help' to get help. PS /home/sk>
在PowerShell会话中,我们提到PowerShell命令作为cmdlet,我们提到PowerShell提示符标志为PS 。
在PowerShell工作几乎类似于Bash。
我在PowerShell中运行了一些Linux命令。
似乎几乎所有Linux命令都在PowerShell中使用。
此外,PowerShell有自己的一组命令(cmdlet)。
Tab函数(自动完成)功能与Bash一起工作。
清除?
好吧,让我们少数例子。
查看PowerShell版本
要查看PowerShell的版本,请输入:
$PSVersionTable
示例输出:
Name Value ---- ---- PSVersion 6.1.2 PSEdition Core GitCommitId 6.1.2 OS Linux 4.15.0-45-generic #48-Ubuntu SMP Tue Jan ... Platform Unix PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 WSManStackVersion 3.0
如上所述,PowerShell的版本为6.1.2.
创建文件
要创建一个新文件,请使用"new-item"命令,如下所示。
New-Item theitroad.txt
示例输出:
Directory: /home/sk Mode LastWriteTime Length Name ---- ------------- ------ --- ------ 2/11/19 10:28 AM 0 theitroad.txt
或者只是使用">"如下图所示:
"" > theitroad.txt
其中""描述文件是空的。
onitad.txt是文件名。
要在文件中添加一些内容,请运行以下命令:
Set-Content theitroad.txt -Value "Welcome to theitroad blog!"
或者
"Welcome to theitroad blog!" > theitroad.txt
查看文件的内容
我们已从PowerShell创建了一些文件。
我们如何查看该文件的内容?
这很容易。
只需使用"get-content"命令以显示任何文件的内容。
Get-Content <Path> <filename>
例子:
Get-Content theitroad.txt
示例输出:
Welcome to theitroad blog!
删除文件
要删除文件或者项目,请使用"删除项"命令,如下所示。
Remove-Item theitroad.txt
让我们验证该项目是否真的使用命令删除:
Get-Content theitroad.txt
我们应该看到如下所示的输出。
Get-Content : Cannot find path '/home/sk/theitroad.txt' because it does not exist. At line:1 char:1 + Get-Content theitroad.txt + ~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (/home/sk/theitroad.txt:String) [Get-Content], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
或者我们可以只需使用"LS"命令查看文件是否存在或者不存在。
查看运行进程
要查看正在运行的进程列表,请运行:
Get-Process
示例输出:
NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName ------ ----- ----- ------ -- -- ----------- 0 0.00 0.00 0.02 599 599 agetty 0 0.00 0.00 0.00 2385 385 anacron 0 0.00 0.00 0.00 257 0 ata_sff 0 0.00 0.00 0.07 556 556 auditd 0 0.00 0.00 0.03 578 578 avahi-daemon 0 0.00 0.00 0.00 590 578 avahi-daemon 0 0.00 0.00 0.05 2327 327 bash 0 0.00 0.00 0.00 19 0 bioset 0 0.00 0.00 0.00 352 0 bioset 0 0.00 0.00 0.00 360 0 bioset 0 0.00 0.00 0.35 597 597 crond 0 0.00 0.00 0.00 31 0 crypto 0 0.00 0.00 0.11 586 586 dbus-daemon 0 0.00 0.00 0.03 63 0 deferwq 0 0.00 0.01 0.93 585 585 firewalld 0 0.00 0.00 0.00 30 0 fsnotify_mark 0 0.00 0.00 0.00 43 0 ipv6_addrconf 0 0.00 0.00 0.02 94 0 kauditd 0 0.00 0.00 0.00 20 0 kblockd 0 0.00 0.00 0.00 14 0 kdevtmpfs 0 0.00 0.00 0.00 351 0 kdmflush 0 0.00 0.00 0.00 359 0 kdmflush 0 0.00 0.00 0.00 13 0 khelper 0 0.00 0.00 0.03 29 0 khugepaged 0 0.00 0.00 0.00 26 0 khungtaskd 0 0.00 0.00 0.00 18 0 kintegrityd 0 0.00 0.00 0.00 41 0 kmpath_rdacd 0 0.00 0.00 0.00 42 0 kpsmoused 0 0.00 0.00 0.00 28 0 ksmd 0 0.00 0.00 0.17 3 0 ksoftirqd/0 0 0.00 0.00 0.02 27 0 kswapd0 0 0.00 0.00 0.00 2 0 kthreadd 0 0.00 0.00 0.00 39 0 kthrotld 0 0.00 0.00 0.01 2313 0 kworker/0:0 0 0.00 0.00 0.04 2369 0 kworker/0:0H 0 0.00 0.00 0.00 2440 0 kworker/0:1 0 0.00 0.00 0.05 2312 0 kworker/0:2H 0 0.00 0.00 0.28 2376 0 kworker/0:3 0 0.00 0.00 0.25 6 0 kworker/u2:0 0 0.00 0.00 0.00 272 0 kworker/u2:2 0 0.00 0.00 0.01 473 473 lvmetad 0 0.00 0.00 0.02 2036 036 master 0 0.00 0.00 0.00 21 0 md 0 0.00 0.00 0.00 7 0 migration/0 0 0.00 0.00 0.00 15 0 netns 0 0.00 0.00 0.22 653 653 NetworkManager 0 0.00 0.00 0.00 16 0 perf 0 0.00 0.00 0.01 2071 036 pickup 0 0.00 0.00 0.05 799 799 polkitd 0 0.00 0.02 5.02 2401 327 powershell 0 0.00 0.00 0.00 2072 036 qmgr 0 0.00 0.00 0.00 8 0 rcu_bh 0 0.00 0.00 0.73 10 0 rcu_sched 0 0.00 0.00 0.00 9 0 rcuob/0 0 0.00 0.00 0.51 11 0 rcuos/0 0 0.00 0.00 0.06 582 582 rsyslogd 0 0.00 0.00 0.00 267 0 scsi_eh_0 0 0.00 0.00 0.00 271 0 scsi_eh_1 0 0.00 0.00 0.00 275 0 scsi_eh_2 0 0.00 0.00 0.00 269 0 scsi_tmf_0 0 0.00 0.00 0.00 273 0 scsi_tmf_1 0 0.00 0.00 0.00 277 0 scsi_tmf_2 0 0.00 0.00 0.03 1174 174 sshd 0 0.00 0.00 0.79 2322 322 sshd 0 0.00 0.00 1.68 1 1 systemd 0 0.00 0.00 0.24 453 453 systemd-journal 0 0.00 0.00 0.04 579 579 systemd-logind 0 0.00 0.00 0.19 481 481 systemd-udevd 0 0.00 0.00 0.54 1175 175 tuned 0 0.00 0.00 0.02 12 0 watchdog/0 0 0.00 0.00 0.01 798 798 wpa_supplicant 0 0.00 0.00 0.00 17 0 writeback 0 0.00 0.00 0.00 378 0 xfs_mru_cache 0 0.00 0.00 0.00 379 0 xfs-buf/dm-1 0 0.00 0.00 0.00 539 0 xfs-buf/sda1 0 0.00 0.00 0.00 382 0 xfs-cil/dm-1 0 0.00 0.00 0.00 542 0 xfs-cil/sda1 0 0.00 0.00 0.00 381 0 xfs-conv/dm-1 0 0.00 0.00 0.00 541 0 xfs-conv/sda1 0 0.00 0.00 0.00 380 0 xfs-data/dm-1 0 0.00 0.00 0.00 540 0 xfs-data/sda1 0 0.00 0.00 0.51 383 0 xfsaild/dm-1 0 0.00 0.00 0.00 543 0 xfsaild/sda1 0 0.00 0.00 0.00 377 0 xfsalloc
上面的命令将显示Linux系统中的运行进程的整个列表。
要查看任何特定的运行进程,请使用上面的命令使用"-Name"选项。
例如,要查看PowerShell进程,请运行:
Get-Process -Name pwsh
示例输出:
NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName ------ ----- ----- ------ -- -- ---------- 0 0.00 99.32 3.28 2575 398 pwsh
查看命令别名
你懒得输入整个命令吗?
只需输入几个单词并点击"标签"密钥,命令将自动完成或者建议的命令列表将显示,就像Linux Bash shell一样。
或者,有一些命令存在别名。
例如,要清除屏幕,我们将键入:清除主机。
或者我们可以简单地键入上面的命令'cls'或者'clear'的别名以清除屏幕。
要查看可用别名列表,请运行:
Get-Alias
以下是可用别名的完整列表:
CommandType Name Version Source ----------- ---- ------- ------ Alias ? -> Where-Object Alias % -> ForEach-Object Alias cd -> Set-Location Alias chdir -> Set-Location Alias clc -> Clear-Content Alias clear -> Clear-Host Alias clhy -> Clear-History Alias cli -> Clear-Item Alias clp -> Clear-ItemProperty Alias cls -> Clear-Host Alias clv -> Clear-Variable Alias cnsn -> Connect-PSSession Alias copy -> Copy-Item Alias cpi -> Copy-Item Alias cvpa -> Convert-Path Alias dbp -> Disable-PSBreakpoint Alias del -> Remove-Item Alias dir -> Get-ChildItem Alias dnsn -> Disconnect-PSSession Alias ebp -> Enable-PSBreakpoint Alias echo -> Write-Output Alias epal -> Export-Alias Alias epcsv -> Export-Csv Alias erase -> Remove-Item Alias etsn -> Enter-PSSession Alias exsn -> Exit-PSSession Alias fc -> Format-Custom Alias fhx -> Format-Hex 3.1.0.0 Microsoft.PowerShell.Utility Alias fl -> Format-List Alias foreach -> ForEach-Object Alias ft -> Format-Table Alias fw -> Format-Wide Alias gal -> Get-Alias Alias gbp -> Get-PSBreakpoint Alias gc -> Get-Content Alias gci -> Get-ChildItem Alias gcm -> Get-Command Alias gcs -> Get-PSCallStack Alias gdr -> Get-PSDrive Alias ghy -> Get-History Alias gi -> Get-Item Alias gin -> Get-ComputerInfo 3.1.0.0 Microsoft.PowerShell.Management Alias gjb -> Get-Job Alias gl -> Get-Location Alias gm -> Get-Member Alias gmo -> Get-Module Alias gp -> Get-ItemProperty Alias gps -> Get-Process Alias gpv -> Get-ItemPropertyValue Alias group -> Group-Object Alias gsn -> Get-PSSession Alias gsv -> Get-Service Alias gu -> Get-Unique Alias gv -> Get-Variable Alias h -> Get-History Alias history -> Get-History Alias icm -> Invoke-Command Alias iex -> Invoke-Expression Alias ihy -> Invoke-History Alias ii -> Invoke-Item Alias ipal -> Import-Alias Alias ipcsv -> Import-Csv Alias ipmo -> Import-Module Alias kill -> Stop-Process Alias md -> mkdir Alias measure -> Measure-Object Alias mi -> Move-Item Alias move -> Move-Item Alias mp -> Move-ItemProperty Alias nal -> New-Alias Alias ndr -> New-PSDrive Alias ni -> New-Item Alias nmo -> New-Module Alias nsn -> New-PSSession Alias nv -> New-Variable Alias oh -> Out-Host Alias popd -> Pop-Location Alias pushd -> Push-Location Alias pwd -> Get-Location Alias r -> Invoke-History Alias rbp -> Remove-PSBreakpoint Alias rcjb -> Receive-Job Alias rcsn -> Receive-PSSession Alias rd -> Remove-Item Alias rdr -> Remove-PSDrive Alias ren -> Rename-Item Alias ri -> Remove-Item Alias rjb -> Remove-Job Alias rmo -> Remove-Module Alias rni -> Rename-Item Alias rnp -> Rename-ItemProperty Alias rp -> Remove-ItemProperty Alias rsn -> Remove-PSSession Alias rv -> Remove-Variable Alias rvpa -> Resolve-Path Alias sajb -> Start-Job Alias sal -> Set-Alias Alias saps -> Start-Process Alias sasv -> Start-Service Alias sbp -> Set-PSBreakpoint Alias sc -> Set-Content Alias select -> Select-Object Alias set -> Set-Variable Alias si -> Set-Item Alias sl -> Set-Location Alias sls -> Select-String Alias sp -> Set-ItemProperty Alias spjb -> Stop-Job Alias spps -> Stop-Process Alias spsv -> Stop-Service Alias sv -> Set-Variable Alias type -> Get-Content Alias where -> Where-Object Alias wjb -> Wait-Job
要查看任何特定命令的别名,请键入:
Get-Alias cls
示例输出:
CommandType Name Version Source ----------- ---- ------- ------ Alias cls -> Clear-Host
查看可用命令的完整列表
要查看所有可用PowerShell命令的列表,请运行:
Get-Command
观看帮助
不知道特别是什么吗?
没问题。
只需输入"帮助"以获得帮助。
我们不必在互联网上搜索。
我们还可以使用"get-help"命令以及任何PowerShell命令。
它是与Linux中的"Man"命令类似的东西。
例如,要显示名为"Clear-Host"的命令的帮助部分,请运行:
Get-Help Clear-Host
示例输出:
NAME Clear-Host SYNOPSIS SYNTAX Clear-Host [<CommonParameters>] DESCRIPTION RELATED LINKS https://go.microsoft.com/fwlink/?LinkID=225747 REMARKS To see the examples, type: "get-help Clear-Host -examples". For more information, type: "get-help Clear-Host -detailed". For technical information, type: "get-help Clear-Host -full". For online help, type: "get-help Clear-Host -online
如上所述,"get-help"显示特定PowerShell命令的帮助部分,如命令,语法格式,别名和备注等名称。
要退出PowerShell控制台,只需输入:
exit