Windows Powershell過(guò)濾管道結(jié)果
如果要過(guò)濾對(duì)象可以使用Where-Object;如果要過(guò)濾對(duì)象的屬性,可以使用Select-Object;如果要自定義個(gè)性化的過(guò)濾效果可以使用ForEach-Object。最后如果想過(guò)濾重復(fù)的結(jié)果,可是使用Get-Uinque。
篩選管道結(jié)果中的對(duì)象
如果你只對(duì)管道結(jié)果的特定對(duì)象感興趣,可是使用Where-Object對(duì)每個(gè)結(jié)果進(jìn)行嚴(yán)格篩選,一旦滿(mǎn)足你的標(biāo)準(zhǔn)才會(huì)保留,不滿(mǎn)足標(biāo)準(zhǔn)的就會(huì)自動(dòng)丟棄。例如你通過(guò)Get-service查看運(yùn)行在機(jī)器上的當(dāng)前服務(wù),但是可能只關(guān)心哪些正在運(yùn)行的服務(wù),這時(shí)就可是通過(guò)每個(gè)服務(wù)的屬性Status進(jìn)行過(guò)濾。但是前提條件是你得事先知道待處理的對(duì)象擁有哪些屬性。你可以通過(guò)Format-List * ,也可以通過(guò)Get-memeber。
PS C:Powershell> Get-service | Select-Object -First 1 | Format-List * Name : AdobeARMservice RequiredServices : {} CanPauseAndContinue : False CanShutdown : False CanStop : True DisplayName : Adobe Acrobat Update Service DependentServices : {} MachineName : . ServiceName : AdobeARMservice ServicesDependedOn : {} ServiceHandle : Status : Running ServiceType : Win32OwnProcess Site : Container : PS C:Powershell> Get-service | Select-Object -First 1 | Get-Member -MemberType Property TypeName: System.ServiceProcess.ServiceController Name MemberType Definition ---- ---------- ---------- CanPauseAndContinue Property System.Boolean CanPauseAndContinue {get;} CanShutdown Property System.Boolean CanShutdown {get;} CanStop Property System.Boolean CanStop {get;} Container Property System.ComponentModel.IContainer Container {g... DependentServices Property System.ServiceProcess.ServiceController[] Dep... DisplayName Property System.String DisplayName {get;set;} MachineName Property System.String MachineName {get;set;} ServiceHandle Property System.Runtime.InteropServices.SafeHandle Ser... ServiceName Property System.String ServiceName {get;set;} ServicesDependedOn Property System.ServiceProcess.ServiceController[] Ser... ServiceType Property System.ServiceProcess.ServiceType ServiceType... Site Property System.ComponentModel.ISite Site {get;set;} Status Property System.ServiceProcess.ServiceControllerStatus...
知道了對(duì)象有哪些屬性,要完成上面提到的需求就很容易了。
PS C:Powershell> get-service | Where-Object {$_.Status -eq "Running"} Status Name DisplayName ------ ---- ----------- Running AdobeARMservice Adobe Acrobat Update Service Running AppHostSvc Application Host Helper Service Running AppIDSvc Application Identity Running Appinfo Application Information Running AudioEndpointBu... Windows Audio Endpoint Builder Running Audiosrv Windows Audio Running BDESVC BitLocker Drive Encryption Service Running BFE Base Filtering Engine Running BITS Background Intelligent Transfer Ser... Running CcmExec SMS Agent Host
這里稍微解釋一下,Where-Object的參數(shù)的是一個(gè)布爾表達(dá)式,$_代表過(guò)濾過(guò)程中經(jīng)過(guò)管道的當(dāng)前結(jié)果。另外Where-Object還有一個(gè)別名 “?” 更形象。
選擇對(duì)象的屬性
包含在每一個(gè)對(duì)象中的屬性可能有很多,但是并不是所有的屬性你都感興趣,這時(shí)可以使用Select-Object 限制對(duì)象的屬性。接下來(lái)的例子演示如果獲取機(jī)器上匿名帳號(hào)的完整信息。
PS C:Usersv-bali.FAREAST> Get-WmiObject Win32_UserAccount -filter "LocalAccount=True AND Name='guest'" AccountType : 512 Caption : myhomeguest Domain : myhome SID : S-1-5-21-3064017030-3269374297-2491181182-501 FullName : Name : guest
如果你只對(duì)用戶(hù)名、描述,啟用感興趣。
PS C:Powershell> Get-WmiObject Win32_UserAccount -filter "LocalAccount=True AND Name='guest'" | Select-Object Name,Description,Disabled Name Description Disabled ---- ----------- -------- guest Built-in account for gu... True
Select-Object也支持通配符。
Dir | Select-Object * -exclude *A*
限制對(duì)象的數(shù)量
列出最后修改的5個(gè)文件
PS C:Powershell> Dir | Select-Object -ExcludeProperty "*N*" -First 5 目錄: C:Powershell Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 2011/11/24 18:30 67580 a.html -a--- 2011/11/24 20:04 26384 a.txt -a--- 2011/11/24 20:26 12060 alias -a--- 2011/11/25 11:20 556 employee.xml -a--- 2011/11/29 19:23 21466 function.ps1
列出占用CPU最大的5個(gè)進(jìn)程
PS C:Powershell> get-process | sort -Descending cpu | select -First 5 Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 1336 98 844304 809388 1081 164.69 3060 iexplore 224 10 74676 62468 188 81.10 4460 AcroRd32 130 9 28264 39092 167 70.57 3436 dwm 169 8 7576 29568 134 65.22 3364 notepad 989 34 72484 35996 393 62.67 4724 BingDict
逐個(gè)處理所有管道結(jié)果
如果想對(duì)管道結(jié)果進(jìn)行逐個(gè)個(gè)性化處理可是使用ForEach-Object
ls | ForEach-Object {"文件名: 文件大小(M): " -f $_.Name,$_.Length/1M}
PS C:Powershell> ls | ForEach-Object {"文件名:{0} 文件大小{1}KB: " -f $_.Name,
($_.length/1kb).tostring()}
文件名:a.html 文件大小65.99609375KB:
文件名:a.txt 文件大小25.765625KB:
文件名:alias 文件大小11.77734375KB:
文件名:employee.xml 文件大小0.54296875KB:
文件名:function.ps1 文件大小20.962890625KB:
文件名:LogoTestConfig.xml 文件大小0.181640625KB:
文件名:ls.html 文件大小3.37890625KB:
刪除重復(fù)對(duì)象
Get-Unique可以從已排序的對(duì)象列表中刪除重復(fù)對(duì)象。Get-Unique會(huì)逐個(gè)遍歷對(duì)象,每次遍歷時(shí)都會(huì)與前一個(gè)對(duì)象進(jìn)行比較,如果和前一個(gè)對(duì)象相等就會(huì)拋棄當(dāng)前對(duì)象,否則就保留。所以如果對(duì)象列表中沒(méi)有排序,Get-Unique不能完全發(fā)揮作用,只能保證相鄰對(duì)象不重復(fù)。
PS C:Powershell> 1,2,1,2 | Get-Unique 1 2 1 2 PS C:Powershell> 1,2,1,2 | Sort-Object |Get-Unique 1 2 PS C:Powershell> ls | foreach{$_.extension} | Sort-Object |Get-Unique .bat .html .ps1 .txt .vbs .xml
- Shell腳本中管道的幾種使用實(shí)例講解
- Shell腳步攻略之管道重定向基礎(chǔ)
- PowerShell管道入門(mén)必看篇(管道例子大全)
- linux shell 管道命令(pipe)使用及與shell重定向區(qū)別
- PowerShell實(shí)現(xiàn)按條件終止管道的方法
- PowerShell中終止管道的方法
- PowerShell入門(mén)教程之PowerShell管道介紹
- Windows Powershell導(dǎo)出管道結(jié)果
- shell腳本一鍵同時(shí)推送代碼至github和gitee的解決辦法
- 如何利用 tee 命令調(diào)試shell腳本中的管道
相關(guān)文章
PowerShell 獲取系統(tǒng)信息的函數(shù)
如果你要得到本地或遠(yuǎn)程的使用配置信息,又不想浪費(fèi)太多的解決時(shí)間??梢栽赑owershell中使用systeminfo.exe提取數(shù)據(jù)2014-03-03PowerShell顯示隱藏文件和系統(tǒng)文件的方法
這篇文章主要介紹了PowerShell顯示隱藏文件和系統(tǒng)文件的方法,重點(diǎn)在于對(duì)文件、文件夾屬性的介紹,需要的朋友可以參考下2014-08-08PowerShell Contains函數(shù)查找字符串實(shí)例
這篇文章主要介紹了PowerShell Contains函數(shù)查找字符串實(shí)例,Contains函數(shù)的作用是查詢(xún)一個(gè)字符串中是否存在另一個(gè)字符串,需要的朋友可以參考下2014-08-08使用PowerShell操作Windows服務(wù)的命令小結(jié)
這篇文章主要介紹了使用PowerShell操作Windows服務(wù)的命令小結(jié),本文只是做了一個(gè)命令列表,需要的朋友可以參考下2014-08-08PowerShell腳本中控制Windows DNS服務(wù)的方法
這篇文章主要介紹了PowerShell腳本中控制Windows DNS服務(wù)的方法,本文給出了相關(guān)例子和命令,需要的朋友可以參考下2014-08-08Windows Powershell 訪問(wèn)數(shù)組
本文主要講訴了windows powershell 從數(shù)組中選擇多個(gè)元素,將數(shù)組逆序輸出,給數(shù)組添加和刪除元素,希望對(duì)大家理解powershell能有所幫助2014-09-09