WHAT

 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

 The severity of the logged line can be set as:

        1 - Information

        2 - Warning

        3 - Error

 

Warnings will be highlighted in yellow. Errors are highlighted in red.

 The tools to view the log:

   SMS Trace - http://www.microsoft.com/en-us/download/details.aspx?id=18153

   CM Trace - Installation directory on Configuration Manager 2012 Site Server - <Install Directory>\tools\

 

EXAMPLE

   Log-ScriptEvent c:\output\update.log "Application of MS15-031 failed" Apply_Patch 3

   This will write a line to the update.log file in c:\output stating that "Application of MS15-031 failed".

   The source component will be Apply_Patch and the line will be highlighted in red as it is an error (severity - 3).

 

OUTPUT

Here’s an image from a log file populated by the function:

 

 

CODE-SAMPLE 

PowerShell
Edit|Remove
<#       
THIS CODE-SAMPLE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED   
    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR   
    FITNESS FOR A PARTICULAR PURPOSE.  
     
This sample is not supported under any Microsoft standard support program or service.   
    The script is provided AS IS without warranty of any kind. Microsoft further disclaims all  
    implied warranties including, without limitation, any implied warranties of merchantability  
    or of fitness for a particular purpose. The entire risk arising out of the use or performance  
    of the sample and documentation remains with you. In no event shall Microsoft, its authors,  
    or anyone else involved in the creation, production, or delivery of the script be liable for   
    any damages whatsoever (including, without limitation, damages for loss of business profits,   
    business interruption, loss of business information, or other pecuniary loss) arising out of   
    the use of or inability to use the sample or documentation, even if Microsoft has been advised   
    of the possibility of such damages, rising out of the use of or inability to use the sample script,   
    even if Microsoft has been advised of the possibility of such damages.   
#>
 
LEGACY
 
VB Script
Edit|Remove
Sub Logger(strRemLogFile, nPhase, Msg, Svrty) 
             
    'eg Logger "\\halocm06\results$\w2k3_sp2\<server>.log", "999", "My message", 3'NB Svrty 1 = Info'NB Svrty 2 = Warning'NB Svrty 3 = ErrorDim objFSO, objLogFile, arrSvrty, SvrtyCheck, i 
             
    Const ForAppending = 8 
             
    arrSvrty = Array(1,2,3) 
    SvrtyCheck = ""For i = 0To UBound(arrSvrty) 
        IfCInt(arrSvrty(i)) = CInt(Svrty) Then 
            SvrtyCheck = "OK"ExitForEndIfNextIf SvrtyCheck <> "OK"Then 
        MsgBox "Unrecognised Severity in Logger Sub" & Chr(13) & Chr(13), 16, WScript.ScriptName 
    EndIfOnErrorResumeNextSet objFSO = CreateObject("scripting.FileSystemObject") 
    Set objLogFile = objFSO.OpenTextFile(strRemLogFile, ForAppending, True) 
    If Err.Number <> 0Then 
        MsgBox "Error (" & Err.Number & ") opening '" & strRemLogFile & "' for appending" & Chr(13) & Chr(13) & "Aborting!"16, WScript.ScriptName 
        Err.Clear 
        ExitSubElse 
        objLogFile.WriteLine "<![LOG[" & Msg & "]LOG]!>" & "<time=" & Chr(34) & Time & ".000+0" & Chr(34) & " date=" & Chr(34) _ 
        & Month(Now) & "-" & Day(Now) & "-" & Year(Now) & Chr(34) & " component=" & Chr(34) & WScript.ScriptName _ 
        & Chr(34) & " context=" & Chr(34) & Chr(34) & " type=" & Chr(34) & Svrty & Chr(34) & " thread=" _ 
        & Chr(34) & nPhase & Chr(34) & " file=" & Chr(34) & WScript.ScriptFullName & Chr(34) & ">"  
        objLogFile.Close 
    EndIfSet objLogFile = NothingSet objFSO = NothingEndSub
 
PS - have a look at freakydinde's version of this function in the Q & A section - it may be of more relevance to SCCM folks.