PoshCode Archive  Artifact Content

Artifact 40e8bd9a0f78255f9fa4b188a4d152dd321e328c560b18e48055b82b663d6879:

  • File Write-Log-System-Center.ps1 — part of check-in [7f2003e53c] at 2018-06-10 13:29:27 on branch trunk — Writes log files in a System Center format, readable by Trace32. I haven’t yet tested functionality with CMTrace, but based on what I can find, it should work. (user: John Bruckler size: 2904)

# encoding: ascii
# api: powershell
# title: Write-Log System Center
# description: Writes log files in a System Center format, readable by Trace32. I haven’t yet tested functionality with CMTrace, but based on what I can find, it should work.
# version: 0.1
# type: function
# author: John Bruckler
# license: CC0
# function: Write-Log
# x-poshcode-id: 3687
# x-archived: 2016-08-16T11:25:06
# x-published: 2013-10-11T12:53:00
#
#
function Write-Log
{
  <#
    .Synopsis
      Write a message to a log file in a format compatible with Trace32 and Config Manager logs.
    .Description
      This cmdlet takes a given message and formats that message such that it's compatible with
      the Trace32 log viewer tool used for reading/parsing System Center log files.
      
      The date and time (to the millisecond) is determined at the time that this cmdlet is called.
      Several optional arguments can be provided, to define the Component generating the log
      message, the File that is generating the message, the Thread ID, and the Context under which
      the log entry is being made.
    .Parameter Message
      The actual message to be logged.
    .Parameter Component
      The Component generating the logging event.
    .Parameter File
      The File generating the logging event.
    .Parameter Thread
      The Thread ID of the thread generating the logging event.
    .Parameter Context
    .Parameter FilePath
      The path to the log file to be generated/written to. By default this cmdlet looks for a
      variable called "WRITELOGFILEPATH" and uses whatever path is there. This variable can be
      set in the script prior to calling this cmdlet. Alternatively a path to a file may be
      provided.
    .Parameter Type
      The type of event being logged. Valid values are 1, 2 and 3. Each number corresponds to a 
      message type:
      1 - Normal messsage (default)
      2 - Warning message
      3 - Error message
  #>
  [CmdletBinding()]
  param(
    [Parameter( Mandatory = $true )]
    [string] $Message,
    [string] $Component="",
    [string] $File="",
    [string] $Thread="",
    [string] $Context="",
    [string] $FilePath=$WRITELOGFILEPATH,
    [ValidateSet(1,2,3)]
    [int] $Type=1
  )
  
  begin
  {
    $TZBias = (Get-WmiObject -Query "Select Bias from Win32_TimeZone").bias
  }
  
  process
  {
    $Time = Get-Date -Format "HH:mm:ss.fff"
    $Date = Get-Date -Format "MM-dd-yyyy"
    
    $Output  = "<![LOG[$($Message)]LOG]!><time=`"$($Time)+$($TZBias)`" date=`"$($Date)`" "
    $Output += "component=`"$($Component)`" context=`"$($Context)`" type=`"$($Type)`" "
    $Output += "thread=`"$($Thread)`" file=`"$($File)`">"
    
    Write-Verbose "$Time $Date`t$Message"
    Out-File -InputObject $Output -Append -NoClobber -Encoding Default -FilePath $FilePath
  }
}