By: Mason Prewett

 

PowerShell is a powerful language to use when completing repetitive tasks on a system. I usually find myself needing to use PowerShell with the following common requirements:

  1. Completing large tasks that take a long time to process
  2. Needing logic to be processed on a schedule
  3. The processing of dynamic sets of data

All of these requirements make it important to analyze exactly what the script did after it is complete. This is where logging in your scripts comes in handy.

*Note – This article will only focus on logging to a text file.

Open Windows PowerShell ISE. Search the start menu if you have never opened this before. It is always a good idea to right click it and “Run as administrator” as well.

Let’s start off with the most basic log to file.

 
Running this code will log the message “This is my first log to file” to a text file located at C:PSLog.txt. The “>>” operator writes a single line to a text file. There is no need to create, open or close the file when writing to it, PowerShell takes care of this for you.

Run this code for a second time and you will notice it will append the same message on the next line of the text file.

I usually want a fresh log file for each code execution. Use the following code to delete the file if it exists and write to a brand new file.

 
Great! This is logging to a text file and ensuring that each execution overwrites the last.

I also like to add the date and time of every log each time one is written. This helps me understand how long each step is taking. Having to do this in the code above would be a little messy and not something I would want to have to do over and over. Let’s also look at making this a little more modular using a function.

 
C:PSLog.txt

6/30/2017 10:42:18 AM – Hello from a function call

6/30/2017 10:42:18 AM – This is easy to use

6/30/2017 10:42:18 AM – A lot cleaner too

This code allows us to simply call the function by name and add the log message as a parameter in a familiar PowerShell format: LogMessage -Message “Hello World”. Now the log file has the date and time automatically added before each message. This is because all we have to do is pass our function the message, and it handles adding the date and time each time using the (Get-Date).ToString code. This is looking a lot better for using in a PowerShell script that has to do a lot of logging.

*Note – Make sure to add the function definition before calls are made to it. This may appear to work if you run it more than once in ISE, but that is because it stores it in memory. If you schedule a script to run, the function will have to be defined before it is called or you will not get any logging.

Let’s take it a step further. This is a common operation that I could use in all of my scripts, so let’s make it even more modular using a separate logging script.

C:Logger.ps1

 
C:LogExample.ps1

 
Now we have a logging script that can be reused in any future scripts that are written. The “dot notation” (. “C:Logger.ps1) includes the content of an external script in a local script. It essential pastes the content of the script where it is used. To be clear, that is “dot space ‘file name’”. Additionally, I added another script locally that calls the function and provides the LogFile so I don’t have to do this every time.

 

Keep your data analytics sharp by subscribing to our mailing list!

Thanks for reading! Keep your knowledge sharp by subscribing to our mailing list to receive fresh Key2 content around data analytics, business intelligence, data warehousing, and more!

 
 
 
 
 


Key2 Consulting is a data warehousing and business intelligence company located in Atlanta, Georgia. We create and deliver custom data warehouse solutions, business intelligence solutions, and custom applications.