5a. Windows Powershell內置cmdlets

Windows Powershell內置cmdlets
在這篇文章中,我所說的cmdlet意思就是Powershell內置命令,格式是單獨的動-名詞(Verb-noun)結構,前面也介紹過cmdlet的另外一層意思,由一系列的命令組成,一般會超過1行命令,並且保存在擴展名爲PS1的文本文件中。這裏,我們將側重於介紹Powershell內置的cmdlets
Powershell內置的cmdlets分爲13大塊
  1. 1. system built-in cmdlets: 有關係統的內置cmdlets
  2. 2. Objects built-in cmdlets: 有關對象的內置cmdlets
  3. 3. Items Built-in cmdlets: 有關項目(Item)的內置cmdlets
  4. 4. Aliases Built-in Powershell Cmdlets:有關別名的內置Cmdlets
  5. 5. Drivers Built-in cmdlets:有關驅動器的內置cmdlets
  6. 6. Paths and location: 有關路徑和位置的cmdlets
  7. 7. Contents Built-in Powershell Cmdlets: 有關內容的內置cmdlets
  8. 8. Access Operating System Components: 訪問操作系統組件的cmdlets
  9. 9. Output formatters Built-in Cmdlets: 有關輸出格式的內置cmdlets
  10. 10. Variable: 變量
  11. 11. Security Built-in Cmdlets: 有關安全的內置cmdlets
  12. 12. Miscellaneous Powershell Built-in cmdlets: 其他Powershell內置cmdlets
  13. 13. Debugging: 有關調試的內置cmdlets

Powershell
內置cmdlet分類
1System Built-in cmdlets 有關係統的內置Cmdlets
  1. a) get-help. 這是發掘Powershell內置資料庫的最好命令。在很多的cmdlets,都是以動詞"get"開頭,比如 get-help alias。 請記住get-help支持通用符,比如輸入get-help *object
  2. b) get-command: 顯示有關Powershell的版本號(不要把它和主機名搞混了)。這個命令可以幫你去查看你的Powershell版本號
  3. c) get-culture: 顯示主機的區域設置信息(就如控制面板裏的區域設置顯示一樣)
  4. d) get-uiculture: 顯示主機的當前用戶界面區域性信息
更多關鍵系統cmdlets請參考以前(3. Powershell 關鍵命令介紹)
2) Object Built-in Cmdlets: 有關對象的內置cmdlets
  1. a) get-member: 這是現在爲止最重要的cmdlet . get-member列舉了對象的屬性(Properties)、方法(Method)、類(Type)型等信息, 比如
  2. get-wmiobject | get-member

  3. 請參考前面的特別介紹get-member的文章

  4. b) foreach-object: 這個cmdlets的目的是在多個對象中執行循環,比如驅動器。foreach對於管道中的每個對象執行相同的腳本,比如
  5. $Nic= get-WmiObject Win32_NetworkAdapterConfiguration
    foreach ( $MacAdr in $Nic ) { "Mac Address = " + $MacAdr.MacAddress}

  6. 請注意,括號的作用是很重要的,() 或{},每個都有特定的作用(條件){腳本塊}。也請注意+,他不但可以把數字相加,而且可以把文本相連。

  7. c) where-object ( 經常會被簡寫成"Where"),依據管道的條件來過濾輸入,允許只對特定的對象進行操作,比如
  8. get-service | where-object{$_status -eq "stopped"}

  9. d) new-object: 創建.net或者com對象的實例。New-object在創建VBScript對象的時候特別有用,比如網羅對象,活動目錄對象(Active Directory Object),其他的程序比如IE對象也在此列。
  10. e) Select-object: 基於設置在cmdlet命令行的參數來過濾對象
  11. f) group-object: 一個非常有潛力的cmdlet, 在蒐集基於相同屬性的對象的時候非常有用
  12. g) sort-object: 按照屬性值進行排序
3Items Built-in Cmdlets 有關項目的cmdlets
  1. a) get-childitem: 顯示特定目錄的子項目,比如在一個文件夾的文件清單
  2. b) Get-item:獲取爲了某個命令空間的對象的值,比如get-iem .\* 顯示了在當前目錄的所有文件
  3. c) new-item: 在命名空間中創建新項目,比如創建子目錄
  4. d) set-item: 將項的值更改爲特定的值
  5. e) Remove-item: 刪除指定項
  6. f) move-item: 把項從一個地方轉移到別處,並且把原文件刪除
  7. g) rename-item: 對現有對象重命名
  8. h) copy-item: 把對象從一個地方複製到別處,保留原文件
  9. i) clear-item: 刪除對象的內容,但是不會刪除對象,比如可以註冊表鍵值刪除掉,但是不會刪除該註冊表
  10. j) invoke-item: 對對象指定某種程序執行
  11. k) get-acl: 獲取某個文件或者項的訪問控制清單(ACL)
  12. l) set-acl: 對某個文件或者項設置安全訪問控制清單
4Alias Built-in Powershell Cmdlets: 有關別名的默認cmdlets
想了解別名Alias很簡單,只要從我們內置的那些alias開始就行,比如gci (get-childitem), 比如Ft(format table). 例子有, get-process * | ft name,handles,cpu -auto
  1. a) set-alias: 對某個別名設置一個別名,你可以用它來創建你自己的別名
  2. b) get-alias: 返回所有定義好的別名
  3. c) new-alias: 創建一個新的別名,但是如果退出Poweshell中,這個別名將不能使用
  4. d) export-alias: 把系統所有別名導出到CSV格式的文件中
  5. e) import-alias: 從CSV 文件中更新系統別名
5) Drives Built-in Cmdlets: 有關驅動器的內置cmdlets
  1. a) get-psdrive: 獲取有關Poweshell驅動器的所有信息
  2. b) new-PSdriver: 新建Poweshell驅動器
  3. c) get-psProvider: 返回所有註冊的CMDlet提供程序和驅動器
  4. d) Remove-Drive: 從cmdlet提供商刪除驅動器
6)Paths and Location 路徑和位置
  1. a) get-location: 顯示當前位置
  2. b) set-location: 更改當前工作位置(它的別名是CD)
  3. c) combine-path: 將路徑從 Windows PowerShell 路徑轉換爲 Windows PowerShell 提供程序路徑
  4. d) test-path: 檢查路徑是否存在,存在的話返回正確,否則就是錯誤
  5. a) e)resolve-path: 解析路徑中的通用符
  6. e) Push-location將當前位置推入堆棧
  7. f) pop-location: 將當前位置更改爲最近推入到堆棧中的位置
7) Contents Built-in Powershell Cmdlets 有關內容的Cmdlets
  1. a) add-content: 將內容追加到一個文件裏,如果文件不存在就創建
  2. b) clear-content: 把文件裏的內容清除,但是文件還是保留
  3. c) get-content: 按行讀取文件內的內容
  4. d) set-content: 把文件內的所有內容以新內容取代
8) Access Operation System Components
  1. a) get-process: 返回所有運行的系統進程
  2. b) stop-process: 停止一個運行的進程
  3. c) get-service: 返回顯示註冊的系統服務
  4. d) stop-service: 停止正在運行的系統服務
  5. e) start-service: 啓動已經停止的系統服務
  6. f) suspend-service: 暫停正在運行的系統服務
  7. g) resume-service: 繼續暫停的系統服務
  8. h) restart-service: 停止並重新啓動系統服務
  9. i) set-service: 重新配置系統服務
  10. j) New-service: 註冊一個新的系統服務
  11. k) get-eventlog: 返回指定系統日誌的詳細清單
  12. l) get-wmiobject: 從WMI返回數據
9output Formatters build-in cmdlets: 有關輸出格式的內置cmdlets
  1. a) Out-file: 將輸出結果寫入到一個文件中
  2. b) out-printer: 將輸出結果發送到打印機上
  3. c) out-string: 把輸出結果返回給一個字符串
  4. d) out-null: 忽略所有輸出
  5. e) out-default: 把輸出發送到主機,不能在命令行中調用
  6. f) out-host: 把輸出發送到主機
  7. g) format-list: 將輸出的格式設置爲屬性列表,其中每個屬性均各佔一行顯示
  8. a) H) format-table: 將輸出的格式設置爲表(每行一個對象)
  9. h) format-wide: 將對象的格式設置爲只能顯示每個對象的一個屬性的寬表 (可以幾個對象一行)
  10. i) import-csv: 導入使用 Export-CSV cmdlet 產生的格式的逗號分隔值 (CSV) 文件,並返回對應於該 CSV 文件中所表示的對象的對象
  11. j) export-csv: 創建逗號分隔值 (CSV) 文件來顯示輸入對象
  12. k) write-host: 使用主機用戶界面來顯示對象
  13. l) Write-progress: 在 Windows PowerShell 命令窗口內顯示進度欄。
  14. m) write-debug: 將調試消息寫入主機顯示屏
  15. n) Write-verbose: 將字符串寫入主機的詳細顯示屏
  16. o) write-error: 將對象寫入錯誤管道
  17. p) read-host: 從控制檯讀取一行輸入
10Variable 變量
  1. a) get-variable: 返回變量的值
  2. b) new-variable: 創建帶特定名稱和值的新變量
  3. c) set-variable: 更新或者創建新的變量,
  4. d) remove-variable: 刪除變量定義
  5. e) clear-variable: 清空所有變量的值,但是定義繼續保留
11Security Built-in Cmdlets 有關安全的cmdlets
  1. a) get-pfxcertificate: 獲取文件中有關X509Certificate的信息
  2. b) get-credential: 通過詢問通過提供密碼來生成憑證
  3. c) get-authenticodesignatureL獲取文件中有關 Authenticode 簽名的信息。
  4. d) set-authenticodesignature: 更新Authenticode簽名
12) Miscellaneous Poweshell Built-in Cmdlets 其他Poweshell內置cmdlets
  1. a) get-date: 顯示當前日期和時間
  2. b) set-date: 更改系統日期和時間
  3. c) measure-object: 度量對象的特徵及其屬性
  4. d) new-timespan: 比較兩個時間日期對象的時間
  5. e) start-sleep: 暫停 處理
13Debugging 調試
Get-Command 的結果
下列命令可以得到所有的cmdlet
get-command | fw -column 3
p_w_picpath
結果是:
Add-Content Add-History Add-Member
Add-PSSnapin Clear-Content Clear-Item
Clear-ItemProperty Clear-Variable Compare-Object
ConvertFrom-SecureString Convert-Path ConvertTo-Html
ConvertTo-SecureString Copy-Item Copy-ItemProperty
Export-Alias Export-Clixml Export-Console
Export-Csv ForEach-Object Format-Custom
Format-List Format-Table Format-Wide
Get-Acl Get-Alias Get-AuthenticodeSignature
Get-ChildItem Get-Command Get-Content
Get-Credential Get-Culture Get-Date
Get-EventLog Get-ExecutionPolicy Get-Help
Get-History Get-Host Get-Item
Get-ItemProperty Get-Location Get-Member
Get-PfxCertificate Get-Process Get-PSDrive
Get-PSProvider Get-PSSnapin Get-Service
Get-TraceSource Get-UICulture Get-Unique
Get-Variable Get-WmiObject Group-Object
Import-Alias Import-Clixml Import-Csv
Invoke-Expression Invoke-History Invoke-Item
Join-Path Measure-Command Measure-Object
Move-Item Move-ItemProperty New-Alias
New-Item New-ItemProperty New-Object
New-PSDrive New-Service New-TimeSpan
New-Variable Out-Default Out-File
Out-Host Out-Null Out-Printer
Out-String Pop-Location Push-Location
Read-Host Remove-Item Remove-ItemProperty
Remove-PSDrive Remove-PSSnapin Remove-Variable
Rename-Item Rename-ItemProperty Resolve-Path
Restart-Service Resume-Service Select-Object
Select-String Set-Acl Set-Alias
Set-AuthenticodeSignature Set-Content Set-Date
Set-ExecutionPolicy Set-Item Set-ItemProperty
Set-Location Set-PSDebug Set-Service
Set-TraceSource Set-Variable Sort-Object
Split-Path Start-Service Start-Sleep
Start-Transcript Stop-Process Stop-Service
Stop-Transcript Suspend-Service Tee-Object
Test-Path Trace-Command Update-FormatData
Update-TypeData Where-Object Write-Debug
Write-Error Write-Host Write-Output
Write-Progress Write-Verbose Write-Warning


總結:
Poweshell內置的cmdlets
Powershell 有豐富的內置的動-名詞形式的cmdlet.你可能自己已經知道20個,但是強烈推薦你每個都過一篇,這樣你可能在以後也用的到。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章