PowerShell equivalent of LINQ Any()?

前端 未结 11 678
灰色年华
灰色年华 2020-12-01 03:29

I would like to find all directories at the top level from the location of the script that are stored in subversion.

In C# it would be something like this

         


        
11条回答
  •  情话喂你
    2020-12-01 04:00

    I recommend the following solution:

    <#
    .SYNOPSIS 
       Tests if any object in an array matches the expression
    
    .EXAMPLE
        @( "red", "blue" ) | Where-Any { $_ -eq "blue" } | Write-Host
    #>
    function Where-Any 
    {
        [CmdletBinding()]
        param(
            [Parameter(Mandatory = $True)]
            $Condition,
    
            [Parameter(Mandatory = $True, ValueFromPipeline = $True)]
            $Item
        )
    
        begin {
            [bool]$isMatch = $False
        }
    
        process {
          if (& $Condition $Item) {
              [bool]$isMatch = $true
          }
        }
    
        end {
            Write-Output $isMatch
        }
    }
    
    # optional alias
    New-Alias any Where-Any
    

提交回复
热议问题