More PowerShell Coloring Tips for Domain Controller Statuses

Posted on January 26, 2016 by Jeff Hicks in PowerShell with 0 Comments

color-aspect-hero

I hope you’ve enjoyed our journey into displaying domain controller service statuses in a more colorful format. I hope you have been thinking about how you could extend the techniques I’ve shown you into more flexible and re-usable tools. You will want to be caught up on the previous articles or some of what I’m about to show you might be confusing.

I’m sure you can think of a number of commands where you would like to highlight a particular value. In this article, I want to end up with a PowerShell function that will get the job done. Once again, let’s start with a set of domain controllers and services.

Depending on the service status, I want to display it in a given console color based on a hashtable.

I’m also going to need a regular expression pattern to match on the output.

When building a tool, remember to break things down into discrete steps so that you can modularize. I’ll save the data separately.

This makes it easier to revise my code that will handle the formatting, and I won’t have to rerun the command all the time. I know from the previous articles that I need to turn the data into an array of strings. I had been using the Split() method, but it is just as easy to use the Split operator.

Now I can pipe this to the code I had been using with ForEach-Object.

Highlighted output (Image Credit: Jeff Hicks)

Highlighted output (Image Credit: Jeff Hicks)

So what I need to do is modularize the ForEach code. Cmdlets and advanced functions work that way anyway under the hood. Here’s a prototype.

And it more or less works.

Testing the prototype function (Image Credit: Jeff Hicks)

Testing the prototype function (Image Credit: Jeff Hicks)

Well, there are some errors, but the function is also limited. The function assumes I will always use the results from Get-Service and that I always want to use these colors. Don’t code yourself into a corner. The goal is to create something flexible enough to use in a variety of situations.

To me, it makes more sense to parameterize the key colors and regular expression pattern. Don’t worry, I’m not going to draw this out. Here’s my completely revised function.

Sponsored

This version lacks any sort of help documentation, but you can see the syntax:

Out-Highlight syntax (Image Credit: Jeff Hicks)

Out-Highlight syntax (Image Credit: Jeff Hicks)

The default behavior is to process a collection of input objects looking for regular expression pattern matches and then highlighting the match with a corresponding console color in the hightlight hashtable.

Testing the function (Image Credit: Jeff Hicks)

Testing the function (Image Credit: Jeff Hicks)

I also added options to color the background or the entire line.

Highlighting an entire line (Image Credit: Jeff Hicks)

Highlighting an entire line (Image Credit: Jeff Hicks)

This should now work with any type of command, although it may take some work to build the correct regular expression. Here’s an example that highlights files based on their extension.

Highlighting files (Image Credit: Jeff Hicks)

Highlighting files (Image Credit: Jeff Hicks)

Or virtual machines:

Highlighting virtual machines (Image Credit: Jeff Hicks)

Highlighting virtual machines (Image Credit: Jeff Hicks)

As I mentioned in previous articles, these techniques are writing directly to the console not the pipeline. This means you can’t do anything with the results other than look at it. Normally you should always be thinking about working with objects in the pipeline, but sometimes highlights like this can make your work a bit easier and PowerShell can help with that as well.

Sponsored

I hope a few of you will kick this around, and let me know what you think.

Sponsored

Tagged with ,