Sunday, November 27, 2022

Update on the MIM Script from Last Post

On PowerShell Power Users LinkedIn group, Tim Clapper posted a way to speed up the main script.  Here two snippets from the the script I posted:

$results = get-aduser -filter * -properties * | where-object {$_.givenName -eq $FirstName -and $_.sn -like $tempLastName| select-object givenname,sn | Measure-object

While ($results.count -eq 1

#this checks if there is an account with the first name and last name.  If $results.count is zero, the while loop is exited.

{

       $tempLastName = $LastName + $counter

       #This command now looks for a first name and last name+counter.

       $results = get-aduser -filter * -properties * | where-object {$_.givenName -eq $FirstName -and $_.sn -like $tempLastName| select-object givenName,sn | measure-object

       $counter++

}

Tim recommended I make the following changes to speed up the script:
1. Instead of using the where-object statement, move the conditions to the -filter switch
2. Instead of using 'sn', use 'surname'

The changes would look like the following:

$results = get-aduser -filter(givenName -eq $FirstName -and surname -like $tempLastName) | select-object givenName,surname | measure-object

Within the While loop

While ($results.count -eq 1

#this checks if there is an account with the first name and last name.  If $results.count is zero, the while loop is exited.

{

       $tempLastName = $LastName + $counter

       #This command now looks for a first name and last name+counter.

       $results = get-aduser -filter(givenName -eq $FirstName -and surname -like $tempLastName) | select-object givenName,surname | measure-object


       $counter++

}


I've tested the code and it does run faster since the code isn't searching all of Active Directory with the Where-Object command.

Test the change for yourself and let me know what you think. You can find the full code and MIM activity screen shot at https://github.com/mikeegan400/MIM-Test-for-Unique-First-and-Last-Name

A huge thank you to Tim Clapper.  He is one of the many PowerShell fans in the LinkedIn PowerShell group.  I highly recommend you join!

Until next time...


No comments:

Post a Comment