Functions/Tracing/Write-HostAndLog.ps1
<#
.SYNOPSIS Display message and output to file. #> function Write-HostAndLog { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [String] $Message, [Parameter(Mandatory = $false)] [String] $MethodName, [Parameter(Mandatory = $False)] [ConsoleColor] $ForegroundColor, [Parameter(Mandatory = $False)] [switch] $NoNewline, [Parameter(Mandatory = $False)] [switch] $NoTimeStamp, [Parameter(Mandatory = $False)] [ValidateSet("VERB", "INFO", "WARN", "FAIL", "SUCCESS")] [String] $Level = "INFO" ) begin { } process { if ($NoTimeStamp -eq $false) { $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"; $Message = "[$timestamp]`t$Message"; } if ($null -eq $ForegroundColor) { $ForegroundColor = [ConsoleColor]::White; } else { if ($ForegroundColor -eq [ConsoleColor]::Red) { $level = "FAIL"; } elseif ($ForegroundColor -eq [ConsoleColor]::Green) { $level = "SUCCESS"; } elseif ($ForegroundColor -eq [ConsoleColor]::Yellow) { $level = "WARN"; } } $logMessage = "[$level] - $Message"; if ($NoNewline -eq $false) { $logMessage += [Environment]::NewLine; } $logMessage | Out-File -FilePath $Global:LogFilePath -Encoding utf8 -Append -Force; if ($level -eq "VERB") { Write-Verbose $Message; } elseif ($level -eq "INFO") { Write-Host $Message -ForegroundColor $ForegroundColor -NoNewline:$NoNewline; } elseif ($level -eq "SUCCESS") { Write-Host $Message -ForegroundColor $ForegroundColor -NoNewline:$NoNewline; } elseif ($level -eq "WARN") { Write-Host $Message -ForegroundColor Yellow -NoNewline:$NoNewline; } elseif ($level -eq "FAIL") { Write-Host $Message -ForegroundColor Red -NoNewline:$NoNewline; } } end { } } Export-ModuleMember -Function Write-HostAndLog -Alias *; |