Windows Powershell过滤管道结果

所属分类: 脚本专栏 / PowerShell 阅读数: 1030
收藏 0 赞 0 分享

如果要过滤对象可以使用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

更多精彩内容其他人还在看

Powershell学习笔记--使用正则表达式查找文件

本文介绍PowerShell中使用正则表达式的查找文件的方法,PowerShell的正则表达式与微软其它语言的正则表达式是一样的,使用非常方便。
收藏 0 赞 0 分享

Windows Powershell 介绍和安装

Powershell 是运行在windows机器上实现系统和应用程序管理自动化的命令行脚本环境。微软之所以将Powershell 定位为Power,并不是夸大其词,因为它完全支持对象。其可读性,易用性,可以位居当前所有shell之首。
收藏 0 赞 0 分享

Windows Powershell 自定义控制台

这篇文章主要介绍了Windows Powershell 自定义控制台,包括选项、字体、布局和颜色四个方面的自定义风格,希望对大家有所帮助
收藏 0 赞 0 分享

windows Powershell 快速编辑模式和标准模式

powershell控制台有两种模式,一个是快速编辑模式,一个是标准模式。
收藏 0 赞 0 分享

PowerShell中查找字符串位置的IndexOf函数使用实例

这篇文章主要介绍了PowerShell中查找字符串位置的IndexOf函数使用实例,例子简单明了,容易看懂,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell使用正则表达式查找字符串实例

这篇文章主要介绍了PowerShell使用正则表达式查找字符串实例,主要是对match运算符的使用介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell中使用正则表达式跨行匹配字符串的方法

这篇文章主要介绍了PowerShell中使用正则表达式跨行匹配字符串的方法,重点在于正则表达式的写法,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell中查看当前版本、Windows版本、.NET版本信息的代码

这篇文章主要介绍了PowerShell中查看当前版本、Windows版本、.NET版本信息的代码,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell 入门基础教程

Windows PowerShell 是专为系统管理员设计的新 Windows 命令行外壳程序。该外壳程序包括交互式提示和脚本环境,两者既可以独立使用也可以组合使用
收藏 0 赞 0 分享

PowerShell Contains函数查找字符串实例

这篇文章主要介绍了PowerShell Contains函数查找字符串实例,Contains函数的作用是查询一个字符串中是否存在另一个字符串,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多