Testing PowerShell HashTables

So I’ve been watching the PowerShell Toolmaking Fundamentals course on Pluralsight authored by Adam Bertram.  You may be surprised that I watch other PowerShell related courses, but I always pick up something I didn’t know about, find a new teaching technique or something else that makes me say, “that was cool.” I have found a few of these in Adam’s course so far.

One of the tricks he demonstrated was using a hashtable as a parameter value for an advanced function that could then be splatted to Set-ADuser.  For the sake of what I want to demonstrate here’s my simplified version of such a function.

The function gets the specified user and then updates the user with hashtable of parameters from Set-ADUser.  If you know all the parameter names this works just fine.


I’ve easily updated the user account.


But what if I make a mistake with the hashtable of settings?


There is no parameter called FirstName for Set-ADUser. It should be GivenName. One thing you could do, and this is the point of this article,  is to  validate the hashtable keys against parameter names from Set-ADuser.

You can use Get-Command to list all of the parameter names.


What we need to do is make sure that all of the keys in the settings hashtable are in this list. Here’s a quick test.


I can use code like this to test if the keys from $s are also in $p:


Although I may be more interested in the cases where they don’t match.

This won’t give any results because nothing matches the filter. But if I modify the hashtable with a bogus entry it will.


With this concept in mind I can revise the function.


Certainly you could add other code to list the available parameters, suggest corrections or whatever. But now the function won’t attempt to run and gracefully handles bad keys.

Once corrected, the function works as expected.


And please don’t take any of this as an indication that Adam missed something in his course. Far from it.  No course can teach you absolutely everything you need to know to build effective PowerShell tools. You need to build what works for you and add error handling that you feel is appropriate. In this case I thought this would offer a nice learning opportunity for you to learn about hashtable keys and a few operators.

Computer Certificate tools

In my Pluralsight course on Advanced DSC I used a few functions I wrote to make it easier to work with computer certificates. If you need to encrypt things like passwords in a DSC configuration,  you must some type of certificate thumbprint as well as a copy of the certificate. The idea is that you can use the exported certificates public key to encrypt the password. The remote computer then uses the private key to decrypt.

The functions I wrote for the course were relatively simple, but I always knew I would re-visit them. Now I have. Not only have I extended the functionality, I’ve also turned the functions into a module.

The module contains two functions, Export-MachineCertificate and Get-MachineCertificateThumbprint.  They are very similar in terms of their output, except that the export function does just that, it exports the certificate. I have a hard coded path of C:\Certs but you can change that.

The commands rely on the PKI module that you should have on Windows 8 and later.

Exporting a certificate

By default, the function will also test if the certificate is valid, although you can skip that test.

If you just want the thumbprint, you can use the other command.

Getting a certificate thumbprint

I suppose I could have combine the functions into one.  In fact, as I write this I can think of a few other changes I might have made but I think I will leave things as they stand.

You can find the files on GitHub.

PowerShell Play by Play with Don Jones

pluralsight_logoI’m very excited to tell you about this latest effort from Pluralsight. A few months ago I sat down with Don Jones, and we recorded a live course on getting started with PowerShell.  The finished product has finally been released. Over the course of about an hour we talk about taking an idea or task and accomplishing it with PowerShell. The video is unrehearsed and often spontaneous. It was a lot of fun to do and I think you’ll find it entertaining and informative.

If you are a Pluralsight subscriber head over here and let me know what you think.

DevOps Twitter Chat

DevOpsChat-tw_v1_sd This Thursday, February 26, I will be doing a live Twitter chat from 12PM to 1PM EST. The topic is DevOps but I’m sure we’ll talk about PowerShell and automation in general. Use hashtag #DevOpsChat to submit questions and follow along. You can follow me on Twitter as @JeffHicks. I hope you’ll join me.

New PowerShell Pluralsight Course

WatermarkLogo151x79I am so happy to announce that my first course under the Pluralsight badge has been released. My latest course is titled PowerShell v4 New Features. This course is aimed at IT Pros with previous experience using PowerShell, especially PowerShell v3. The course runs just under 3 hours (although it felt much longer than that while preparing it 🙂 ) and covers just the new bits that have changed from v3 including an introduction to Desired State Configuration (DSC). I provide enough information to get you started.

If you need to get caught up on v3 first, you can find my other courses on my author page. I hope you find my new course helpful.