All documentation

PowerShell Custom Logger

Published November 20, 2022 Guide

As my PowerShell scripts began to adapt, the need to log output became a priority.

In this Article I will go over the usage of a custom PowerShell logger I created. You have find the project here: https://github.com/ctejeda/PowerShell-Logger

Clone the project and Import the script.

git clone https://github.com/ctejeda/PowerShell-Logger.git

Import the Module

Import-Module ./PowerShell-Logger/Invoke-Logger.ps1

You can simply call the Logger by issuing the following command:

Invoke-Logger -Message "This is a test" -Logfile "\path\to\logfile.log"

If you open up the Logfile.log you should see something similar to this:

The above would be the most basic use of the script. there are other flags you can use that are useful. Let's go over them.
-Message flag is a required flag to log output. you can combine static strings and variable like this: -Message "The user: $uservariable was updated successfully"

-Logfile flag is a required flag to store the data from message to a log file specified. You can declare the file in a variable like so: $Logfile = "c:\users\ctejeda\documents\logfile.log" and then call it later like so: Invoke-Logger -Message "The user: $uservariable" was updated successfully" -Logfile $logfile

-ShowOuput is an optional flag to show the log output. The output will be displayed in green.

This is ideal for a success message.

-ShowWarning is an optional flag to show the log output as a warning. The output will be displayed in yellow

This is ideal for a warning message

-ShowError is an optional flag to show the log output as a error. The output will be displayed in red.

This is ideal for erro messages. I use this a a lot with my Try {} Catch {}. Example
Try { Get-Aduser -Identity $uservariable } Catch {Invoke-logger -Message "Error trying to get user $uservariable Error showed: $_ " -ShowError}

Wrapping the logger module around a for-each action

This is where the module really becomes useful.

For this example, I created a test script to loop through a test directory I created where I have some files

Get-ChildItem -Path /Users/christophertejeda/testfolder | % {

    Import-Module "/Users/christophertejeda/PowerShell-Logger/Invoke-Logger.ps1"

    
    $logfile = "/Users/christophertejeda/logfile.log"
    $name = $_.name
    $size = $_.size
    if ($size -gt '2441472') {
    Invoke-Logger -Message "File $name is over 2441472 KBs. The size is $size" -LogFile $logfile -ShowOutput }
    else {Invoke-Logger -Message "File $name is under 2441472 KBs. The size is $size" -LogFile $logfile -showwarning}
    
    
    
    
    
    
    }

The above test script will Import our custom PowerShell Logger, loop through all files in the specified directory, log a success message if the file over 2441472 KBs, and log a warning message if the file is not over 2441472 KBs. here's how the output look on a terminal.

And the Logfile.log output

For error handling I modified the test script to include a Try & Catch

Get-ChildItem -Path /Users/christophertejeda/testfolder | % {

    Import-Module "/Users/christophertejeda/PowerShell-Logger/Invoke-Logger.ps1"

    
    $logfile = "/Users/christophertejeda/testfolder/logfile.log"
    $name = $_.name
    $size = $_.size
    try {
    if ($size -gt '2441472') {
    get-ite
    Invoke-Logger -Message "File $name is over 2441472 KBs. The size is $size" -LogFile $logfile -ShowOutput 
    }

    else {Invoke-Logger -Message "File $name is under 2441472 KBs. The size is $size" -LogFile $logfile -showwarning}
    }

    catch {Invoke-Logger -Message "Error occured while proccessing file $name. Error was $_" -LogFile $logfile -showError}
    
    
    
    
    
    
    }

The Results:

The logfile.log output

I hope this custom PowerShell logger helps you the next time you want to log your PowerShell scripts.