KeDo_MiniHelper.psm1

$FunctionScriptName = "MiniHelper"
Write-Verbose "Import-Start| [$($FunctionScriptName)]"


# Just an easy way to add PSObject to an array
function Add2Arr($Property) { New-Object PSObject -Property $Property }

# Used for normal output - can be changed here to anything thats needed for automation and reporting #? Implemented as a last resort due to ever changing env requirements of output...
function Write-Out($in) { 
    switch ($env:OutputSwitch) {
        "Host" { Write-Host $in }
        "Output" { Write-Output $in }
        "Verbose" { Write-Verbose $in }
        "Debug" { Write-Debug $in }
        "Warning" { Write-Warning $in }
        "Error" { Write-Error $in }
        "None" {}
        default { Write-Output $in }
    }
}
function Write-VOut($in) { 
    switch ($env:VerboseSwitch) {
        "Host" { Write-Host $in }
        "Output" { Write-Output $in }
        "Verbose" { Write-Verbose $in }
        "Debug" { Write-Debug $in }
        "Warning" { Write-Warning $in }
        "Error" { Write-Error $in }
        "None" {}
        default { Write-Verbose $in }
    }
} 
function Write-DOut($in) { 
    switch ($env:DebugSwitch) {
        "Host" { Write-Host $in }
        "Output" { Write-Output $in }
        "Verbose" { Write-Verbose $in }
        "Debug" { Write-Debug $in }
        "Warning" { Write-Warning $in }
        "Error" { Write-Error $in }
        "None" {}
        default { Write-Debug $in }
    }
} 
# Replace unwanted characters from variable
function PrettyVar($in, $Filter = $env:PrettyRegex, $Replace = '') { ($in -replace $Filter, $Replace).trim() } # [^a-zA-Z ,.0-9-]

# Replace all except AlphaNum from var
function AlphaNumVar($in, $Filter = '[^a-zA-Z0-9]', $Replace = '_') { ($in -replace $Filter, $Replace).trim() } # [^a-zA-Z0-9]

# Replace Umlaute
function ReplaceUmlaute($in) { ($in.Replace('ö', 'oe').Replace('ä', 'ae').Replace('ü', 'ue').Replace('ß', 'ss').Replace('Ö', 'Oe').Replace('Ü', 'Ue').Replace('Ä', 'Ae').trim()) }

# Get first value of array that is not $null - returns $null if not exist || Input should be sorted accordingly
function FirstNotNull ($in) { 
    if (($in | Measure-Object).Count -gt 0 ) { 
        $values = ($in | Where-Object { $null -ne $_ })
        if ($values.count -eq 1) { $values } else { $values[0] }
    } else { $null }
}

# Simple unified log entry with timestamp
function LogEntry {
    param(
        [Parameter()]$Entry,
        [Parameter()]$LogFile,
        [Parameter()][switch]$NoMessage,
        [Parameter()][switch]$ShowMessage,
        [Parameter()][switch]$NoReturn,
        [Parameter()][switch]$NoPrefix,
        [Parameter()][switch]$NoFile
    )
    # Create logentry
    $date = Get-Date -Format o
    
    if (!$WorkStep) {
        $WorkStep = "NoWorkStep"
    }
    
    if (!$NoPrefix) {
        $value = "[$date | $WorkStep] $entry"
    } else {
        $value = $entry
    }
    
    #* OUT
    # Host message
    if ((!$EnvValues.NoLogEntryDisplay -and !$NoMessage) -or ($EnvValues.NoLogEntryDisplay -and $ShowMessage)) {
        Write-Host $value
    } 

    # Create File
    if (!$NoFile) { 
        # Set filepath
        
        if ($Logfile) {
            $LogFile = $LogFile
        }
        
        if (!$LogFile -and !$DefaultLogFile -and ($BackupConfig.id -and $BackupWorkPath)) {
            # No directly or default defined file - Creating backup default
            $LogFile = $BackupWorkPath + "\" + $BackupConfig.id + "@JOBLOG.log"
        } elseif (!$LogFile -and !$DefaultLogFile -and ($RunbookIdentifier -and $BackupWorkPath)) {
            # No directly or default defined file - Creating backup default fallback
            $LogFile = $BackupWorkPath + "\" + $RunbookIdentifier + "@JOBLOG.log"
        } elseif (!$LogFile -and $DefaultLogFile) {
            # No directly defined file - Using defined default
            $LogFile = $DefaultLogFile
        } elseif (!$LogFile) {
            Write-Warning "No Logfile available!"
        }
        # Create file
        if ($LogFile) {
            $value | Out-File -Path $LogFile -Append
        }
    }
    
    # Return
    if (!$NoReturn) { return $value } 
}

Export-ModuleMember -Function *
Write-Verbose "Import-END| [$($FunctionScriptName)]"