Tag Archives: Scripting

PowerShell Toolmaking Dos and Don’ts

During the last Microsoft MVP Summit, Channel 9 invited MVPs into their studios to record short presentations on anything they wanted. This was too good a deal to pass on, so my friend Greg Shields and I jumped into the studio to talk about PowerShell Toolmaking. To be more accurate, Greg, who knows a little PowerShell and is probably like many of you,  “interviewed” me about some toolmaking best practices.  The video is now available on Channel 9. Or you can watch it here.

Enjoy!

Creating Class Based PowerShell Tools Demos

PUGS_logoDuring my recent trip to Sweden, I had an opportunity to do a presentation for the PowerShell User Group Sweden. It was a big turn out and I appreciate everyone who attended.  My talk was on using the new class feature in PowerShell 5.0 to create new types of PowerShell tools. Classes were introduced to make it easier to create DSC resources, but there’s no reason we can’t use them for other purposes.

I started with a quick intro into classes and then walked through a series of demos created a new tool using PowerShell classes. I have posted my demo files on GitHub. There were no slides.  The demo files are for educational purposes only and the final result is by no means production worthy, nor does it represent everything you could do.

The files are open to anyone interested. If you have questions on them, feel free to leave a comment.  And even though they are on Github, this isn’t an active project. I will probably revise these files as I use them in other presentations. Otherwise, enjoy and learn.

The Power of Custom Properties

The other day fellow PowerShell MVP Adam Bertram published an article about using custom properties with Select-Object. It is a good article in that it gets you thinking about PowerShell in terms of objects and not simple text. But I want to take Adam’s article as a jumping off point and take his ideas a bit further. I’m going to use Adam’s same example as a learning tool. Don’t get distracted by other ways to get the same information. The process and techniques are what matter here.

Whenever I’m working with PowerShell, I’m always thinking about how I can use this at scale.  How can I get this same information for 10 or 100 servers? And of course,  at this point I need to make sure I include a computername in the results.  First, I’ll try something with a single computer.

image

Close but not quite. Get-CimInstance is writing multiple objects to the pipeline.  The server in question has 2  8GB sticks of memory which is what you see in the output. I need something more along Adam’s original idea to that this becomes 16GB.

What I really want is the Sum property from Measure-Object and to that I need to add a Computername property. I’ll turn things around a bit.

image

This works because I’m using the common PipelineVariable parameter introduced in PowerShell 4.  What happens is that the pipeline output from Get-CimInstance is stored in a variable, pv, which I can access later in the expression. In my case I’m defining a new property for the computername using $pv and adding it to the selected output from Measure-Object.

However, if I try this for multiple computer names, I don’t get the expected result.

image

That’s because I’m adding up the physical memory instances from all servers, which isn’t really what I want. Instead, this is a situation where I have to process each computer individually.

image

One thing to be careful of when using the ForEach enumerator is that you can’t pipe the output to another cmdlet like Export-CSV, unless you explicitly save the results to a variable.

Then you can pipe $data to other cmdlets. You can use ForEach-Object although it might be little harder to follow.

But this makes it easier if you need to pipe the output to something else.

image

To wrap this up let’s go all out and define a few more custom properties.

image

Even though I’m selecting a few properties from the output of Measure-Object, I’m defining several others which are calculated on the fly. There is so much you can do with this technique,  but if I lost you anywhere please let me know.