Today I thought I would share some thing that I have developed over the last year or so as I have been working more and more with PowerShell…. some standard functions that can simply be dot sourced and called at any time so that all my scripts have some awesome logs to go along with the awesome script.
Why have logging?
Well personally I feel that this is a very important part of a script or tool that is developed, particularly if it is going to be used regularly and provides some sort of valuable functionality.
Having a log that people can follow, and see what is happening while the script is executing or if the script runs into errors is very useful. Not only that, but I feel people will respect your developed scripts \ tools as they see it as a mature product, not some dodgey back-yard operation that was just copied from the web and had the crap hacked through it.
The other reason is that it makes troubleshooting a hell of a lot easier… particularly 6 – 12 months down the track when lets say you are running the script in a different environment than what it was originally developed for. Having a log and being able to write out the result of each step of your script allows you to easily pin-point where the problem lies, especially if you pipe out error messages to the log (both custom or system errors).
Another great reason is having compassion on the guy the might be having to update or troubleshoot your script \ tool in the future. Its sort of like having well commented code (which I know everyone does… lol), it makes it easier for someone to be able to track your script and know what is happening and why its happening.
Finally, the reason why I personally like logging is because the way I layout all of my scripts is a seperate function per major task that the script completes. At the end of my scripts I then have an execution section where I call all of my functions in order and pass the required variables.
Using logging with this approach to development is great because it allows you to break your log into sections… each function has its own mini log section if you like, so it makes tracking the script really really easy and plus it looks awesome.
Logging Function Library: The contents
Becuase of the reasons above, I decided to create a logging function library which is essentially just a .ps1 file with a bunch of standard log functions. In each of my scripts I just dot source the .ps1 file and then call the each of the functions as required. The functions included in logging library are:
- Log-Start: Creates the .log file and initialises the log with date and time, etc
- Log-Write: Writes a informational line to the log
- Log-Error: Writes an error line to the log (formatted differently)
- Log-Finish: Completes the log and writes stop date and time
- Log-Email: Emails the content of the log
Logging Function Library: Log Formatting
All of my scripts use this logging library and therefore all the logs look kinda the same. This is actually a good thing because it allows your user base to get used to a particular log format and so its just another incentive for them to use and enjoy your script that you worked so hard on.
Here is a screenshot of one of my logs that has been written using this logging function library
Logging Function Library: The Code
Ok guys, enough rampling… here it is in all of its raw beauty:
Logging Function Library: Installation Instructions
Here are the instructions to start using this logging function library in your scripts:
- Copy the code above and save it in a new .ps1 file called “Logging_Functions.ps1″. Alternatively you can download the file from here: PowerShell Logging Function Library
- Copy the file and store it in a central location that everyone has access to (Note: This is very important otherwise your script will fail for people who don’t have access to the function library)
- In your script dot source the Logging_Functions.ps1 file. You should do this at the top of your PowerShell script and it should look something like this:
- Call the relevant functions as required (see the examples found in the meta information of each of the functions on how to do this)
PowerShell Script Template
If you are unsure on how to do step 4 above, or you would like to fill-out a standard template that have everything you need to create a new PowerShell script, then check out this post: PowerShell Script Template
If you have any problems using my PowerShell Logging Function Library then please let me know in the comments below or send me an email and I will try my hardest to get you all fixed up. Also, if you have any cool ideas or have some thoughts on how to improve the library then I am always intersted in hearing what others are doing and how I can improve myself.
Thanks guys and happy scripting