PowerShell: how to grep command output?

前端 未结 7 1339
温柔的废话
温柔的废话 2021-01-30 08:27

In PowerShell I have tried:

alias | select-string Alias

This fails even though Alias is clearly in the output. I know this is beca

7条回答
  •  佛祖请我去吃肉
    2021-01-30 08:39

    For a more flexible and lazy solution, you could match all properties of the objects. Most of the time, this should get you the behavior you want, and you can always be more specific when it doesn't. Here's a grep function that works based on this principle:

    Function Select-ObjectPropertyValues {
        param(
        [Parameter(Mandatory=$true,Position=0)]
        [String]
        $Pattern,
        [Parameter(ValueFromPipeline)]
        $input)
    
        $input | Where-Object {($_.PSObject.Properties | Where-Object {$_.Value -match $Pattern} | Measure-Object).count -gt 0} | Write-Output
    }
    

提交回复
热议问题