Yet another Using statement

If I have seen further it is by standing on ye sholders of Giants.
— Isaac Newton

I haven’t been blogging for a while and to get back on a track I’ve decided to start with something simple.

Have you ever found a code sample online which shows, you how to do something useful? In my case, it usually ends being improved in some way over original concept. I call this “standing on the shoulders of giants”, hence the quote in the beginning.

So by this post, I’m opening a new series of notes where I’ll be writing about something that I’ve found online and ended (hopefully) improving on.

What’s exactly is a Using?

Using is a C# statement that helps you to ensure that the object is disposed as soon as it goes out of scope, and it doesn’t require explicit code to ensure that this happens. Please note that there is also a using directive which has its PowerShell counterpart since v5.0. It allows to indicate which namespaces are used in the session. This is not the using we’re looking for.

Continue reading

Advertisements

Visualizing PowerShell pipeline

A picture1 is worth a thousand words.

Occasionally, I see people having issues while trying to understand how PowerShell pipeline is executed. Most of them have no problems when Begin/Process/End blocks are in the single function. And if in doubt, I can always point them to the Don Jones’ The Advanced Function Lifecycle article. But when multiple cmdlets are chained into the one single pipeline, things become a little less clear.

Consider this example.

function Use-Begin {
    Begin {
        Write-Host 'Begin'
    }
}

function Use-End {
    End {
        Write-Host 'End'
    }
}

Let’s try to pipe one function into another:

PS C:\Users\beatcracker> Use-Begin | Use-End

Begin
End

So far, so good, nothing unexpected. The Begin block of the Use-Begin function executes first, and the End block of the Use-End function executes last.

But what happens if we swap the functions in our pipeline?

Continue reading

$PSBoundParameters, Pipeline and the ValueFromPipelineByPropertyName parameter attribute

While using and abusing $PSBoundParameters in one of the my functions, I’ve encountered some strange and unexpected behaviour. If you pass an object via pipeline to a function which accepts parameters from an object properties using ValueFromPipelineByPropertyName, Powershell re-binds $PSBoundParameters only for existing object properties. If object property is not exists in the next pipeline loop, $PSBoundParameters will keep the key from the previous iteration.

Continue reading

Splatting and mandatory parameters

Splat them. For the PowerShell knows those that are His own.

Splatting is very useful PowerShell technique that allows to greatly simplify calls to a functions or cmdlets. Basically, instead of passing arguments one by one, you create a hashtable where keys named as arguments and their values are passed to the function.

I use splatting extensively. Usually I have one big hashtable, where all the settings for the current script are stored. The hashtable itself is read from INI file, which are in my opinion provide optimum balance between hardcoding settings into the script itself and storing them in XML files. So, when I’ve to call a function in my script, I just splat this hashtable on it and PowerShell binds values from the hashtable to the function parameters. Well, at least this should be so in a perfect world.

Continue reading