Doing More with PowerShell Objects

Posted on September 9, 2015 by Jeff Hicks in PowerShell with 0 Comments

In “A PowerShell Tip for Selecting Data Easily,” I guided you from the process of working with objects in the pipeline. I want to bring that topic and scenario back up to explore a few more reasons why I think this is important. If you missed the first article, take a moment to get caught up so you’ll understand my examples.

In the original code sample, the author was creating a tabular separated file. That is, a CSV that uses a tab instead of a comma as the delimiter. This is something I see people struggle with all the time. They spend a lot of energy trying to create strings and saving them to a file when all they need to do is take some time to read PowerShell help and experiment with the Export-CSV cmdlet. This is a one line expression that creates practically the same result as the original, text-oriented code.

Personally, I would have stuck with the default comma delimiter, but as you can see it isn’t difficult to modify.

A tab delimited file (Image Credit: Jeff Hicks)

A tab delimited file (Image Credit: Jeff Hicks)

Let’s go a step further and use PowerShell to create a log for any running VM and start any that aren’t currently running.

As I’ve said before, this isn’t a Hyper-V tutorial. I just happen to be using Get-VM and Start-VM for my example. When I run this in the PowerShell ISE, I get the WhatIf message for the single VM that’s not running:

The WhatIf scenario (Image Credit: Jeff Hicks)

The WhatIf scenario (Image Credit: Jeff Hicks)

Sponsored

Otherwise, I create the text file. All of this works because I’m writing VM objects to the pipeline. In this case, I need to use PowerShell’s ForEach-Object cmdlet because I need to do something for each object in the pipeline depending on the status property. Next, I create an empty array in the begin scriptblock and add each matching VM object to it. In the end scriptblock, I select what I need, format it, and create the text file. If you can visualize the process, I think you’ll find this much easier.

More than likely, this is a process you want to repeat and you don’t want to have to type it every time, so you can copy and paste the commands into a script file.

I’ve added some parameters, including validation tests, error handling, and support for WhatIf. This makes it flexible and easy-to-use.

Using a PowerShell script to save typing

Using a PowerShell script to save typing (Image Credit: Jeff Hicks)

If you think of this script as a canned PowerShell session, you may be quite happy with the result. The script that I’ve come up with is essentially a playback of commands that you would have typed interactively with the added benefits of things like error handling. Because everything has been scripted, I’m also ensured of consistency. It doesn’t matter who runs the script. As long as they have they necessary credentials the script will execute the same every time for every user. This is a powerful benefit, often overlooked, when it comes to PowerShell automation.

Sponsored

All of that said, even though this works, fundamentally I think there’s a better approach to all of this, and I’ll go into that in more detail in another article.

Sponsored

Tagged with , , , ,