Creating a logging framework in PowerShell – The Final Part

In Part-1, we looked at why logging was important, established a set of guidelines or targets that we need to keep in mind while designing the framework and defined few variables that we will be using.

In Part-2, we expanded on the basics created a few more variables and created a function ‘Write-Log’, which as the name suggests writes messages to the log file.

In Part-3, we saw how we can manage the multiple log files that get generated using ‘Switch-LogFile’ function.

In Part-4, we saw how we can tie all the components needed (and, then some) for the logger by creating a new object and adding methods that will help us use the newly created object. Also, remember that it is the caller that should instantiate the Logger object and set file properties based on script name.

In this final part we will be seeing how we can use this new module to log our script actions. Before, we start off you need the resources. You can download the zip file containing the scripts here.

Our example, consists of 3 files ‘GrandParent_RunMe.ps1’, ‘Parent.ps1’, ‘Child.ps1’ and of course, the module file ‘PS-Log.psm1’. Let us see, what GrandParent_Runme.ps1 is doing:

	#You need to change the below to match the path where PS-Log module is stored.
	Import-Module D:\Programming\PS-Modules\PS-Log\PS-Log.psm1 -Verbose

	#Get the caller info.
	$scriptInfo = Get-ScriptInfo
	$logFileName = $scriptInfo.Path + '\' + $scriptInfo.Name + '.log'

	#manage your log files before you start writing to them.
	Switch-LogFile -Name $logFileName

	#if you do not call the Get-ScriptInfo method, we can set the log file name
	#using the SetLogFileName method for the object. This way we can have each
	#script called from here have its own log file.
	$gplog = New-LogFile ($scriptInfo.Name, $logFileName)

	$gplog.WritePSInfo("ScriptName = $($scriptInfo.Name.ToString())")
	$gplog.WritePSInfo("Starting script: GrandParent.ps1")
	& .\Parent.ps1
	$gplog.WritePSInfo("Exiting script: GrandParent.ps1")
	Remove-Module PS-Log

As you can see, the script is pretty straight forward, it prints a couple of messages and calls another script in this case ‘Parent.ps1’. Similarly, ‘Parent.ps1’ calls ‘Child.ps1’. ‘Child.ps1’ prints a couple of messages and we pop off the stack returning to ‘GrandParent_RunMe.ps1’. For the Grandparent, we need to edit import-module statement to match the location where the module is.

Let us see what happens when we run ‘GrandParent_RunMe.ps1’…

Powershell logging framework

GrandParent_RunMe First Run

As you can see in the image, we have a new file called ‘GrandParent_RunMe.log’ and all the scripts have logged their actions in this one log. Pretty cool!!

Let us see what happens when we run ‘GrandParent_RunMe.ps1’, again…

Powershell logging framework

GrandParent_RunMe.ps1 second run

As you can see the Switch-LogFile statement did what it was supposed to. It renamed the ‘GrandParent_RunMe.log’ from the frist run to ‘GrandParent_RunMe.001.log’ and creates a new log with script name which is ‘GrandParent_RunMe.log’. Let us check what is written into the log files created in both the runs.

Powershell logging framework.

Content from both log files

As we worked through the whole series, we defined a framework and created a module that follows the guidelines set by the framework. Also, this modules allows for a consistent way to log your script actions. Until next time.


By profession, I’m a SQL Server Database Administrator. I love to poke my nose into different corners and see how stuff looks in there. I keep looking for new things to do as my mind refuses to settle on one topic.

Tagged with: , , , ,
Posted in PowerShell
4 comments on “Creating a logging framework in PowerShell – The Final Part
  1. kiquenet says:

    The logging framework works using Powershell Remoting? thanks

    • SqlChow says:

      Sorry, for the delayed response. Have been away for a bit.

      To answer your question, frankly I do not know as I have not done much work on remoting.

  2. Selcselc says:

    Hi, can we use your module free of charge for production and commercial purposes? it is very well developed.

    • SqlChow says:

      Hi Sorry, for the delayed response. I have been travelling quite a bit lately.

      This code is free for you to read and learn from. I still want to add some features to the module, so until I’m done you can’t distribute it or modify it. I need to make sure that unfinished code does not get out and mess up a system by accident.

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: