Efficient Use of Powershell across Domain Controllers: LastLogon Part 2


Our previous script was simple but not quick. Our second attempt will try to reduce the time it takes to gather all the pieces.

First of all we may imagine that our use of the hashtable in Powershell may be inefficient as we start from 0 object and are continuously adding new users, whereas we approximately know the number of users from any Get-ADUser count returns. The approximation could only be wrong if a bunch of new users were created on a single Domain Controller and not yet replicated (In this case your RID Master will have its share of workload).

Some research on the Internet may help you to find this kind of thread where you’ll learn that you can initialize a hashtable with a predefined number of elements, using the following syntax:

Then we may rewrite our loop to gather all the information and then do computation:

A run of this program will output this kind of figures (8000 users on a dozen of domain controllers located across the world):GetADUser-MeasureCommand-Attempt2

It is better than our first attempt. However if we look at the resources used during the run, we may still be surprised that there is connection to the network during the entire time the script is run. If you are not convinced, launch the resource monitor and filter the ‘powereshell’ process. With our script, you’ll see that each Domain Controller is still contacted during the ‘Computing’ part of our script.

Therefore our information retrieval is not as optimized as we though. That will be a quest for part 3.

Leave a comment

Your email address will not be published. Required fields are marked *