Building A Hypervisor Independent Script – Part 2

Last time we covered getting the information we care about out of the various PowerShell toolkits, and what that looks like. If you are just joining us, or want to refresh your knowledge, the first part in the series is here.

This time we’re going to cover the Pipeline, and the “Begin” and “Process” code blocks. Lets dive right in.

The Pipeline

The pipeline is what puts the “Power” into PowerShell. Well, maybe not 100% of what does that, but PowerShell is insanely stronger for it. Microsoft provides a stellar introduction to the pipeline and its associated concepts over here. So we’ll not spend too much time repeating it. I’ll wait while you read it.

Now that you’re comfortable with the pipeline, lets talk about how you get your script to take pipeline input.

Begin { }

What better place to begin things than at the beginning. The begin code block is where you put things (bits of code, variables, functions) that you want executed BEFORE the pipeline execution is passed to the body of the script. I use it for variables that need to be defined prior to the script getting underway, and for custom properties as you’ll soon see.

This is what a begin block looks like. In here we’ve defined two expressions, “XenHost_UUID” and “VMHost_UUID”. These are what will actually return our UUID properties form each VM when we put the rest of this script together.

We’re progressing quick tonight, aren’t we? Now on to:

Process { }

When using the Begin/Process setup, This is where all of the real action in the script occurs. Or at least, it’s where the logic and flow take place. This is also where the information from the pipline gets fed into the body of the scrip and the action starts to get good.

download | new post

The first thing we do is to identify the object we’re being fed from the pipeline, and making sure it is something that we’re concerned about. Once we know that, we then fill the $output variable with data from our expressions above, or throw an error.

Good stuff, right? There is also an End {} block, talked about next.

End { }

The End { } block. It is well, the end. It’s where all of your clean up code, and formatting, and the like go. Really, that’s all there is to this block.

We can now wrap this post up. Remember to review the Intro post, subscribe (via RSS) to be notified of the next post “Putting it together”.

If you have any comments, please feel free to drop a line in the comments. Or leave us a note on Twitter

blog comments powered by Disqus