powershell@管道符过滤的顺序问题@powershell管道符如何工作

news/2024/9/23 10:25:32/

文章目录

    • select 和 where谁先执行
    • powershell管道符
      • stop-service 为例查看文档中的典型参数介绍
      • stop-process为例介绍管道符传参是怎么工作的
        • Id参数
        • InputObject 参数
        • Name参数
        • 额外的试验
        • 反面例子
        • 应用:get-process 和stop-process配合

select 和 where谁先执行

  • 在执行筛选时,指定命令的顺序确实很重要。

  • 例如,考虑这样一种情况:使用 Select-Object 只选择几个属性(比如Name,Date),而使用 Where-Object 过滤的属性是(size)不在选择范围内的属性。在这种情况下,必须先进行过滤,否则在尝试执行过滤时,管道中将不存在该属性。

  • 下面的例子返回的内容会是空的

    powershell">Get-Service -ErrorAction Ignore |
    Select-Object -Property DisplayName, Running, Status |
    Where-Object CanPauseAndContinue
    
  • 更改select和where的顺序,就是正确的用法

    powershell">PS>Get-Service -ErrorAction Ignore |
    >> Where-Object CanPauseAndContinue |
    >> Select-Object -Property DisplayName, StatusDisplayName                                           Status
    -----------                                           ------
    Control Center Hotkey Service                        Running
    Intel(R) Dynamic Tuning Technology Telemetry Service Running
    Intel(R) Graphics Command Center Service             Running
    Intel(R) Innovation Platform Framework Service       Running
    Workstation                                          Running
    Web Threat Defense Service                           Running
    Web Threat Defense User Service_1bd31e               Running
    Windows Management Instrumentation                   Running
    

powershell_34">powershell管道符

  • 这里解释powershell管道符是如何工作的,编写支持管道符的函数另见它文(阅读本文有助于理解和编写这类支持管道符函数)

  • One-liners and the pipeline - PowerShell | Microsoft Learn ~ 单行代码和管道 - PowerShell | Microsoft Learn

  • 我们知道,有些命令可以接受管道符传递过来的参数,例如

    • Stop-Service,简写别名为spsv
    • Stop-process,简写为spps
  • 下面介绍它们怎么处理管道符输入

stop-service 为例查看文档中的典型参数介绍

  • 为了说明问题,这里截取了stop-service的部分参数的文档

    • 这里截取的是三个典型的参数

      powershell">...
      -DisplayName <String[]>Specifies the display names of the services to stop. Wildcard characters arepermitted.Required?                    truePosition?                    namedDefault value                NoneAccept pipeline input?       FalseAccept wildcard characters?  false-InputObject <ServiceController[]>Specifies ServiceController objects that represent the services to stop. Enter avariable that contains the objects, or type a command or expression that gets theobjects.Required?                    truePosition?                    0Default value                NoneAccept pipeline input?       True (ByValue)Accept wildcard characters?  false-Name <String[]>Specifies the service names of the services to stop. Wildcard characters arepermitted.Required?                    truePosition?                    0Default value                NoneAccept pipeline input?       True (ByPropertyName, ByValue)Accept wildcard characters?  false
      ...
      
    • 当一个参数同时接受按属性名和值输入的管道输入时,它总是先尝试按值输入。

    • 如果按值输入失败,它就会尝试按属性名输入。

    • 按值输入有点误导。我更喜欢称之为按类型。这意味着如果你将产生 ServiceController 对象类型的命令结果导入 Stop-Service,它会将该输入绑定到 InputObject 参数。

    • 但如果将产生字符串输出的命令结果导入 Stop-Service,则会将其绑定到 Name 参数。

    • 如果将一条不产生 ServiceController 或 String 对象的命令的结果导入 Stop-Service,不意味着绑定一定失败

    • 如果它产生(返回)的某个对象的属性包含了Name,那么它会将输出中的 Name 属性绑定到 Stop-Service 的 Name 参数。

stop-process为例介绍管道符传参是怎么工作的

  • 经典命令stop-process,结束进程的cmdlet

    • powershell">  -Id <System.Int32[]>Specifies the process IDs of the processes to stop. To specify multiple IDs, use commas to separate the IDs. Tofind the PID of a process, type `Get-Process`.Required?                    truePosition?                    0Default value                NoneAccept pipeline input?       True (ByPropertyName)Accept wildcard characters?  false-InputObject <System.Diagnostics.Process[]>Specifies the process objects to stop. Enter a variable that contains the objects, or type a command or expression that gets the objects.Required?                    truePosition?                    0Default value                NoneAccept pipeline input?       True (ByValue)Accept wildcard characters?  false-Name <System.String[]>Specifies the process names of the processes to stop. You can type multiple process names, separated by commas,or use wildcard characters.Required?                    truePosition?                    namedDefault value                NoneAccept pipeline input?       True (ByPropertyName)Accept wildcard characters?  true
      
  • 为了便于介绍,这里创建两个notepad进程

    • powershell">PS C:\Users\cxxu\Desktop> $pss=ps Notepad
      PS C:\Users\cxxu\Desktop> $pssNPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName------    -----      -----     ------      --  -- -----------44    74.81     123.38       0.58     440   1 Notepad12     2.70      13.02       0.02   21176   1 Notepad
      
Id参数
  • 可以看到,Id参数接受管道符的输入,并且是管道符前的表达式输出的对象包含名为Id的属性(而且属性值类型兼容<System.Int32[]>时才会被绑定到这个参数上
InputObject 参数
  • 然而stop-process还有其他参数支持管道符的形式输入,例如InputObject,并且通常具有更高的优先级,因为powershell管道符传参时,优先检查ByValue的参数,然后才是ByPropertyName的参数

  • 正如前面所讲的那样,文档里面InputObject是带有类型的,类型为<System.Diagnostics.Process[]>,当管道符前面的表达式产生的刚好是这个类型的对象,那么就会被InputObject所捕获绑定

  • 执行$pss|stop-Process仿佛执行的是以下遍历语句

    powershell">foreach ($p in $pss){stop-process -InputObject $p
    }
  • 在PowerShell中,-InputObject 参数通常用于指定直接传递给 cmdlet 的输入对象。

  • 例如参数声明 -InputObject <System.Diagnostics.Process[]>,其中<System.Diagnostics.Process[]> 表示 InputObject 参数应该是 System.Diagnostics.Process 类型的对象数组,这个类型代表系统中的进程。

  • 这种参数类型经常用于那些接收进程作为输入的cmdlet,比如 Stop-Process

  • 例如,如果你有一个 System.Diagnostics.Process 对象的数组,你可以使用 Stop-Process cmdlet 来结束这些进程:

    powershell"># 获取所有名为 "notepad" 的进程对象的数组
    $processes = Get-Process -Name "notepad"# 将进程对象数组传递给 Stop-Process cmdlet 来结束这些进程
    Stop-Process -InputObject $processes
    
    • 在这个例子中,-InputObject 参数接收了一个 Process 对象的数组,然后 Stop-Process cmdlet 将结束数组中的每一个进程。

    • 当你直接使用 -InputObject 参数时,通常不需要通过管道传递对象,因为你已经直接提供了要处理的对象数组。

Name参数
  • Id参数类似,当管道符传入的对象包含了Id属性,并且类型为<System.String[]>时,有机会绑定到这个参数上
额外的试验
  • 我们提到说管道符输入时,会分别检查byValue和byPropertyName类型的参数

  • 下面自定义一个类型,然后看看通过管道符传递能否按照预期工作

  • 自定义一个类型,包含一个字段Name,值为notepad

    powershell">$custObj = [PSCustomObject]@{Name = 'notepad'
    }
    
  • 检查$custObj对象的成员

    PS C:\repos\scripts> $custObj = [PSCustomObject]@{
    >>     Name = 'notepad'
    >> }
    PS C:\repos\scripts> $custObj | Get-MemberTypeName: System.Management.Automation.PSCustomObjectName        MemberType   Definition
    ----        ----------   ----------
    Equals      Method       bool Equals(System.Object obj)
    GetHashCode Method       int GetHashCode()
    GetType     Method       type GetType()
    ToString    Method       string ToString()
    Name        NoteProperty string Name=notepad
    
    • 可以发现powershell会自定让其继承一些必要的成员方法和特殊属性
  • 使用这个对象传递给Stop-Service

    • powershell">#启动一个notepad进程,用来试验杀死
      PS C:\repos\scripts>  notepad
      #将$custObj传递给Stop-Process,检验其是否能够解析出$custObj对象中有Name属性,从而杀死所有Name为notepad的进程
      PS C:\repos\scripts> $custObj | Stop-Process   
      
    • 经过检验,上述语句顺利执行

反面例子
  • powershell">PS> 'notepad'|Stop-Process
    Stop-Process: The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
    
    • 输入对象无法绑定到该命令的任何参数,原因可能是该命令不接受管道输入,或者输入及其属性与接受管道输入的任何参数都不匹配。

    • 在这个例子中,语句尝试传递一个字符串notepadstop-process,我们可以猜测其意图是杀死所有名为notepad的进程,然而stop-process不支持这种用法,不会达到以下语句的效果

      • powershell">stop-process -name 'notepad'
        
    • stop-process的所有参数中,有3个参数会尝试绑定来自管道符的输入,正如文档指出的,Id,InputObject,Name三个参数,并且都有各自的类型要求,注明在了文档中

      • 其中IdName都是ByPropertyName类型的,而InputObject优先级高,是ByValue型的
      • 上述反面例子中,管道符前的字符串notepad不是InputObject要求的类型,因此不会被绑定到InputObject
      • notepad作为字符串对象,没有IdName属性,自然也不会绑定到IdName参数
    • 综上,字符串作为管道符传递给stop-process必然报错

  • 事实上,stop-process第一个默认的位置参数是Id,也就是进程号,而不是进程对应的程序名字

    • 这是合理的,因为一个程序可能有多个运行示例,例如notepad被打开多个进程,而我们要关闭notepad时可能要保留其中的一个或者只想关闭其中的具体某一个,为了防止轻易关闭所有进程造成误杀,因此要显式使用-Name参数指出您确实想要关闭所有具有指定程序名字的进程
    • 否则您应该通过get-process来查看相关进程,然后记住或者复制Id号,执行stop-process <Id>
应用:get-process 和stop-process配合
  • 假设有以下对象

    • powershell">PS> $pss=get-process notepad
      PS>$pssNPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName------    -----      -----     ------      --  -- -----------47    73.11     117.62       0.17   12772   1 Notepad13     2.83      12.51       0.00   13768   1 Notepad
      
  • 我们看看$pss对象能否传递给stop-process这个cmdlet呢?

    • 利用Get-Member(alias:gm)来检查$pss对象的成员(属性,成员函数等),这个输出通常是比较长的

      • 事实上,传递给gm的如果是一个容器类型(比如数组),那么gm返回的是容器保存的对象的类型

      • 例如某个变量是包含2个字符串的数组,那么传给gm返回的是字符串类型的成员列表

      • 我们检查以下$pss是什么类型,调用对象的.GetType()方法即可

        PS> $pss.GetType()IsPublic IsSerial Name                                     BaseType
        -------- -------- ----                                     --------
        True     True     Object[]                                 System.Array
        
        • 可以看到,$pss的类型是一个数组(可迭代对象)
    • 这里看看$pss数组中的对象有哪些成员,以便于我们对$pss进一步处理,比如便利迭代等操作

      powershell">PS C:\Users\cxxu\Desktop> $pss|gmTypeName: System.Diagnostics.ProcessName                       MemberType     Definition
      ----                       ----------     ----------
      Handles                    AliasProperty  Handles = Handlecount
      Name                       AliasProperty  Name = ProcessName
      NPM                        AliasProperty  NPM = NonpagedSystemMemorySize64
      PM                         AliasProperty  PM = PagedMemorySize64
      SI                         AliasProperty  SI = SessionId
      VM                         AliasProperty  VM = VirtualMemorySize64
      WS                         AliasProperty  WS = WorkingSet64
      Parent                     CodeProperty   System.Object Parent{get=GetParentProcess;}
      Disposed                   Event          System.EventHandler Disposed(System.Object…
      ErrorDataReceived          Event          System.Diagnostics.DataReceivedEventHandle…
      Exited                     Event          System.EventHandler Exited(System.Object, …
      OutputDataReceived         Event          System.Diagnostics.DataReceivedEventHandle…
      BeginErrorReadLine         Method         void BeginErrorReadLine()
      BeginOutputReadLine        Method         void BeginOutputReadLine()
      CancelErrorRead            Method         void CancelErrorRead()
      ...
      Id                         Property       int Id {get;}
      MachineName                Property       string MachineName {get;}
      MainModule                 Property       System.Diagnostics.ProcessModule MainModul…
      MainWindowHandle           Property       System.IntPtr MainWindowHandle {get;}
      ...
      
    • 我们可以利用group来统计一下结果,比如我们gm返回的结果,按照对象的成员的各种类型来统计

      • powershell">PS> $pss|gm |group -Property MemberTypeCount Name                      Group
        ----- ----                      -----7 AliasProperty             {Handles = Handlecount, Name = ProcessName, NPM = No…1 CodeProperty              {System.Object Parent{get=GetParentProcess;}}52 Property                  {int BasePriority {get;}, System.ComponentModel.ICon…1 NoteProperty              {string __NounName=Process}8 ScriptProperty            {System.Object CommandLine {get=…2 PropertySet               {PSConfiguration {Name, Id, PriorityClass, FileVersi…19 Method                    {void BeginErrorReadLine(), void BeginOutputReadLine…4 Event                     {System.EventHandler Disposed(System.Object, System.
      • 可以看到,$pss中的对象包含的成员的类型有8种,最重要的两类是Property,Method

    • powershell">PS C:\Users\cxxu\Desktop> $pss|gm |?{$_.Name -eq 'Id'}TypeName: System.Diagnostics.ProcessName MemberType Definition
      ---- ---------- ----------
      Id   Property   int Id {get;}
      
    • 回到stop-process,经过上面的操作可知,get-process返回的对象(如果是数组,自动迭代其中的元素)包含了名为Id的属性,那么可以通过管道接受Get-Process的输出

      • PS> $pss|Stop-Process
        
      • 上述命令会杀死所有$pss中列出的进程,仿佛执行的是

        • powershell">foreach ($p in $pss){stop-process -id $p.Id
          }
          
  • 处理上面的$pss是一个包含多个进程对象的数组情况以外,也可以处理单个进程的情况

    • 例如

      powershell">PS[BAT:84%][MEM:37.91% (12.02/31.70)GB][21:36:45]
      # [~\Desktop]
      PS> ps gopeedNPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName------    -----      -----     ------      --  -- -----------51   147.82     155.86     223.50   16432   1 gopeedPS[BAT:84%][MEM:37.91% (12.02/31.70)GB][21:36:47]
      # [~\Desktop]
      PS> $p=ps gopeedPS[BAT:84%][MEM:37.91% (12.02/31.70)GB][21:37:03]
      # [~\Desktop]
      PS> $pNPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName------    -----      -----     ------      --  -- -----------52   148.82     157.17     223.62   16432   1 gopeedPS[BAT:84%][MEM:37.91% (12.02/31.70)GB][21:37:04]
      # [~\Desktop]
      PS> $p.GetType()IsPublic IsSerial Name                                     BaseType
      -------- -------- ----                                     --------
      True     False    Process                                  System.ComponentModel.Com…PS[BAT:84%][MEM:37.90% (12.02/31.70)GB][21:37:10]
      # [~\Desktop]
      PS> $p|gmTypeName: System.Diagnostics.ProcessName                       MemberType     Definition
      ----                       ----------     ----------
      Handles                    AliasProperty  Handles = Handlecount
      Name                       AliasProperty  Name = ProcessName
      NPM                        AliasProperty  NPM = NonpagedSystemMemorySize64
      PM                         AliasProperty  PM = PagedMemorySize64...
      


http://www.ppmy.cn/news/1459297.html

相关文章

linux学习笔记——硬盘原理以及linux中的sector与block

在计算机硬盘中&#xff0c;最小的存储单位叫做扇区sector&#xff0c;0.5kb&#xff0c;多个连续扇区组合在一起形成了块block&#xff0c;最小的块包含8个扇区&#xff0c;4kb 我们可以在linux中印证 创建一个新的文件2.txt&#xff0c;查看文件大小为0k 在文件中添加字符后…

[笔试训练](十八)

目录 052:字符串压缩 053:chika和蜜柑 054:01背包 052:字符串压缩 压缩字符串(一)_牛客题霸_牛客网 (nowcoder.com) 题目&#xff1a; 题解&#xff1a; 双指针模拟 class Solution { public:string compressString(string param) {int nparam.size();string ret;int num…

Middle for Mac:简洁高效的文本编辑软件

追求简洁与高效&#xff1f;Middle for Mac将是您文本编辑的最佳选择。这款Mac平台上的文本编辑器&#xff0c;以其独特的魅力和实用的功能&#xff0c;赢得了众多用户的喜爱。 Middle注重用户体验&#xff0c;采用简洁直观的界面设计&#xff0c;让您能够迅速上手并享受高效的…

IIS部署vue项目 IIS重写URL

【第一步】安装IIS {1&#xff09;打开控制面板 -> 打开程序和功能 -> 打开启用或关闭windows功能 &#xff08;2&#xff09;找到 Internet Information Services 勾选【web管理工具】和【万维网服务】&#xff0c;然后 确定 【第二步】安装URL重写模块 1). 安装URL …

【Web前端】jquery_json

1.jquery 1.1jquery简介 jquery是一个快速、简洁的javascript框架&#xff0c;于2006年1月份发布。jquery设计的宗旨是"write less,domore"&#xff0c;倡导写更少的代码&#xff0c;做更多的事情。封装了javascript常用的一些功能代码&#xff0c;提供一种简便的j…

软件应用开发安全设计指南

1.1 应用系统架构安全设计要求 设计时要充分考虑到系统架构的稳固性、可维护性和可扩展性&#xff0c;以确保系统在面对各种安全威胁时能够稳定运行。 在设计系统架构时&#xff0c;要充分考虑各种安全威胁&#xff0c;如DDoS攻击、SQL注入、跨站脚本攻击&#xff08;XSS&…

【编程向导】Docker-常用命令

常用命令 管理命令 管理命令说明builder管理构建config管理配置container管理容器context管理上下文engine管理引擎image管理镜像network管理网络node管理 Swarm 节点plugin管理插件secret管理 Docker secretsservice管理服务stack管理 Docker stacksswarm管理 Swarm 集群sys…

我的创作纪念日1460天(4年)

机缘 作为一名技术爱好者&#xff0c;我最初成为创作者的初心源于对知识的渴望和对分享的热情。在参与多个实战项目的过程中&#xff0c;我积累了丰富的经验&#xff0c;这些经验不仅仅是代码和解决方案&#xff0c;更多的是对问题本质的理解和解决问题的思维方式。我意识到&a…