Categories
Functions Powershell

No need for excuses – Lets get logging

Hi there folks!
I want to share with you one of my favorite functions I’ve ever written: Write-Info.

Now this function is not the fanciest, nor is it the longest or the most technically advanced.
But this function has probably saved me the most amount of time and it’s just so nice to have.

Whether you are scripting changes to thousands of mailboxes, removing unused files on a network share or building a complicated Powershell tool with a graphical user interface (GUI), logging of information is important and can save your butt if a mistake is made or someone simply changes their mind…

But having to write the message to the console and then to a log file, and to add the date and keep track of errors and updating a status bar in the GUI… It gets tiresome rather quickly and you end up not doing it or just doing some of it.

This is where Write-Info steps in and saves the day!

At its current iteration, Write-Info can write to console (Write-Host), write to log file (Out-File), write to status bar and to a rich text box!
But that’s not enough, you can also choose fore color on the text for both the console and rich text box!
You can choose to write the information as three different categories Error, Warning or just the default Normal.
In the log file, date and time, computer name, username and category is added to each row providing even more information automagically.

All log files are by default added to the “$Global:CurrentSessionsLogs” variable which you can use to output the logs with for example Out-GridView to easily show the logs for the current session to the end user of your Powershell tool.
Exceptions to that can easily be made by using the switch “$ExcludeFromCurrentSessionLogs”.
If you want to use multithreading, this variable can be named $Global:sync.CurrentSessionsLogs instead and still work with Write-Info.

You can set the “$LogName” variable at the beginning of your script and Write-Info will automagically catch that and use as the log file name (exceptions can be made by giving a new log name when calling the function, eg. ‘Write-Info “hey there” -LogName “AnotherLogFile”‘).

When using the Error switch, the text forecolor is automatically set to red and the logged file is created in \Log\Errors instead of \Log\.
It also attaches $_.exception.message to the end of your error message.
With this you can add your own error message but still provide the error message provided by Powershell.

Ok that sounds great, but what if I don’t have a \Log\ directory you ask?
Write-Info creates its own file structure so that you do not have to worry about that!
On top of that it creates the directories as hidden for that extra stealth.

Lets look at some examples!

Simple Try Catch example with Write-Info using -Error
$LogName = "TechMeAwayTutorial"
 Try{
     Get-ChildItem .\FolderThatDoesNotExist -ErrorAction Stop
 }
 Catch{
     Write-Info "Failed to get the childitem!" -Error
 }
Simple Try Catch example with Write-Info using -Warning
Simple If Else statement with Write-Info, with -TextColor Green

Using the $Global:CurrentSessionLogs functionality requires that you initialize the variable at script start, I recommend that you do something along the lines of:

$Global:CurrentSessionLogs = @()
$Global:CurrentSessionLogs += "Script Started" | Select @{ n = "Date" ; e = { Get-Date -f "yyyy-MM-dd HH:mm:ss" } }, @{ n = "Category" ; e = { "NORMAL" } }, @{ n = "Text" ; e = { $_ } }

Or if you want it to support multithreading:

$Global:sync.CurrentSessionLogs = @()
$Global:sync.CurrentSessionLogs += "Script Started" | Select @{ n = "Date" ; e = { Get-Date -f "yyyy-MM-dd HH:mm:ss" } }, @{ n = "Category" ; e = { "NORMAL" } }, @{ n = "Text" ; e = { $_ } }

Now this might look a bit complicated or annoying but this function is primarily for bigger tools and can be ignored if not wanted.

Writing to a status bar or rich text box is very easy. Write-Info will look for $statusbar and if it exists, set $statusbar.Text.
Rich text box can be specified with the parameter -AlsoWriteToRTB or by setting up the variable: “$WriteInfoRichTextBoxObject”

Where can I get Write-Info?

I’ve uploaded it to my GitHub which you can find here: https://github.com/Primycha/Write-Info

I hope this gives you a good understanding of what Write-Info is and how it could help you.
I know it has helped me a lot and I hope it can help you too!

If something is left unanswered or you have any questions please feel free to comment and let me know!

Leave a Reply

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