简体   繁体   English

如何从PS输出中过滤出特定值

[英]How to filter out a particular value from PS output

I am running a PS command get-Keyproperty which is returning result in table format at below" 我正在运行PS命令get-Keyproperty ,它以下面的表格式返回结果”

Key                  Label                          Policy     Running  Required
---                  -----                          ------     -------  --------
abc                 UI                               on         True     False  
efg                 UI                               off        True     False 

I want to retrieve value of Policy which is on 我想检索已onPolicy

How to retrieve Policy value of abc Key ? 如何获取abc Key Policy值?

I am running get-Keyproperty | Select-Object abc 我正在运行get-Keyproperty | Select-Object abc get-Keyproperty | Select-Object abc but it is not working. get-Keyproperty | Select-Object abc但不起作用。

To get the value of Policy try to run 要获得Policy的价值,请尝试运行

Get-Keyproperty | Select-Object -ExpandProperty Policy

Even though the command above is the recommended way, you can also get the same result with (Get-Keyproperty).Policy . 即使上面的命令是推荐的方法,您也可以通过(Get-Keyproperty).Policy获得相同的结果。

If you want to check if the value matches "on" just do (Get-Keyproperty | Select-Object -ExpandProperty Policy) -eq "on" 如果要检查值是否与“ on”匹配,只需执行(Get-Keyproperty | Select-Object -ExpandProperty Policy) -eq "on"

If you want the Policy Value of a certain entry you can either: 如果您想要某个条目的Policy值,则可以:

  1. Choose the entry over its position in the table 在表中的位置上选择条目

    (Get-Keyproperty | Select-Object -ExpandProperty Policy)[0]

  2. Or by finding it over its value of Key 或者通过找到它的Key价值来找到它

    Get-Keyproperty | Where-Object {$_.Key -eq "abc" } | Select-Object -ExpandProperty Policy

    As mentioned in the comments, a shorter way of the same command is 如注释中所述,相同命令的一种较短方法是

    (Get-Keyproperty | ? Key -eq "abc").Policy

    ? is an alias ( Get-Alias ? ) and the {script block} is only neccessary for more complex expressions which then requires the $_.Key or $PSItem.Key (introduced in PSv3) notation; 是一个别名( Get-Alias ? )和{script block}仅用于更复杂的表达式neccessary然后需要$_.Key$PSItem.Key (在PSv3介绍)符号; which both are synonym for the current object in the pipeline . 两者都是管道中当前对象的同义词。 See Get-Help Where-Object . 请参阅Get-Help Where-Object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM