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

Powershell过滤管道结果的更多相关文章

  1. Powershell使用管道

    管道并不是什么新事物,以前的Cmd控制台也有重定向的命令,例如Dir | More可以将结果分屏显示.传统的Cmd管道是基于文本的,但是Powershell是基于对象. PS> ls | Sor ...

  2. PowerShell过滤文件中的重复内容

    Get-Content -Path E:\test11\data.txt | Sort-Object | Get-Unique 源文件: AA0001 2014-06-30 15:27:13.073 ...

  3. PowerShell管道入门,看看你都会不(管道例子大全)

    PowerShell的一个重中之重的功能就是管道(pipeline),本文从浅入深,一步一步详解管道的使用方法和例子,来看看有没有你所不知道的吧,如果全知道,恭喜你已经很厉害啦--适用于所有Power ...

  4. 【转】PowerShell入门(七):管道——在命令行上编程

    转至:http://www.cnblogs.com/ceachy/archive/2013/02/22/PowerShell_Pipeline.html 管道对于Shell来说是个化腐朽为神奇的东西, ...

  5. Powershell --在线学习

    介绍和安装 自定义控制台 快速编辑模式和标准模式 快捷键 管道和重定向 Powershell交互式 数学运算 执行外部命令 命令集 别名 通过函数扩展别名 执行文件和脚本 Powershell变量 定 ...

  6. PowerShell 在线教程 4

    PowerShell 在线教程 4   认识Powershell 介绍和安装 自定义控制台 快速编辑模式和标准模式 快捷键 管道和重定向 Powershell交互式 数学运算 执行外部命令 命令集 别 ...

  7. 巧用linux版powershell,管理linux下的docker

    大家好,我把用powershell的docker马甲命令的好处,放在了页面下方,从第五章开始. powershell 传教士 原创文章 始于 2017-09-07 允许转载,但必须保留名字和出处,否则 ...

  8. powershell入门教程-v0.3版

    powershell入门教程-v0.3版 来源 https://www.itsvse.com/thread-3650-1-1.html 参考 http://www.cnblogs.com/piapia ...

  9. 【189】◀▶ PowerShell 系统学习

    参考网站如下: PowerShell 中文博客      PowerShell 博客——叹为观止 Mater-PowerShell      通过 PowerShell 编写脚本      Power ...

随机推荐

  1. IIS 403.14 - Forbidden错误解决方法

    HTTP 错误 403.14 - ForbiddenWeb 服务器被配置为不列出此目录的内容. 解决方法如下: 打开IIS的”处理程序映射设置“,在右边的操作栏下有 ”添加脚本映射“请求路径:*可执行 ...

  2. iOS开发手记 - iOS9.3 UINavigationController添加后不显示storyboard中viewcontroller里的控件的解决方法

    我原先是这么做的,通常也是这么做 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSD ...

  3. windows下MongoDB的安装及配置

    http://jingyan.baidu.com/article/d5c4b52bef7268da560dc5f8.html 使用MongoVUE链接本地mongodb 基本用法见这里:http:// ...

  4. IOS XMPP

    http://www.cnblogs.com/lmyhao/p/4120616.html

  5. MAC安装SVNServer

    MAC已经自带了SVN,所以,直接使用就好 1.创建svn repository svnadmin create /path/svn/pro  //仓库位置,svn是svn的目录,pro是一个版本库的 ...

  6. (转)UIWebView与JavaScript的那些事儿

    UIWebView是IOS SDK中渲染网面的控件,在显示网页的时候,我们可以hack网页然后显示想显示的内容.其中就要用到javascript的知识,而UIWebView与javascript交互的 ...

  7. lesson2:java阻塞队列的demo及源码分析

    本文向大家展示了java阻塞队列的使用场景.源码分析及特定场景下的使用方式.java的阻塞队列是jdk1.5之后在并发包中提供的一组队列,主要的使用场景是在需要使用生产者消费者模式时,用户不必再通过多 ...

  8. resin安装

    1. 说明 Resin版本:resin-3.1.6 2. 安装jdk 安装jdk-1_5_0_09-windows-i586-p.exe程序到相应目录. (假设安装到D:\ jdk1.5.0_09) ...

  9. 十二.200多万元得到的创业教训--app名字是关键

    摘要:当完毕了一个app后,就要须要上应用市场,以下讲一下起名和上应用市场的一些技巧. 健生干货分享:第12篇 1.必须是先上app store,再上其它应用市场 为啥要这样做?由于app store ...

  10. Android 基于Netty的消息推送方案之概念和工作原理(二)

    上一篇文章中我讲述了关于消息推送的方案以及一个基于Netty实现的一个简单的Hello World,为了更好的理解Hello World中的代码,今天我来讲解一下关于Netty中一些概念和工作原理的内 ...