functions/common/Invoke-LoggedTask.ps1

function Invoke-LoggedTask {
    param(
        [ScriptBlock]$Command,
        [string]$LogFilePath,
        [string]$CommandText = $null
    )

    $startTime = Get-Date
    $timestamp = $startTime.ToString("HH:mm:ss")

    if (-not $CommandText) {
        $commandText = ($Command.ToString().Trim() -replace '\s+', ' ')
    } else {
        $commandText = $CommandText
    }

    $logLine = "[$timestamp] $commandText"
    Write-Host $logLine

    & $Command

    $duration = (Get-Date) - $startTime
    "(Dauer: $duration) $logLine`n" | Out-File -FilePath $LogFilePath -Append -Encoding utf8
}