Types/OpenPackage/Match.ps1

<#
.SYNOPSIS
    Matches content in a package
.DESCRIPTION
    Matches content in a package.

    Takes two regular expression patterns:

    * A File Pattern
    * A Content Pattern

    If neither is provided, lists parts

    If only the file pattern is provided, lists parts the match the pattern.

    If both the file and content pattern are provided, outputs matches.
#>

param(
$FilePattern,
$ContentPattern
)

if ($ContentPattern -and $ContentPattern -isnot [Regex]) {
    $ContentPattern = [Regex]::new($ContentPattern,'IgnoreCase', '00:00:05')
}

if (-not $this.GetParts) { return }

:nextPart foreach ($part in $this.GetParts()) {
    if ($filePattern -and ($part.Uri -notmatch $FilePattern)) {
        continue nextPart
    }
    if (-not $ContentPattern) {
        $part
        continue nextPart
    }

    $partStream = $part.GetStream()

    if (-not $partStream ) { continue }
    
    $partStreamReader = [IO.StreamReader]::new($partStream)    

    $partText = $partStreamReader.ReadToEnd()



    $partStreamReader.Close()
    $partStreamReader.Dispose()
    
    $partStream.Close()
    $partStream.Dispose()

    $ContentPattern.Matches($partText) |
        Add-Member NoteProperty Uri $part.Uri -Force -PassThru |
        Add-Member NoteProperty Package $this
}