Last active
May 17, 2017 01:30
-
-
Save rodmhgl/70ba6e968dba60d48249359ff920c9ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Export-Log { | |
<# | |
.Synopsis | |
Writes tab-separated log messages to a specified file. | |
.DESCRIPTION | |
Writes tab-separated log messages to a specified file. | |
.EXAMPLE | |
Export-Log -messagetype ERROR -logfile c:\temp\log.csv -message "Encountered error performing operation." | |
Will log an error message to the log located in c:\temp\log.csv | |
.EXAMPLE | |
Export-Log -messagetype INFO -message "Successfully completed operation" | |
Will log an informational only message to the log located in the default location | |
.Parameter MessageType | |
Valid options are WARN, INFO, and ERROR | |
.Parameter LogFile | |
Defaults to the default $logfile parameter of the parent script | |
.Parameter Message | |
Message to log | |
#> | |
[cmdletbinding()] | |
param( | |
[ValidateNotNull()] | |
[ValidateNotNullOrEmpty()] | |
[ValidateSet("WARN", "INFO", "ERROR")] | |
[parameter(Mandatory=$true)] | |
[string]$MessageType, | |
[parameter(Mandatory=$true)] | |
[string]$Message, | |
[string]$LogFile=$script:LogFile, | |
$delimiter = "`t" | |
) | |
switch ($MessageType) { | |
'INFO' { | |
Write-Verbose -Message $Message | |
} | |
'WARN' { | |
Write-Warning -Message $Message | |
} | |
'ERROR' { | |
Write-Error -Message $Message | |
} | |
} | |
[PSCustomObject]@{ | |
TimeStamp = $(Get-Date -Format MM/dd/yy-HH:mm:ss) | |
MessageType = $MessageType.ToUpper() | |
Message = $Message | |
} | Export-Csv -Path $LogFile -Delimiter $Delimiter -NoTypeInformation -Append | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment