Private/Write-Log.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
Function Write-Log { [CmdletBinding()] param ( [parameter()][ValidateNotNullOrEmpty()][String] $Message = "", [parameter()][int] $Severity = 1, [parameter()][string] $LogFile = '', [parameter()][switch] $ShowMsg ) switch ($Severity) { 1 { $Category='Info' } 2 { $Category='Warning' } 3 { $Category='Error' } } $MsgTxt = "$(Get-Date -f 'yyyy-M-dd HH:mm:ss') $Category $Message" if (![string]::IsNullOrEmpty($logfile)) { $MsgTxt | Out-File -FilePath $LogFile -Append -NoClobber -Encoding Default -Force } if ($showmsg) { switch ($Severity) { 3 { Write-Host $Message -ForegroundColor Red } 2 { Write-Host $Message -ForegroundColor Yellow } 1 { Write-Host $Message } } } else { Write-Verbose $MsgTxt } } |