Windows Powershell过滤管道结果

所属分类: 脚本专栏 / PowerShell 阅读数: 1163
收藏 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

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

Windows Powershell Switch 循环

这篇文章主要介绍了Windows Powershell Switch 循环以及PowerShell中数组可以与switch语句结合,产生意想不到的效果。
收藏 0 赞 0 分享

Windows Powershell 定义函数

这篇文章主要介绍了Windows Powershell 定义函数,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell实现导入安装证书功能脚本分享

这篇文章主要介绍了Powershell实现导入安装证书功能脚本分享,本文用编程方法实现把pfx证书文件导入到指定的库中,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell改变脚本执行优先权的代码分享

这篇文章主要介绍了Powershell改变脚本执行优先权的代码分享,本文通过控制进程的方式调整程序的执行顺序,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell读取PFX证书并输入密码的脚本分享

这篇文章主要介绍了Powershell读取PFX证书并输入密码的脚本分享,本文实现直接用脚本输入密码,避免了手动输入密码的麻烦,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell访问SQL Server数据库代码实例

这篇文章主要介绍了Powershell访问SQL Server数据库代码实例,本文直接给出代码,使用时只需要替换数据库配置参数即可,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell打印文本文档例子

这篇文章主要介绍了Powershell打印文本文档例子,本文相对简单,默认给出的是打印系统日志文件,使用时要替换成自己想打印的文件,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell创建简洁的HTML报告例子

这篇文章主要介绍了Powershell创建简洁的HTML报告例子,本文先是讲解了实现的步骤,然后给出了实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell在一个会话中只允许执行指定命令的方法

这篇文章主要介绍了Powershell在一个会话中只允许执行指定命令的方法,使用本文的技巧可以达到控制权限的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell中创建自定义对象例子

这篇文章主要介绍了Powershell中创建自定义对象例子,本文内容需要在3.0版本以上有效,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多