Powershell过滤管道结果 2


通过管道可以过滤某些对象和对象的属性,这个功能很实用,因为很多时候我们并不是对所有的结果感兴趣,可能只会对某些结果感兴趣。如果要过滤对象可以使用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                : 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...

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

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的参数的是一个布尔表达式,$_代表过滤过程中经过管道的当前结果。另外Where-Object还有一个别名 “?” 更形象。

选择对象的属性

包含在每一个对象中的属性可能有很多,但是并不是所有的属性你都感兴趣,这时可以使用Select-Object 限制对象的属性。接下来的例子演示如果获取机器上匿名帐号的完整信息。

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

如果你只对用户名、描述,启用感兴趣。

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*

限制对象的数量

列出最后修改的5个文件

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个进程

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

逐个处理所有管道结果

如果想对管道结果进行逐个个性化处理可是使用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-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
本文链接: https://www.pstips.net/powershell-filtering-pipeline-results.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

回复 北国荔枝 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

2 条评论 “Powershell过滤管道结果