Log-ScriptEvent Function

This function will write a line of data to a script log file in a format that can be parsed by Trace32.exe / CMTrace.exe

 
 
 
 
 
4 Star
(1)
2,014 times
Add to favorites
3/17/2016
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • The module produces error about the stream in case you opened the log in CMTRACE
    2 Posts | Last post March 28, 2016
    • Additionally to the Posts above, the function is sometimes blocked from writing if you have opened the log-file in cmtrace.
      
      My solution here is to use the following line:
      Out-File -InputObject $LogLine -Append -NoClobber -Encoding Default -FilePath $LogfileName
      
      instead of:
      Add-Content -Path $LogfileName -Value $LogLine 
      
      Thanks for the good work, I use it in a few scripts.
    • Thanks for that, Olaf :)
  • you don't report time zone, can't be used in production
    7 Posts | Last post January 22, 2015
    • you don't report time zone, can't be used in production
      
      you rename parameters in a bad way, it's messy and misunderstanding
      
      function name does not respect powershell standard (try Add-TextToLog instead)
      
      context is not reported in trace32, but file is mandatory
      
      => by the way thank for you good work, thank again i downloaded and use your file after a little editing
    • #==================================================================================
      #    Function 	:	Add-TextToLog 
      #    Usage 		:	print log file in append mode and Trace32 format
      #    Inputs		:	message, component, message type, logfile path
      #    Outputs	:	- (print log file in append mode)
      #
      #	 msgType=	1	display as normally line 
      #	 msgType=	2	display as warning (yellow line)
      #	 msgType=	3	display as error (red line)
      #
      #==================================================================================
      Function Add-TextToLog 
      { 
          Param( 
                [parameter(Mandatory=$true,HelpMessage="message text to log")] 
                [string]$msg,
       
                [parameter(Mandatory=$true,HelpMessage="function source")]  
                [string]$component,
      
                [parameter(Mandatory=$true,HelpMessage="message type : 1 - Information, 2 - Warning, 3 - Error")]  
                [ValidateRange(1,3)] 
                [int]$type,
      
                [parameter(Mandatory=$true,HelpMessage="source file")]  
                [string]$file,
      
                [parameter(Mandatory=$true,HelpMessage="destination file to append text")]  
                [string]$logpath
                ) 
          
          # get time zone
          $datetime = New-Object -ComObject WbemScripting.SWbemDateTime 
          $datetime.SetVarDate($(Get-Date))
          $strUTC = $datetime.Value
          $UTC = $strUTC.Substring(21, $strUTC.Length - 21);
      
          #Create the line to be logged 
          $text = "<![LOG[$($msg)]LOG]!><time=`"$(Get-Date -Format HH:mm:ss.mmmm)$($UTC)`" date=`"$(Get-Date -Format M-d-yyyy)`" component=`"$($component)`" context=`"$([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)`" type=`"$($type)`" thread=`"$([Threading.Thread]::CurrentThread.ManagedThreadId)`" file=`"$($file)`">"
      
          # APPEND the line to log file 
          Add-Content -Path $logPath -Value $text 
      }
      
      Add-TextToLog -msg "ENDING SUCCESS [functionName]" -component [functionName] -type 1 -file [ScriptName] -logpath "C:\test\log.log" 
    • Hey, freakydinde,
      
      Thanks for the feedback, dude. Interestingly, one of my colleagues in the SCCM practice had already pointed out the UTC offset issue... I just never got round to fixing it! I've borrowed some of your code and added it to my own - hope you don't mind?!
      
      This function was always intended to be built upon by any interested individuals from the community, as I'm not a SCCM chap, so thank you for your input. I've included reference to your version on my main page.
      
      Point taken re: PowerShell function naming standards, but I had my reasons for using Log-ScriptEvent :)
      
      Take it easy,
      
      Ian.
       
    • after have been outside any coding rules for many years, in those day i'm becoming a naming convention extremist ;)
      
      today i finished that function, which will be soon used in production, code in next message (too long)
      
      
    • <#
      .Synopsis
      Log File writer
      
      .Description
      Append text the log file in Trace32 format, date, time (utc / milliseconds), thread id and context are added to the message
      
      .Parameter msg
      the message you want to be logged, ex : "FUNCTION START"
      
      .Parameter component
      function source from which come this message, ex : "Set-RegKey"
      
      .Parameter type
      message type : 1 - Information, 2 - Warning (yellow line), 3 - Error (red line), ex : 1
      
      .Parameter file
      powershell script source from which come this message, ex : "myscript.ps1"
      
      .Parameter logpath
      destination file to append text, ex : "C:\path\name.log"
      
      .Example
      Write-TextToLog -msg "ENDING SUCCESS myfunctionName" -component "myfunctionName" -type 1 -file "myscriptname" -logpath "C:\path\name.log"
      #>
      Function Write-TextToLog
      { 
          Param( [parameter(Mandatory=$true,Position=0)][string]$msg,
                 [parameter(Mandatory=$true,Position=1)][string]$component,
                 [parameter(Mandatory=$true,Position=2)][ValidateRange(1,3)][int]$type,
                 [parameter(Mandatory=$true,Position=3)][string]$file,
                 [parameter(Mandatory=$true,Position=4)][string]$logpath ) 
          
          # get time zone
          $datetime = New-Object -ComObject WbemScripting.SWbemDateTime 
          $datetime.SetVarDate($(Get-Date))
          $strUTC = $datetime.Value
          $UTC = $strUTC.Substring(21, $strUTC.Length - 21);
      
          # Create the line to be logged 
          $text = "<![LOG[$($msg)]LOG]!><time=`"$(Get-Date -Format HH:mm:ss.mmmm)$($UTC)`" date=`"$(Get-Date -Format M-d-yyyy)`" component=`"$($component)`" context=`"$([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)`" type=`"$($type)`" thread=`"$([Threading.Thread]::CurrentThread.ManagedThreadId)`" file=`"$($file)`">"
      
          # APPEND the line to log file 
          Add-Content -Path $logPath -Value $text
          Write-Host $msg
      }
    • Both the original function and the one in the comments has the incorrect date format. It should be: $(Get-Date -Format HH:mm:ss.fff). You have to use fff at the end instead of mmmm. f gets a fraction of a second. m gets minutes.
    • Nice one, mmash!
      
      Updated accordingly.
      
      Ian.