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). 

擴展閱讀 編輯

外部連結 編輯

微軟官網
其他