麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > PowerShell > 正文

Windows Powershell過濾管道結果

2020-10-29 21:02:49
字體:
來源:轉載
供稿:網友

如果要過濾對象可以使用Where-Object;如果要過濾對象的屬性,可以使用Select-Object;如果要自定義個性化的過濾效果可以使用ForEach-Object。最后如果想過濾重復的結果,可是使用Get-Uinque。

篩選管道結果中的對象

如果你只對管道結果的特定對象感興趣,可是使用Where-Object對每個結果進行嚴格篩選,一旦滿足你的標準才會保留,不滿足標準的就會自動丟棄。例如你通過Get-service查看運行在機器上的當前服務,但是可能只關心哪些正在運行的服務,這時就可是通過每個服務的屬性Status進行過濾。但是前提條件是你得事先知道待處理的對象擁有哪些屬性。你可以通過Format-List * ,也可以通過Get-memeber。

PS C:Powershell> Get-service | Select-Object -First 1 | Format-List *Name        : AdobeARMserviceRequiredServices  : {}CanPauseAndContinue : FalseCanShutdown     : FalseCanStop       : TrueDisplayName     : Adobe Acrobat Update ServiceDependentServices  : {}MachineName     : .ServiceName     : AdobeARMserviceServicesDependedOn : {}ServiceHandle    :Status       : RunningServiceType     : Win32OwnProcessSite        :Container      :PS C:Powershell> Get-service | Select-Object -First 1 | Get-Member -MemberTypeProperty  TypeName: System.ServiceProcess.ServiceControllerName        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...

知道了對象有哪些屬性,要完成上面提到的需求就很容易了。

PS C:Powershell> get-service | Where-Object {$_.Status -eq "Running"}Status  Name        DisplayName------  ----        -----------Running AdobeARMservice   Adobe Acrobat Update ServiceRunning AppHostSvc       Application Host Helper ServiceRunning AppIDSvc      Application IdentityRunning Appinfo       Application InformationRunning AudioEndpointBu...  Windows Audio Endpoint BuilderRunning Audiosrv      Windows AudioRunning BDESVC       BitLocker Drive Encryption ServiceRunning BFE         Base Filtering EngineRunning BITS        Background Intelligent Transfer Ser...Running CcmExec       SMS Agent Host

這里稍微解釋一下,Where-Object的參數的是一個布爾表達式,$_代表過濾過程中經過管道的當前結果。另外Where-Object還有一個別名 “?” 更形象。

選擇對象的屬性

包含在每一個對象中的屬性可能有很多,但是并不是所有的屬性你都感興趣,這時可以使用Select-Object 限制對象的屬性。接下來的例子演示如果獲取機器上匿名帳號的完整信息。

PS C:Usersv-bali.FAREAST> Get-WmiObject Win32_UserAccount -filter "LocalAccount=True AND Name='guest'"AccountType : 512Caption     : myhomeguestDomain     : myhomeSID        : S-1-5-21-3064017030-3269374297-2491181182-501FullName    :Name    : guest

如果你只對用戶名、描述,啟用感興趣。

PS C:Powershell> Get-WmiObject Win32_UserAccount -filter "LocalAccount=True AND Name='guest'" | Select-Object Name,Description,DisabledName            Description                 Disabled----              -----------                 --------guest           Built-in account for gu...           True

Select-Object也支持通配符。

Dir | Select-Object * -exclude *A*
限制對象的數量

列出最后修改的5個文件

PS C:Powershell> Dir | Select-Object -ExcludeProperty "*N*" -First 5  目錄: C:PowershellMode        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個進程

PS C:Powershell> get-process | sort -Descending cpu | select -First 5Handles 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

逐個處理所有管道結果

如果想對管道結果進行逐個個性化處理可是使用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:
刪除重復對象

Get-Unique可以從已排序的對象列表中刪除重復對象。Get-Unique會逐個遍歷對象,每次遍歷時都會與前一個對象進行比較,如果和前一個對象相等就會拋棄當前對象,否則就保留。所以如果對象列表中沒有排序,Get-Unique不能完全發揮作用,只能保證相鄰對象不重復。

PS C:Powershell> 1,2,1,2 | Get-Unique1212PS C:Powershell> 1,2,1,2 | Sort-Object |Get-Unique12PS C:Powershell> ls | foreach{$_.extension} | Sort-Object |Get-Unique.bat.html.ps1.txt.vbs.xml

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久精品伊人网 | 午夜视频在线免费播放 | 91麻豆精品国产91久久久更新资源速度超快 | 老女人碰碰在线碰碰视频 | 久久久亚洲欧美综合 | 国产精品久久久久久久模特 | 福利免费观看 | 欧美一区二区三区免费观看 | 国产成人高清成人av片在线看 | 黄色免费av| 久久久www成人免费精品 | 亚洲性在线视频 | 国产精品久久久久久一区二区三区 | 亚洲嫩草av | 精品一区二区在线观看 | 91一级毛片 | 欧美一级黄色片在线观看 | 欧美一区二区三区久久精品视 | 在线日韩av电影 | 91短视频免费 | 欧美成人se01短视频在线看 | 欧美精品一区二区三区在线播放 | 成人午夜视频免费看 | 高清做爰免费无遮网站挡 | 欧美性受xxx黑人xyx性爽 | 亚州综合一区 | 久色视频| 老司机免费福利午夜入口ae58 | 欧美人的天堂一区二区三区 | 日本一区二区不卡高清 | 免费毛片观看 | 成人在线视频在线观看 | 一级电影在线观看 | 国产免费人做人爱午夜视频 | 国产免费最爽的乱淫视频a 毛片国产 | 欧美成人性生活片 | 国产乱淫av| 欧美精品成人一区二区在线观看 | 国产女同玩人妖 | 91精品国产成人 | 国产精品自拍av |