personio/functions/log_function.ps1
2024-07-26 19:37:06 +02:00

56 lines
1.5 KiB
PowerShell

function Write-Log {
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[Alias("LogContent")]
[string]$Message,
[Parameter(Mandatory=$false)]
[Alias('LogPath')]
[string]$Path="$ScriptDir\log\$($LOGDATE)$($ini.general.logfile)",
[Parameter(Mandatory=$false)]
[ValidateSet("Error","Warn","Info")]
[string]$Level="Info",
[Parameter(Mandatory=$false)]
[switch]$NoClobber
)
Begin {
# Zeige Fehler in Konsole an
# $VerbosePreference = 'Continue'
}
Process {
$FormattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
switch ($Level) {
'Error' {
Write-Error $Message
$LevelText = "[$($OWNPID)] ERROR:"
}
'Warn' {
Write-Warning $Message
$LevelText = "[$($OWNPID)] WARNING:"
}
'Info' {
Write-Verbose $Message
$LevelText = "[$($OWNPID)] INFO:"
}
}
# Schreibe Log in $Path
if (!(Test-Path $Path)) {
"$FormattedDate $LevelText $Message" | Out-File -Encoding "UTF8" -FilePath $Path
} else {
"$FormattedDate $LevelText $Message" | Out-File -Encoding "UTF8" -FilePath $Path -Append
}
}
End {
}
}