Scripts and Logging

Scripting.  It’s a necessary skill in any deployment project.  Given the prolificity of scripting examples on the Internet nowadays, you don’t necessarily need to even know how to write a single line of code to be able to use it!

But what about when your script does something unexpected?  Well, that’s what log files are for, and the better the quality of the log files your scripts generate, the greater their value when things go wrong.  So when adding the logging code to your scripts, do yourself a favour and ensure that they adhere to a predefined format – that way you’ll be able to take advantage of the colour coding feature of many log parsing tools, such as the “Configuration Manager Trace Log Tool” which is (in my opinion) the best one of them all.  And that’s where this blog post comes to your aid!  With little effort at all you can ensure that your scripts will generate these correctly formatted log files.

The below VBS code starts by creating a log file with the same name and location as the script itself, but with the “.log” extension (the location of this log file is easy to modify).  It then proceeds to append text entries to the end of the log file through the use of a call to a Sub procedure, “LogEntry”.  By calling LogEntry() and passing it the required information, the log file will be immediately updated.

Option Explicit

‘DECLARATIONS——————————————————–
Const FOR_APPENDING = 8
Dim strFilename, objFS, objTS, sTime, sDate, sTempMsg, Component, iType

‘Setting up log file for append - if it doesn’t exist, create it.
strFilename = Replace(Replace(WScript.ScriptFullName, “.vbs”, “.log”), “.wsf”, “.log”)
Set objFS = CreateObject(“Scripting.FileSystemObject”)
If objFS.FileExists(strFilename) = False Then objFS.CreateTextFile(strFilename)
Set objTS = objFS.OpenTextFile(strFileName, FOR_APPENDING)

‘SCRIPT BODY———————————————————
Call LogEntry(“NORMAL: This is my test log example - 1″, “1″)
Call LogEntry(“WARNING: This is my test log example - 2″, “2″)
Call LogEntry(“ERROR: This is my test log example - 3″, “3″)
‘END OF SCRIPT BODY————————————————–

‘Script complete, closing log file
objTS.Close

‘PROCEDURES———————————————————-
Sub LogEntry(StringToLog, strType)

sTime = Right(“0″ & Hour(Now), 2) & “:” & Right(“0″ & Minute(Now), 2) & “:” & Right(“0″ & Second(Now), 2) & “.000+000″
sDate = Right(“0″& Month(Now), 2) & “-” & Right(“0″ & Day(Now), 2) & “-” & Year(Now)
sTempMsg = “<![LOG[" & StringToLog & "]LOG]!><time=”"” & sTime & “”" date=“”" & sDate & “”" component=“”" & strType & “”" context=“”"” type=“”" & strType & “”" thread=“”"” file=“”" & “” & “”">”

objTS.WriteLine(sTempMsg)

End Sub

The first parameter that LogEntry() expects is the text to be written as the error message.  The second parameter is the key to the coloured highlighting and is a number between 1 and 3; the number defines what colour (Normal, Warning or Error) that the log parsing tool will use to display the line.

The below example shows how the log file generated in the above code looks in the log parser tool:

 

To get started just copy and paste the above code into a text file and replace the code between the indicated lines with your own code.  Then call LogEntry with your own values whenever you want to write an event to your log file.  When your script runs your log file will be automatically generated!

Of course, if your script is running as part of an MDT Task Sequence, then all of the above can be achieved through a single solitary line of code!  I’ll explain this though in a future blog post.

About these ads

One thought on “Scripts and Logging

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s