Standardize your verbose/debug messages

So my default verbose and debug messages might not really be good looking, so I needed to standardize how I wrote them. I wanted the time, function name and message to be printed and standardized. So I came up with this invoke way.

How do I execute a script block from a variable?

Well first we need to save the standard to a variable and then execute it when needed. Well that is easy in powershell.

That command first will save the script block to be run in the $command variable and then using the invoke operator runs it. If you run the later command again you will notice that the Get-Date is executed now to.

Building it from scratch

But we wanted to add the function name too. So lets look into that. I wrote an article before that talked about good constants in Powershell. These aren’t really constants but Powershell variables that powershell itself populates. Tada if you look in $MyInvocation.MyCommand.Name you will find the current functionname. So lets try that on the commandline:

Well I didn’t get any output.. Well I’m not running in a function am I? So lets  build a function and then throw it in.

Okey so that works. But remember that we will do another invoke later and that will get a new $MyInvocation. So lets add a message instead. That feels just like building a function, add a param and a variablename.

A pot hole

Hey.. why did I not get the correct output? Well you did.. In a way. MyInvocation does provide information about the current invocation, but you are invoking the script block right? So how do we get the MyInvocation from the function. We have to dig a little in powershell scoping. I can access the MyInvocation of the function by calling Get-Variable -Scope 1 MyInvocation -ValueOnly. So what is that scope 1? That tells the cmdlet to walk up 1 level in the invocation stack and get the variable there, in this case the calling function.

Now all at once

[Read more…]