PowerShell Logging – The Basics

PowerShell Logging will be the best thing you learn.  The biggest topic I get asked for help outside the specific activities I am assigned to is PowerShell.    I will be the first to understand everyone has their own style.  I never judge the style or syntax format.  Besides that, PowerShell logging ALWAYS needs to occur.  If you are not logging, your running blind. PowerShell logging is the one the most powerful features you can learn and implement.  

PowerShell does have some native abilities, “Start-Transcript“, and if that is all you want to understand, that is fine.   But, this post will take you through some basic understandings on capturing your outputs in a format that is readable and standard.  With some tricks in creating some great logic around managing your log.

PowerShell Logging – The Setup

The first step into your new logging endeavors is to understand the ability to call a function to simplify your process. 

For this function to become usable we first must set the variable “$logpath” so the function can understand where it is writing to.   My standard is to log to “%SystemDrive%\Windows\Temp”

Calling the Function

During your process of creating your script you will want to call the PowerShell logging function to say your going to run a process, log the process file call, its arguments, other module calls, and most important capturing any error output. 

PowerShell Logging – Catching the Exception

When starting any process, module, function, or block you should wrap it in “try and catch”.  This will ensure you script never exits unexpectedly and you always capture what is happening.  This is what I have found to be the easiest way to handle most scenarios. 

PowerShell Logging – Using the Error Variable

A option for logging functions is with the built in “cmdlet” switch “-ErrorVariable”.  This parameter captures the error of the function you are running.  To test use the following example. 

PowerShell Logging – Capturing the Exit Code

When you run “Start-Process” you can capture the exit code when you assign it to a variable with the “-wait” switch included.   This helps when you need to capture non-failure exit codes. For example the “3010” exit  code means there is a “Soft Reboot” required.  This exit code is not a failure.  

Bringing It All Together

The below script block brings the above examples together.  Calling the process through a try and catch loop and checking the exit code. 

PowerShell Logging – SCCM Task Sequence

If your responsibilities have you running a lot of processes in a SCCM task sequence environment the following code block will be most helpful.  This will allow you to log to the same directory the SCCM task environment is logging to. The below script block outlines loading the COM object to test if you are in such an environment and gather the directory where logging is occurring. 

When the Microsoft Deployment Toolkit (MDT) is integrated, and utilizing the “Copy Logs on Error” function, these logs will be copied to the log monitoring directory.  

PowerShell Logging with a Process

When developing a process that runs on a schedule you will want to manage the file.  The below script block shows how to parameter the log location and size of the file; with applying a header to a new log file.

PowerShell Logging to the Event Viewer

When developing workflows, services, or schedule tasks you can use the system’s event viewer to capture your necessary output.  Utilizing the system’s Event Viewer with PowerShell Logging will allow you to monitor the process with a tool like System Center Operations Manager, Microsoft Operations Manager Suite, or any other log analytic system. 

With All That Being Said

When developing your next PowerShell script or process; I hope this post has outlined some necessary tools or ideas that will ultimately lead to more PowerShell logging.  Just having this in tool box has saved me countless of hours sifting through command windows and debugging the script.  

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.