Let’s Log it with PowerShell

IT Administrators use PowerShell to automate everyday tasks, create reports and deploy applications and settings to endpoints. Logging is a best practice for the majority of the scripts we deploy. It can be used to provide debug information to them, document the execution flow in a file or simply give information about anything. Although it is really important for the above reasons, many ignore it.

In this post two quick and easy logging approaches will be discussed, the fast one and the structured.

Now everyone is thinking what is structured and fast approach. Let’s clarify this below.

Fast Approach

The easiest way to incorporate a logging mechanism into a script is by using the Start-Transcript cmdlet. By using this, a record of all or part of a PowerShell session is written in a text file. There are various parameters that can be used with Start-Transcript and can be found in the official documentation here.

The script to start logging is shown below:

Start-Transcript -Path "C:\Users\Public\logs.txt" -Append -ErrorAction Stop

Write-Host "$(Get-Date) This is a test logging"

Stop-Transcript

! Don’t forget to stop the transcript with the Stop-Transcript cmdlet.

The above block code starts a new transcript at a specified location in a specified file and records every output of the script. This means that every Write-Host etc. will produce an output to this file.

The output of the above is shown below:

As we can see a lot of “useless” information is produced without reason. That is why this is the fast and dirty way to create a log file.

Structured Approach

In this approach a more structured solution is provided. A function is used that displays the wanted message to the console and writes it to the log file.

$LogFilepath = "C:\Users\Public\log2.txt"

Function LogAndConsole($message,$LogFilepath,$level) {
    if ($level -eq "information") {
        $currenttimestamp = Get-Date -format u
        $message = "[" + $currenttimestamp + "] " + "INFO: " + $message
        Write-Host $message -ForegroundColor Green
        $message | Out-File $LogFilepath -Append
    }
    if ($level -eq "error") {
        $currenttimestamp = Get-Date -format u
        $message = "[" + $currenttimestamp + "] " + "ERROR: " + $message
        Write-Host $message -ForegroundColor Red
        $message | Out-File $LogFilepath -Append
    }
}

LogAndConsole -message "test error" -LogFilepath $LogFilepath -level "error"
LogAndConsole -message "test info" -LogFilepath $LogFilepath -level "information"

In order to use the above code in our scripts, we have to declare the desired log file output location ($LogFilepath) and call the LogAndConsole function passing as parameters the message, the location of log file and the log level (information or error -> can be extended accordingly).

The outcome of the execution of the above script is shown below:

Console

Log file

These are two possible ways of logging into a PowerShell script. The fast and dirty way for everyday script and the structured way for serious deployments.

Leave a Reply

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