欢迎光临
我们一直在努力

PowerSehll 中过滤管道结果

开头先讲四句话:

第一,如果要过滤对像,可以使用Where-Object;

第二,如果要过滤对像的属性,可以使用Select-Object;

第三,如果要自定义过滤效果,可以使用ForEach-Object;

第四,如果想过滤重复的结果,可以使用Get-Unique;

然后再加上对有些命令行中 -Filter的理解,Filter在有些命令中会出现,也是过滤的意思,我查了半天的文档,没有一个明确的解释,但突然从一个命令的解释中得到了官方的解释:

查询一个命令的全部帮助,如下:

PS C:\> help Get-WmiObject -full

NAME

    Get-WmiObject

SYNOPSIS

    Gets instances of WMI classes or information about the available classes.

SYNTAX

    Get-WmiObject [-Class] <String> [[-Property] <String[]>] [-Amended] [-AsJob] [-Authentication {Default | None |

    Connect | Call | Packet | PacketIntegrity | PacketPrivacy | Unchanged}] [-Authority <String>] [-ComputerName

    <String[]>] [-Credential <PSCredential>] [-DirectRead] [-EnableAllPrivileges] [-Filter <String>] [-Impersonation

    {Default | Anonymous | Identify | Impersonate | Delegate}] [-Locale <String>] [-Namespace <String>]

    [-ThrottleLimit <Int32>] [<CommonParameters>]

这条命令中有一个-filter参数,详细解释如下:

 -Filter <String>

     Specifies a Where clause to use as a filter. Uses the syntax of the WMI Query Language (WQL).

     Important: Do not include the Where keyword in the value of the parameter. For example, the following commands

     return only the logical disks that have a DeviceID of ‘c:’ and services that have the name ‘WinRM’ without

     using the Where keyword.

     `Get-WmiObject Win32_LogicalDisk -filter “DeviceID = ‘c:’ “`

     `Get-WmiObject win32_service -filter “name=’WinRM'”`

     Required?                    false

     Position?                    named

     Default value                None

     Accept pipeline input?       False

     Accept wildcard characters?  false

我对这个解释的理解是,首先不是所有命令支持-filter参数,对于支持的命令,所遵守的语法是WMI查询语言,如果想过多的查义这个句法,自行研究吧。

准备数据源,其实我就想看看一个对像有多少属性,然后去调用一下子。

PS C:\> Get-Service | Select-Object -First 1 | Get-Member -MemberType Properties

   TypeName: System.ServiceProcess.ServiceController

Name                MemberType    Definition

—-                ———-    ———-

Name                AliasProperty Name = ServiceName

RequiredServices    AliasProperty RequiredServices = ServicesDependedOn

CanPauseAndContinue Property      bool CanPauseAndContinue {get;}

CanShutdown         Property      bool CanShutdown {get;}

CanStop             Property      bool CanStop {get;}

Container           Property      System.ComponentModel.IContainer Container {get;}

DependentServices   Property      System.ServiceProcess.ServiceController[] DependentServices {get;}

DisplayName         Property      string DisplayName {get;set;}

MachineName         Property      string MachineName {get;set;}

ServiceHandle       Property      System.Runtime.InteropServices.SafeHandle ServiceHandle {get;}

ServiceName         Property      string ServiceName {get;set;}

ServicesDependedOn  Property      System.ServiceProcess.ServiceController[] ServicesDependedOn {get;}

ServiceType         Property      System.ServiceProcess.ServiceType ServiceType {get;}

Site                Property      System.ComponentModel.ISite Site {get;set;}

StartType           Property      System.ServiceProcess.ServiceStartMode StartType {get;}

Status              Property      System.ServiceProcess.ServiceControllerStatus Status {get;}

第一,如果要过滤对像,可以使用Where-Object;

PS C:\> Get-Service | Where-Object {$_.status -eq “Running”}

Status   Name               DisplayName

——   —-               ———–

Running  AdobeARMservice    Adobe Acrobat Update Service

Running  Appinfo            Application Information

Running  Apple Mobile De… Apple Mobile Device Service

Running  AppMgmt            Application Management

Running  AudioEndpointBu… Windows Audio Endpoint Builder

Running  Audiosrv           Windows Audio

Running  BDESVC             BitLocker Drive Encryption Service

这个命令的理解就是,where本身就是条件的意思,然后得接一个指令块,指明具体的条件。换句都明白的话就是我想看看现在哪些服务是运行的状态呢。$_代表当前对像。点就不解释了。

第二,如果要过滤对像的属性,可以使用Select-Object;

当我们找出想要的对像来,还有一些想法,一般情况下,只显示了部分对像的属性,那要看到指定的对像属性怎么弄呢? SO EASY

PS C:\> Get-Service | Where-Object {$_.status -eq “Running”} | Select-Object Name,MachineName,Site,Status

Name                             MachineName Site  Status

—-                             ———– —-  ——

AdobeARMservice                  .                Running

Appinfo                          .                Running

Apple Mobile Device Service      .                Running

AppMgmt                          .                Running

AudioEndpointBuilder             .                Running

Audiosrv                         .                Running

BDESVC                           .                Running

用句中国人都能听懂的话,就是想看哪个属性写哪个属性。

第三,如果要自定义过滤效果,可以使用ForEach-Object;

这个就得换个例子了,同时有一个好玩的占位符可以用呀,显得高大尚,其实很EASY

上代码:

PS C:\> ls | ForEach-Object {“FileName:{0} FileSize{1:n2}KB” -f $_.name,($_.length / 1kb)}

FileName:XMPCache FileSize0.00KB

FileName:baseline.xml FileSize30,812.20KB

看想来很恶心的样子,其实用简单的意思理解一下,LS 这条命令产生了好多对像集,然后用FOREACH-OBJECT 进行处理,后面的代码块就是执行的条件啦,{0}{1:n2} -f $_.name,($_.length / 1kb)}

这件事的理解就是0,1代表占位符而已,简单吧。{1:n2}n2,看上去很高大上的,其实就是N代表是数字,2,呵呵,保留两位小数。这些东西如果不解释,呵呵,以前我就是非常不明白,看了好多文档,其实SO EASY。

第四,如果想过滤重复的结果,可以使用Get-Unique;

这个直译就是得到唯一值。举个例子。

PS C:\> ls | ForEach-Object {$_.Extension } | Sort-Object | Get-Unique

.csv

.docx

.html

.log

.ps1

.txt

.xlsx

.xml

PS C:\>

这个命令的解释,LS 想要得到当前的目录对像,然后找找当前文件的扩展名,再扔进一个管道排排序,再扔进一个管道去掉重复的。记住你扔来扔去的都是对一个对像集进行操作而已。

POWERSHELL的命令和语法感觉很复杂,但只要是理解了原理,使用起来就SO EASY啦。事上无难事,只要肯攀登。

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。