Skip to main content

Powershell – Log like you mean it


How do you do logging in powershell? Why should you do logging? What should you log? Where do you put your log? How do you remove your log? How do you search your log? All important questions and how you answer then depends upon what your background is like and the preferences you have. This will be a 2 part blog post and this is part 1.


Why should you log?


Well it is not mandatory, however I have 2 reasons:

  1. Help with debugging a script/module/function
  2. Self documenting script/module/function

Firstly; Do you know any program that does not contain any bugs? Working with IT for the last 2 decades, I cannot name one. When you create scripts/modules/functions, you will create bugs, that is where they live and try to make your life a living mess.

Secondly: Adding a little extra information to your logging will make them self documenting. Do you like writing documentation? Well I normally am not fond of it and use logging while debugging to get two birds with one stone.


What should you log?

Anything you like is the short and sweet answer. When troubleshooting something less is less and more is more. Detailed logging is indispensable when you are looking for a clue or reason for an error or wrong output, especially if you did not write the darn thing. For me it is also a question of context. Small scripts normally kind of limits it self from the “rest”. Nested functions, a script file with a handful of functions or a full-blown module require maybe a little more from your logging design.


What information you choose to log, is entirely up to you. Bear in mind that the information should give useful input to any person troubleshooting your work. Sometimes it is difficult, however it as a rule of thumb I usually do excessive logging.

In powershell there is a rule of conduct that states; “ You should not force your logging on your users” or something to that effect. Meaning the user of your creation should have the option to turn off your logging if it is not required. This of course relates to the console and not logging to files or other providers you may have available.


Where do you put your log?

Where ever you want. You may have a look if there is any corporate rule or procedure for where logs should be stored for future reference. There may also be rules regarding to for how long you should keep you logs or when a log should be shipped to an archive. If your corporate policy does not exist, you should be a good boy/girl and put some thought into this and perhaps suggest some guidelines to the paper pushers just for reference.


Use anything or what you are comfortable with. Just consider portability and where your script/module/function will live in the future. Will you always have access to a special database?


How do you search your log?

Well in what format is your log? If you use pure text and save it to a file, you have to search the file for some text that should give you a clue. Kind of obvious, however you get what you give.


If you want to treat your logging as objects in powershell, you have to create them first, store them for safe keeping and serialize them back to objects when you retrieve them, otherwise they will just be pure flat text files. Well unless you use ConvertFrom-String in WMF 5 which has a template approach to parsing pure text files.



How I do logging

Usually I create a function called Write-Verbose since I need to provide verbose logging to the script/function any way. When we do this, we use a little Powershell trick that overrides the cmdlet Write-Verbose. When Powershell tries to resolve your command, it will always first try to find a function with that name and invoke it. Here is an example:


The last line is the call to the "real" Write-Verbose cmdlet prefixed with it's namespace Microsoft.Powershell.Utility. This way, we can now write functions/scripts like this:


Cheers

Tore

Comments

  1. Good Stuff. I used a similar approach but with full proxy functions (accepts every write-* option) and nlog. I turned it into a module you can grab here if you wanted to take it for a spin. https://github.com/zloeber/NLogModule

    ReplyDelete
  2. Hi, glad you liked it. I will take a look at it, thank you!

    ReplyDelete

Post a Comment

Popular posts from this blog

Monitoring Orchestrator runbook events from Operations Manager

Today I will follow up on my colleague’s post Mr ITblog (Knut Huglen) about monitoring Orchestrator Runbook events.  He has build a nice double up SNMP loopback feature that does self monitoring in Orchestrator resulting in entries written to a special Windows Eventlog. Now we need to raise alerts in SCOM when one of his runbooks fails or sends a platform event, who knows there could be trouble lurking in his paradise.

We are not going to do anything fancy, however these are the steps we will be focusing on today:
Create a Management Pack for our customizations Create rules that collects the events from the orchestrator serverOff we go then and fire up the SCOM console and a powershell window. First we create a MP, I am going to use powershell to do this, however you may use the SCOM console as well (Administration – ManagementPacks – Action: Create Management Pack):



Import the Management Pack into SCOM and move on to the Authoring section in the SCOM console. Create a new rule:



Give the…

Build your local powershell module repository - ProGet

So Windows Powershell Blog released a blog a couple of days ago (link). Not too long after, a discussion emerged about it being to complicated to setup. Even though the required software is open source (nugetgalleryserver), it looks like you need to have Visual Studio Installed to compile it. I looked into doing it without visual stuidio, however I have been unable to come up with a solution. I even tweeted about it since I am not an developer. Maybe someone how is familiar with “msbuild” could do a post on how to do it without VS.

Anyhow one of my twitter-friends (@sstranger) came to the rescue and pointed me in the direction of ProGet, hence the title of this post. ProGet comes in 2 different licensing modes
Free (reduced functionality)Enterprise (paid version with extra features)The good news is that the free version supports hosting a local PowershellGet repository which was my intention anyway. So off we go and create a Configration that can install ProGet for us. This is the conf…