src/utility/Search-Items.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function Search-Items {
    [CmdletBinding(PositionalBinding = $true)]
    Param(
        [Parameter(Mandatory = $true)]
        [String]$Path,

        [Parameter(Mandatory = $true)]
        [String[]]$Pattern,

        [Parameter(Mandatory = $false)]
        [String[]]$Include,

        [Parameter(Mandatory = $false)]
        [String[]]$Exclude,

        [Parameter(Mandatory = $false)]
        [int]$Depth = 15,

        [switch]$Recurse
    )

    [hashtable[]]$Results = Get-ChildItem $Path -Include $Include -Exclude $Exclude -Depth $Depth -Recurse:$Recurse.IsPresent -PipelineVariable Fi -File | `
        Get-Content | `
        Select-String -Pattern $Pattern -PipelineVariable Ss | `
        ForEach-Object -Process {
        $Result = @{
            Item        = $Fi
            MatchedLine = $Ss.Line
            Match       = $Ss.Matches[0].Value.Trim()
        }
        $Result
    }

    $Results
}