(Edited 11/30/15 – Completely forgot SqlChow’s contributions! Sorry!)
Wow – two blog posts in two days! A record for me. So today’s post I wanted to try something new. I wanted to start sharing some of my code via a download over this WordPress CMS. Well, that’s not the only reason, I do like to share stuff so if this helps someone out, leave a comment, etc.
So the very first download I want to do was a very useful module that I hobbled together that I used in some of my automated PowerShell scripts — a logging module!
The download below contains my Log_Module_v1.psm1 file. Obviously, remove the .txt extension to use it.
Before I go on, I hobbled this code together based on two other programmers that were kind enough to post their code:
Luca Sturlese – 9to5IT.com
SqlChow – SqlChow’s Blog
Brenton Keegan – GitHub
So big props for those guys sharing their code. Also, this is my first attempt at a module and I put it together in early 2015. Since then Luca has come out with a new version of his logging module and I think I want to take a look at that before I start cleaning up this code. — yeah, this code is in need of clean up.
So, basically to use the module, import it into PowerShell just like any other module.
I put this together because I wanted to use a logging class in PowerShell. Unfortunately, creating classes in PowerShell is a little weird. Take a look at the code or search Google – you’ll see. Also, the class has a NumOfArchives parameter. It’s defaulted to 10. This will allow you to supply one file name, and the code will automatically rename any older log files. Absolutely great for automated PowerShell scripts on a server – a lot less overhead in maintenance of log files.
Here is some example code to get up and running:
$logFileName = $scriptInfo.Path + '\' + $scriptInfo.Name + '.log'
Switch-LogFile -Name $logFileName
$hlog = New-LogFile ($scriptInfo.Name, $logFileName)
If you have any questions, comments, suggestions, leave a comment below. Also, as with any code I post, use at your own risk. I don’t guarantee it working for your particular needs.