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’…
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…
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.
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.