Parallel Processing in PowerShell: Active Directory Cmdlets Revisited

Posted on July 2, 2015 by Jeff Hicks in PowerShell with 0 Comments

In my article, An Introduction to Parallel PowerShell Processing, we looked at different ways to work with PowerShell in parallel. Specifically, we looked at how to find objects with Active Directory cmdlets by searching multiple locations at the same time. Although I complain about the AD cmdlets, they do perform quite well. All that I really need is a version of Get-ADComputer that will automatically do that for me. It would also be useful to be able to specify a search path to exclude.

Once again, my handy Copy-Command function comes to the rescue. In the ISE, I create a new version of the command.

The Get-MyADComputer function in the PowerShell ISE. (Image Credit: Jeff Hicks)

The Get-MyADComputer function in the PowerShell ISE. (Image Credit: Jeff Hicks)

The first thing is to modify the Searchbase parameter so that it can accept an array of strings.

In the Process scriptblock, I need to reflect that if multiple locations are specified that Get-ADComputer be run for each location. Because I am splatting PSBoundParameters to Get-ADComputer, I need to tweek the Searchbase parameter because even though my function can accept an array, the original command does not.

If I run the command without specifying a searchbase, then the command runs normally.

You’ll notice that I’m saving results to a variable. This is so I can exclude based on a regular expression pattern. This requires a new parameter.

Even though I’m specifying a REGEX type, you can use a simple string. However, the searchbase and exclude parameters are mutually exclusive, so I created an additional parameter set. This also means going through the other parameters and adding them to the Exclude parameter set. I made a decision to only exclude if –Filter was used.

This means that I need to remove –Exclude from $PSBoundparameters, because Get-ADComputer won’t recognize it.

In the End scriptblock, if an exclude value was specified, then filter the results otherwise write everything to the pipeline.

Because I’m creating my own version of Get-ADComputer, I also decided to fix the filter parameter. I want to automatically set the filter to *, unless I specify otherwise. I am constantly running Get-ADComputer and expecting it to give me all results, but instead I’m prompted to enter a filter. For me, I guess I’m expecting it to behave like Get-Service or Get-Process, so I’ll bend it to my will and add a default value.

Sponsored

This if statement says, “if the parameter set is Filter or Exclude AND the user did not specify –Filter, go ahead and set a default filter of *.” The last step of the process was to update the comment-based help to reflect the new parameters and change in behavior. Here’s the finished result.

Now I can easily exclude a location:

Or search multiple locations:

Searching multiple computers with Get-MyADComputer. (Image Credit: Jeff Hicks)

Searching multiple computers with Get-MyADComputer. (Image Credit: Jeff Hicks)

And since Get-ADUser is very similar to Get-ADComputer, I created a custom version, copying the changes from Get-MyADComputer.

I can use this much the same way.

Sponsored

I hope these articles give you the confidence to try creating some of your own tools. And even though what you see here looks like a lot of work, most of the code was created by my Copy-Command function. The amount of PowerShell that I had to actually write and test was much smaller.

As always, I welcome your comments.

Sponsored

Tagged with , , ,