Write Log - PPOSHGROUP/PPoShTools GitHub Wiki

Write-Log

PPoShTools -> Write-Log.ps1

Synopsis

Writes a nicely formatted Message to stdout/file/event log.

Syntax

Write-Log [-Error] [-Warn] [-Info] [-_debug] [-Emphasize] [-NoHeader] 
[-Message] <String[]> [[-Indent] <Int32>] [-PassThru] [<CommonParameters>]

Description

It uses optional $LogConfiguration object which describes logging configuration (see LogConfiguration.ps1). It can be set using Set-LogConfiguration function.

Parameters

-Error<SwitchParameter> (default: False)

If specified, an error will be logged.

  • DefaultValue: False
  • PipelineInput: false
  • Required: false

-Warn<SwitchParameter> (default: False)

If specified, a warning will be logged.

  • DefaultValue: False
  • PipelineInput: false
  • Required: false

-Info<SwitchParameter> (default: False)

If specified, an info message will be logged (default).

  • DefaultValue: False
  • PipelineInput: false
  • Required: false

-_debug<SwitchParameter> (default: False)

If specified, a debug message will be logged.

  • DefaultValue: False
  • PipelineInput: false
  • Required: false

-Emphasize<SwitchParameter> (default: False)

If set, the message at console will be emphasized using colors.

  • DefaultValue: False
  • PipelineInput: false
  • Required: false

-NoHeader<SwitchParameter> (default: False)

If specified, header information will not be logged (e.g. '[ERROR]: (function_name)').

  • DefaultValue: False
  • PipelineInput: false
  • Required: false

-Message<String[]>

Message to output.

  • PipelineInput: true (ByValue)
  • Required: true

-Indent<Int32> (default: 0)

Additional indent.

  • DefaultValue: 0
  • PipelineInput: false
  • Required: false

-PassThru<SwitchParameter> (default: False)

If enabled, all log output will be available as return value (will use Write-Output instead of Write-Host).

  • DefaultValue: False
  • PipelineInput: false
  • Required: false

Examples

Example 1

Write-Log -Info "Generating file test.txt."

[I] 2017-05-19 11:14:28 [hostName/userName]: (scriptName/commandName/1) Generating file test.txt.

Example 2

Set-LogConfiguration -LogLevel Warn

Write-Log -Info "Generating file test.txt." Write-Log -Error "Failed to generate file test.txt." [E] 2017-05-19 11:14:28 [hostName/userName]: (scriptName/commandName/1) Failed to generate file test.txt.

Example 3

Set-LogConfiguration -LogFile 'log.txt'

Write-Log -Info "Generating file test.txt." Logs message to stdout and log.txt file.

Example 4

Set-LogConfiguration -LogEventLogSource 'MyApplication' -LogEventLogThreshold Warn

Write-Log -Warn "Failed to generate file test.txt."

⚠️ **GitHub.com Fallback** ⚠️