Skip to content

Instantly share code, notes, and snippets.

@rodmhgl rodmhgl/Export-Log.ps1
Last active May 17, 2017

Embed
What would you like to do?
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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Owner 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
You can’t perform that action at this time.