Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.
Raymond Piller edited this page Aug 13, 2015 · 2 revisions

Write messages to a log file in CMTrace.exe compatible format or Legacy text file format.

Description

Write messages to a log file in CMTrace.exe compatible format or Legacy text file format and optionally display in the console.

Can also specify parameters via ${env:Write-Log} variable. Store variables in as JSON. More in the wiki.

Parameters

Message

The message to write to the log file or output to the console.

Severity

Defines message type. When writing to console or CMTrace.exe log format, it allows highlighting of message type. Options: 1 = Information (default), 2 = Warning (highlighted in yellow), 3 = Error (highlighted in red)

Source

The source of the message being logged. Default is from $MyInvocation: ScriptName:ScriptLineNumber

Component

The heading for the portion of the script that is being executed. Default is from $MyInvocation: Command {Arguments}

LogType

Choose whether to write a CMTrace.exe compatible log file or a Legacy text log file.

LogFileDirectory

Set the directory where the log file will be saved.

LogFileName

Set the name of the log file.

MaxLogFileSizeMB

Maximum file size limit for log file in megabytes (MB). Default is 10 MB.

WriteHost

Write the log message to the console.

ContinueOnError

Suppress writing log message to console on failure to write message to log file.

PassThru

Return the message that was passed to the function

DebugMessage

Specifies that the message is a debug message. Debug messages only get logged if -LogDebugMessage is set to $true.

LogDebugMessage

Debug messages only get logged if this parameter is set to $true in the config XML file.

LogDateFormat

Date format, as expected by Get-Date -Format. Default: 'yyyy-MM-dd'

LogTimeFormat

Time format, as expected by Get-Date -Format. Default: 'HH:mm:ss.fff'

Examples

Write-Log -Message "Installing patch MS15-031" -Source 'Add-Patch' -LogType 'CMTrace'
Write-Log -Message "Script is running on Windows 8" -Source 'Test-ValidOS' -LogType 'Legacy'

Notes

Originally taken from PSAppDeployToolkit.