PowerShell

任务自动化和组态管理框架

PowerShell(包括Windows PowerShellPowerShell Core)是微软公司开发的任务自动化和配置管理架构,由在.NET Framework和后来的.NET上构建的命令行界面壳层相关脚本语言组成,最初仅仅是Windows组件,后于2016年8月18日开源并提供跨平台支持。[4]

PowerShell
Windows Terminal内运行的PowerShell 7
编程范型命令式管道面向对象函数式反射式
设计者杰弗里·史诺威英语Jeffrey Snover(Jeffrey Snover)
布鲁斯·帕耶特(Bruce Payette)
詹姆斯·特鲁赫(James Truher) (et al.)
实现者Microsoft
发行时间2006年11月14日,​17年前​(2006-11-14
当前版本
  • 7.4.1 (2024年1月11日;稳定版本)[1]
编辑维基数据链接
类型系统强类型类型安全类型推论动态类型
系统平台PowerShell: .NET
Windows PowerShell: .NET框架
操作系统Windows 7及其后续版本、LinuxmacOS
许可证MIT许可证[2](但Windows组件仍然是专有的
文件扩展名
  • .ps1 (脚本文件)
  • .ps1xml(XML文档)
  • .psc1(控制台文件)
  • .psd1(数据文件)
  • .psm1(脚本模块)
  • .pssc(会话配置文件)
  • .cdxml (Cmdlet定义的XML文档)
网站microsoft.com/powershell 编辑维基数据链接
启发语言
KshPerlC#CL英语IBM i Control LanguageDCL英语DIGITAL Command LanguageSQLTclTk[3]ChefPuppet

在PowerShell中,管理任务通常由cmdlets(发音为command-lets)执行,这是执行特定操作的专用.NET。可以将cmdlet集合至脚本、可执行文件(一般是独立应用程序)中,或通过常规.NET类(或WMI / COM对象)实例化。[5][6]

源起与目的 编辑

UNIX系统一直有着功能强大的壳程序(shell),Windows PowerShell的诞生就是要提供功能相当于UNIX系统的命令行壳程序(例如:shbashcsh),同时也内置脚本语言以及辅助脚本程序的工具。

程序特性 编辑

  • 一致性的设计让所有工具和系统资料的使用语法、命名原则都相同。
  • 脚本语言简单易学,而且能支持现有的脚本程序和命令行工具。
  • 内含129种称为cmdlet的标准工具,可用来处理常见的系统管理工作。
  • 具备完整的扩展,独立软件商或开发者都能很容易的自行扩展。
  • 进程间数据传递内容具有强类型特征。
  • PowerShell Core可以自动检测字符编码,自动识别Linux和Windows下的换行符,跨平台运行。

cmdlet是Windows PowerShell的指令,发音念法为command-let。这相当于DOS或其他壳程序的内置指令,指令名称的格式都是以连字号(-)隔开的一对动词名词,并且通常都是单数名词;例如在线查询说明的cmdlet指令为get-help,名称的动词部分大致有get、set、add、remove等等(字母都不分大小写)。

Windows PowerShell ISE是Windows PowerShell的主机应用程序。在此程序中,可以在单个Windows GUI中运行命令、编辑与测试脚本。此程序具有多行编辑、Tab补齐、上下文相关帮助、语法着色、选择性执行等功能,而且还支持从右到左的书写顺序等功能。

技术基础 编辑

Windows PowerShell是以.NET Framework技术为基础,并且与现有的WSH保持回溯兼容,因此它的脚本程序不仅能访问.NET CLR,也能使用现有的COM技术。同时也包含了数种系统管理工具、简易且一致的语法,常见如登录数据库、WMI。Exchange Server 2007以及System Center Operations Manager 2007等服务器软件都将内置Windows PowerShell。

示例 编辑

  • 停止所有目前执行中的以"p"字符开头命名的程序:
get-process p* | stop-process
  • 停止所有目前执行中的所有使用大于1000MB存储器的程序:
get-process | where { $_.WS -gt 1000MB } | stop-process
  • 计算一个目录下文件内的字节大小:
get-childitem | measure-object -property length -sum
  • 将"hello, world!"字符串转为英文大写字符,成为"HELLO, WORLD!":
"hello, world!".ToUpper()
  • 订阅一个指定的RSS Feed并显示它最近8个主题:
$rssUrl = "http://blogs.msdn.com/powershell/rss.aspx"
$blog = [xml](new-object System.Net.WebClient).DownloadString($rssUrl)
$blog.rss.channel.item | select title -first 8

命令提示字符比较 编辑

PowerShell命令行与其他命令行解释器的内部和外部命令的比较
PowerShell(命令行) PowerShell(别名) 命令提示符 Unix shell 描述
Get-ChildItem gci, dir, ls dir ls 列出目前或指定文件夹中的所有文件和文件夹
Test-Connection[a] ping ping ping 从目前电脑向指定电脑发送Ping,或指示另一台电脑这样做
Get-Content gc, type, cat type英语TYPE (DOS command) cat 获取文件内容
Get-Command gcm help英语help (command) type英语type (Unix), which英语which (command), compgen 列出可用的命令
Get-Help help, man help英语help (command) apropos英语apropos (Unix), man 在控制台上打印命令的文档
Clear-Host cls, clear cls英语cls (computing) clear 清除屏幕[b]
Copy-Item cpi, copy, cp copy, xcopy英语xcopy, robocopy英语robocopy cp 将文件和文件夹复制到另一个位置
Move-Item mi, move, mv move英语move (command) mv 将文件和文件夹移动到新位置
Remove-Item ri, del, erase, rmdir, rd, rm del英语del (command), erase英语del (command), rmdir, rd rm, rmdir 删除文件或文件夹
Rename-Item rni, ren, mv ren英语ren (command), rename mv 重命名单个文件、文件夹、硬链接或符号链接
Get-Location gl, cd, pwd cd pwd 显示工作路径(目前文件夹)
Pop-Location popd popd英语pushd and popd popd 将工作路径更改为最近推送到堆栈上的位置
Push-Location pushd pushd英语pushd and popd pushd 将工作路径存储到堆栈中
Set-Location sl, cd, chdir cd, chdir cd 改变工作路径
Tee-Object tee 不适用 tee 将输入管道传输到文件或变量,并沿管道传递输入
Write-Output echo, write echo echo 将字符串或其他对像打印到标准流
Get-Process gps, ps tlist,[c] tasklist英语tasklist[d] ps 列出所有正在执行的进程
Stop-Process spps, kill kill英语kill (command),[c] taskkill[d] kill[e] 停止正在执行的进程
Select-String sls findstr find, grep 打印与模式匹配的行
Set-Variable sv, set set英语Environment variable#DOS env, export, set, setenv 创建或更改环境变量的内容
Invoke-WebRequest iwr, curl, wget[f] curl wget, curl 获取互联网上的网页内容
  1. ^ 尽管外部ping命令仍可用于PowerShell,但“Test-Connection”的输出是一个可以通过编程来检查的结构化对象
    While the external ping command remains available to PowerShell, Test-Connection's output is a structured object that can be programmatically inspected.[7]
  2. ^ Clear-Host被实现为预定义的PowerShell功能。
  3. ^ 3.0 3.1 Available in Windows NT4, Windows 98 Resource Kit, Windows 2000 Support Tools
  4. ^ 4.0 4.1 Available in Windows XP Professional Edition and later
  5. ^ Also used in UNIX to send a process any signal, the "Terminate" signal is merely the default
  6. ^ curl and wget aliases are absent from PowerShell Core, so as to not interfere with invoking similarly named native commands.

Windows PowerShell 3.0 编辑

PowerShell 3.0 与 Windows 8 和 Windows Server 2012 集成。微软还为 Windows 7 的 Service Pack 1、Windows Server 2008 的 Service Pack 1 和 Windows Server 2008 R2 的 Service Pack 1 提供了 PowerShell 3.0。[8][9]

PowerShell 3.0 是一个更大的软件包 Windows Management Framework 3.0 (WMF3) (WMF3) 的一部分。 PowerShell 3.0 是一个更大的软件包 Windows 管理框架 3.0 (WMF3) 的一部分,其中还包含支持远程协作的 WinRM 服务。[10][11]

PowerShell 3.0 的新功能包括:[12][13]

  • 计划任务:可使用 Windows 任务调度程序基础架构计划任务在预定的时间和日期运行。
  • 会话重新连接:会话可以断开并重新连接。远程会话现在能更好地应对临时网络故障。
  • 改进了代码编写:增加了代码自动完成(IntelliSense)和代码片段。PowerShell ISE 允许用户使用对话框填写 PowerShell cmdlet 参数。
  • 授权支持:可将管理任务授权给无权执行此类任务的用户,而无需授予他们永久性的额外权限。
  • 更新帮助:可使用 Update-Help 命令更新帮助文档。

参考文献 编辑

  1. ^ 1.0 1.1 v7.4.1 Release of PowerShell. 2024年1月11日 [2024年1月15日]. 
  2. ^ PowerShell LICENSE. [2017-06-10]. (原始内容存档于2022-05-11). 
  3. ^ Snover, Jeffrey. PowerShell and WPF: WTF. Windows PowerShell Blog. Microsoft. 2008-05-25 [2017-06-10]. (原始内容存档于2019-01-08). 
  4. ^ Bright, Peter. PowerShell is Microsoft's latest open source release, coming to Linux, OS X. Ars Technica. Condé Nast. 2016-08-18 [2017-06-10]. (原始内容存档于2022-04-28). 
  5. ^ How Windows PowerShell works. Microsoft Developer Network. Microsoft. [2007-11-27]. (原始内容存档于2008-09-05). 
  6. ^ Truher, Jim. Extend Windows PowerShell With Custom Commands. MSDN Magazine (Microsoft). December 2007 [2017-06-10]. (原始内容存档于2008-10-06). 
  7. ^ Test-Connection. PowerShell documentations. 微软. 9 August 2015 [2019-12-18]. (原始内容存档于2016-10-31). 
  8. ^ PowerShell Version 3 is RTM!. tfl09.blogspot.com. [2023-09-09]. (原始内容存档于2023-09-05). 
  9. ^ WMF 3.0. www.microsoft.com. [2023-09-09]. (原始内容存档于2023-06-19). 
  10. ^ Powershell Version. poshnit.wordpress.com. [2023-09-09]. (原始内容存档于2023-09-05). 
  11. ^ Microsoft has released Windows Management Framework 3.0 - Community Technology Preview. www.ntcompatible.com. [2023-09-09]. (原始内容存档于2023-09-05). 
  12. ^ What Is PowerShell ISE?. www.easeus.com. [2023-09-09]. (原始内容存档于2023-09-05). 
  13. ^ Introducing Windows 8: An Overview for IT Professionals. books.google.com. [2023-09-09]. (原始内容存档于2023-09-05). 

扩展阅读 编辑

外部链接 编辑

微软官网
其他