Public/Find-Know.ps1

# ? TITEL Find-Know
# ? DESCRIPTION Wissen in dieser PowerShell-Datenbank finden
# ? TAGS Wissen PowerShell
# ? VERSION 2020.01.14

#region Prolog
using namespace System
using namespace System.Runtime.InteropServices
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
Set-StrictMode -Version Latest
#endregion

#region Cmdlet Definition

function Find-Know {
    <#
        .SYNOPSIS
            PowerShell-Wissen im AKPT-Module finden
        .PARAMETER Keyword
            Ein oder mehrere Keywords die im Titel, der Beschreibung, der Tags und im Inhalt (s. -IncludeContent) gesucht wird.
        .PARAMETER IncludeContent
            Bei der Suche wird der Inhalt einbezogen.
    #>

    [Alias("fk")]
    [CmdletBinding()]
    param (
        [string[]]$Keyword,
        [switch]$IncludeContent
    )

    Get-ChildItem -Path "$AkptModulePath" -Recurse -Include "*.ps1", "*.psm1", "*.psd1", "*.md" | 
        Where-Object -Property FullName -NotMatch -Value "Einarbeiten" -PipelineVariable file |
        Get-Content -Encoding UTF8 -TotalCount 4 -ReadCount 4 | 
        ForEach-Object -ErrorAction Stop -Process {
            if($_[0] -notmatch "TITEL" -or $_[1] -notmatch "DESCRIPTION" -or $_[2] -notmatch "TAGS" -or $_[3] -notmatch "VERSION") {
                $PSCmdlet.WriteWarning("Die Datei $($file.FullName) enthält keinen korrekten Prolog.")
                return
            }
            [PSCustomObject]@{
                Titel        = [String]($_[0]    -cReplace "^(?'Key'^#\s\?\s*TITEL\s)"       , [string]::Empty)
                Beschreibung = [String]($_[1]    -cReplace "^(?'Key'^#\s\?\s*DESCRIPTION\s)" , [string]::Empty)
                Tags         = [String[]](($_[2] -cReplace "^(?'Key'^#\s\?\s*TAGS\s)"        , [string]::Empty) -split ' ')
                Version      = [Version]($_[3]   -cReplace "^(?'Key'^#\s\?\s*VERSION\s)"     , [string]::Empty)
                FileName     = [String]$file.Name
            }
        }  | 
        Where-Object -FilterScript {
            $pattern = "($($Keyword -join "|"))"
            $_.Titel -IMatch $pattern `
                -or $_.Beschreibung -IMatch $pattern `
                -or ($_.Tags -join ";") -IMatch $pattern `
                -or $_.FileName -IMatch $pattern `
                -or ($IncludeContent -and ($file | Select-String -Pattern $pattern -Quiet))
    }
}

#endregion

#region Epilog

New-Alias -Name fk -Value Find-Know -Force

#endregion