Skip to main content

Write-Log

SYNOPSISโ€‹

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

SYNTAXโ€‹

Write-Log [-Message] <String[]> [[-Severity] <Int16>] [[-Source] <String>] [[-ScriptSection] <String>]
[[-LogType] <String>] [[-LogFileDirectory] <String>] [[-LogFileName] <String>] [[-MaxLogFileSizeMB] <Decimal>]
[[-WriteHost] <Boolean>] [[-ContinueOnError] <Boolean>] [-PassThru] [-DebugMessage]
[[-LogDebugMessage] <Boolean>] [<CommonParameters>]

DESCRIPTIONโ€‹

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

EXAMPLESโ€‹

EXAMPLE 1โ€‹

Write-Log -Message "Installing patch MS15-031" -Source 'Add-Patch' -LogType 'CMTrace'

EXAMPLE 2โ€‹

Write-Log -Message "Script is running on Windows 8" -Source 'Test-ValidOS' -LogType 'Legacy'

EXAMPLE 3โ€‹

Write-Log -Message "Log only message" -WriteHost $false

PARAMETERSโ€‹

-Messageโ€‹

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

Type: String[]
Parameter Sets: (All)
Aliases: Text

Required: True
Position: 1
Default value: None
Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False

-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)

Type: Int16
Parameter Sets: (All)
Aliases:

Required: False
Position: 2
Default value: 1
Accept pipeline input: False
Accept wildcard characters: False

-Sourceโ€‹

The source of the message being logged.

Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: 3
Default value: $([String]$parentFunctionName = [IO.Path]::GetFileNameWithoutExtension((Get-Variable -Name 'MyInvocation' -Scope 1 -ErrorAction 'SilentlyContinue').Value.MyCommand.Name); If ($parentFunctionName) {
$parentFunctionName
}
Else {
'Unknown'
})
Accept pipeline input: False
Accept wildcard characters: False

-ScriptSectionโ€‹

The heading for the portion of the script that is being executed. Default is: $script:installPhase.

Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: 4
Default value: $script:installPhase
Accept pipeline input: False
Accept wildcard characters: False

-LogTypeโ€‹

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

Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: 5
Default value: $configToolkitLogStyle
Accept pipeline input: False
Accept wildcard characters: False

-LogFileDirectoryโ€‹

Set the directory where the log file will be saved.

Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: 6
Default value: $(If ($configToolkitCompressLogs) {
$logTempFolder
}
Else {
$configToolkitLogDir
})
Accept pipeline input: False
Accept wildcard characters: False

-LogFileNameโ€‹

Set the name of the log file.

Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: 7
Default value: $logName
Accept pipeline input: False
Accept wildcard characters: False

-MaxLogFileSizeMBโ€‹

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

Type: Decimal
Parameter Sets: (All)
Aliases:

Required: False
Position: 8
Default value: $configToolkitLogMaxSize
Accept pipeline input: False
Accept wildcard characters: False

-WriteHostโ€‹

Write the log message to the console.

Type: Boolean
Parameter Sets: (All)
Aliases:

Required: False
Position: 9
Default value: $configToolkitLogWriteToHost
Accept pipeline input: False
Accept wildcard characters: False

-ContinueOnErrorโ€‹

Suppress writing log message to console on failure to write message to log file. Default is: $true.

Type: Boolean
Parameter Sets: (All)
Aliases:

Required: False
Position: 10
Default value: True
Accept pipeline input: False
Accept wildcard characters: False

-PassThruโ€‹

Return the message that was passed to the function

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: 11
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-DebugMessageโ€‹

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

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: 12
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-LogDebugMessageโ€‹

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

Type: Boolean
Parameter Sets: (All)
Aliases:

Required: False
Position: 13
Default value: $configToolkitLogDebugMessage
Accept pipeline input: False
Accept wildcard characters: False

CommonParametersโ€‹

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTSโ€‹

System.Stringโ€‹

The message to write to the log file or output to the console.โ€‹

OUTPUTSโ€‹

Noneโ€‹

This function does not generate any output.โ€‹

NOTESโ€‹

https://psappdeploytoolkit.com