Skip to content

Instantly share code, notes, and snippets.

@rodmhgl
Last active May 17, 2017 01:30
Show Gist options
  • Save rodmhgl/70ba6e968dba60d48249359ff920c9ab to your computer and use it in GitHub Desktop.
Save rodmhgl/70ba6e968dba60d48249359ff920c9ab to your computer and use it in GitHub Desktop.
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
}
@dotps1
Copy link

dotps1 commented Apr 20, 2017

[PSCustomObject]@{
    TimeStamp = $(Get-Date -Format MM/dd/yy-HH:mm:ss)
    MessageType = $MessageType.ToUpper()
    Message = $Message
} | Export-Csv -Path $LogFile -Delimiter $Delimiter -NoTypeInformation -Append

@rodmhgl
Copy link
Author

rodmhgl commented May 17, 2017

@dotps1 - Just now saw this, good call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment