Tag Archives: hashtable

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.

image

I’ve easily updated the user account.

image

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

image

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.

image

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.

image

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

image

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.

image

With this concept in mind I can revise the function.

image

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.

image

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.

Sorting Hash Tables

letterjumbleOver the weekend I received a nice comment from a reader who came across an old post of mine on turning an object into a hash table.  He wanted to add a comment but my blog closes comments after a period of time. But I thought it was worth sharing, especially for those of you still getting started with PowerShell. The comment was on how to sort a hash table.

Let’s say you have a simple hash table like this:

And is displayed like this:

To sort on the keys, you can use the GetEnumerator() method which is part of every hash table object. This method creates a System.Collections.DictionaryEntry object for each item in the hash table.

This means you can sort on any property.

By the way, starting in PowerShell 3.0, you could “pre-sort” the hash table by defining it as ‘ordered’.

Now the hash table will always be sorted in the order you defined the entries.

Although, if you want to sort you still can.

Have a great week and I hope you get things sorted out.

Convert Text to Object with PowerShell and Regular Expressions

squarepatternA few weeks ago I was getting more familiar with named captures in regular expressions. With a named capture, you can give your matches meaningful names which makes it easier to access specific captures. The capture is done by prefixing your regular expression pattern with a name.

When you know the name, you can get the value from $matches.

This also works, and even a bit better, using a REGEX object.

With the REGEX object you can get the names.

Because the names include index numbers, I usually filter them out. Once I know the names, I can use them to extract the relevant matches.

Then I realized it wouldn’t take much to take this to the next step in PowerShell. I have a name and a value, why not create an object? It isn’t too difficult to create a hashtable on the fly and use that to create a custom object. Eventually I came up with ConvertFrom-Text.

The function requires a regular expression pattern that uses named captures. With the pattern you can either specify the path to a log file, or you can pipe structured text to the function. By “structured text” I mean something like a log file with a predictable pattern. Or even output from a command line tool that has a consistent layout. The important part is that you can come up with a regular expression pattern to analyze the data. I also wanted to be able to pipe in text in the event I only wanted to process part of a large log file.

Here’s an example using the ARP command.

In this particular example, I’m trimming the ARP output to remove any leading or trailing spaces from each line and then converting each line to an object, using the regular expression pattern.

convertfrom-text

If you haven’t jumped to why command is useful, is that once I have objects I can easily filter, sort, group, export, or just about anything else. By converting a log file into a collection of objects I can do tasks like this:

convertfrom-text-2

I hope some of you will try this out and let me know what you think. What works? What is missing? What problem did this solve? Inquiring minds, well at least mine, want to know. Enjoy.