RelaxedIT.psm1


function Test-RelaxedIT
{
    write-host (Get-ColorText("[Test] ""RelaxedIT.module"" - optimized for pwsh7 :-)"))
}

function Get-ColorText {
    <#
    .SYNOPSIS
    Colors specific patterns in the input text. " ( nummber and dates "
 
    .DESCRIPTION
    The Get-ColorText function takes a string input and applies color formatting to specific patterns such as dates, digits, quoted text, and text within parentheses.
 
    .PARAMETER text
    The input text to be color formatted.
 
    .EXAMPLE
    $sampleText = "01/03/2025 [INF] This is a null ""sample text"" dbNull @varname with a date 2023-03-04 18:15 (komment) and a number 123. bollean true and false "
    Get-ColorText -text $sampleText
 
    .NOTES
    Author: Josef Lahmer
    Date: 5.3.2025
    #>

    param (
        [string]$text
    )

    # Define regex patterns
    $digitPattern = '\b\d+\b'
    $digitPattern = '\ \d+\b'
    $varPattern = '@\w+'
    $datePatternDDMMYYYY = '\b\d{2}\.\d{2}\.\d{4}\b'
    $datePatternYYYYMMDD = '\b\d{4}-\d{2}-\d{2}\b'
    $datePatternDDMMYYYY_slash = '\b\d{2}/\d{2}/\d{4}\b'
    $hourPattern = '\b\d{2}:\d{2}(:\d{2})?\b'
    $quotePattern = '\"[^\"]*\"'
    $darkGrayPattern = '\(.*?\)'
    $BracketPattern = '\[.*?\]'
    $bluePattern = '(?i)\b(true|false|null|DBNull)\b'


    # Replace patterns with colored text
    try {
        $text = [regex]::Replace($text, $darkGrayPattern, {param($match) "`e[90m$($match.Value)`e[0m"})  # DarkGray
        $text = [regex]::Replace($text, $BracketPattern, {param($match) "`e[90m$($match.Value)`e[0m"})  # DarkGray
        $text = [regex]::Replace($text, $datePatternYYYYMMDD, {param($match) "`e[32m$($match.Value)`e[0m"})   # Green for YYYY-MM-DD
        $text = [regex]::Replace($text, $datePatternDDMMYYYY_slash, {param($match) "`e[32m$($match.Value)`e[0m"})   # Green for DD/MM/YYYY
        $text = [regex]::Replace($text, $datePatternDDMMYYYY, {param($match) "`e[32m$($match.Value)`e[0m"})   # Green for DD.MM.YYYY
        $text = [regex]::Replace($text, $hourPattern, {param($match) "`e[32m$($match.Value)`e[0m"})   # Green for HH:MM:SS
        $text = [regex]::Replace($text, $digitPattern, {param($match) "`e[35m$($match.Value)`e[0m"})  # DarkMagenta
        $text = [regex]::Replace($text, $varPattern, {param($match) "`e[93m$($match.Value)`e[0m"}) # @variablename
        $text = [regex]::Replace($text, $quotePattern, {param($match) "`e[96m$($match.Value)`e[0m"})  # Cyan
        $text = [regex]::Replace($text, $bluePattern, {param($match) "`e[34m$($match.Value)`e[0m"})  # blue
        
    }
    catch {
        write-host "Get-ColorText ERROR: $text" -ForegroundColor Red
    }

    # Output the colored text
    return $text
}

function Get-ConfigfromJSON {
    <#
    .SYNOPSIS
        json array config file
    .DESCRIPTION
        use a json config file
    .NOTES
        Information or caveats about the function e.g. 'This function is not supported in Linux'
    .LINK
        Specify a URI to a help page, this will show when Get-Help -Online is used.
    .EXAMPLE
        # $config = Get-ConfigfromJSON -match "2" -config .\mandant.json
        # $config.ConfigValue
    #>

    param (
        [String]$config="config.json",
        [String]$id="id",
        [String]$match=""
    )

    # Read the JSON file
    $jsonobj = Get-Content -Path $config | ConvertFrom-Json

    # Find the object with the matching "anbieternr"
    if ($match -ne "")
    {
        $result = $jsonobj | Where-Object { $_.($id) -eq $match }
    }
    else {
        $result = $jsonobj
    }

    if ($result) {
        return $result
    } else {
        Write-customLOG -logtext ("No configuration found for $id : $match")
    }
}

function Write-customLOG
{
    [string]$logtext = ""

    write-host (Get-LogDateString) + " " + (Get-ColorText($logtext))
}

Function Get-LogDateString 
{
    [CmdletBinding()]
    param (
        [Parameter()]
        [datetime] $date = [datetime]::UtcNow
    )
    <#
    .SYNOPSIS
        #GET-LogDateString #get-date
    .DESCRIPTION
        gibt #z_templates standard schoen formatiertes datum innerhalb der logfiles zurück
    #>

    return ([datetime]::UtcNow).toString("yyyy-MM-dd HH:mm:ss U\tc")
}