Solved

Optimize Powershell (Quest AD) script to populate AD group

Posted on 2011-09-14
3
886 Views
Last Modified: 2012-05-12
Hello,

I have a script that I'd like to use to maintain distribution groups in Active Directory current. I search for a keyword in the title AD attribute and then I add the member to the group. My script works, but it seems that it's taking a little while to complete. I've used the [void] types in an attempt to speed up the script but still slow especially when adding 1000+ users to the group. Another area where my script might be slow is in the fact that I "rebuild" the group every time....in other words, I clear it first and then add the members all over again.

I'm looking to see if someone out there can provide me with tips on how to speed up this script. Thanks.
# Params
$filter = "(title=*keyword*)"
$scope = 'dc=domain,dc=local'
$Group = Get-QADGroup -Identity "My-Group"

# Clear group
[void](Set-QADGroup -Identity $Group.DN -Member $NULL)

# Get all enabled Active Directory accounts
$Searcher = Get-QADUser -Enabled -SearchRoot $scope -IncludedProperties title -LdapFilter $filter  -SizeLimit 0

# Add each account to the specified group
$Searcher | ForEach-Object {
	[void](Add-QADGroupMember -Identity $Group.DN -Member $_.DN )
	}

Open in new window

0
Comment
Question by:bndit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
Dale Harris earned 250 total points
ID: 36539680
I noticed you're not piping anything. This should speed things up a little.  Since the actual VOID of the group is pretty much instant, we won't save much time there.

$filter = "(title=*keyword*)"
$scope = 'dc=domain,dc=local'
$Group = Get-QADGroup -Identity "My-Group"

# Clear group
[void](Set-QADGroup -Identity $Group.DN -Member $NULL)

# Get all enabled Active Directory accounts and add them to the group
Get-QADUser -Enabled -SearchRoot $scope -IncludedProperties title -LdapFilter $filter  -SizeLimit 0 | %{Add-QADGroupMember -Identity $Group.DN -Member $_.DN}

It would help if you were able to reduce the amount of Users that come up via a smaller OU starting point.  The LDAPFilter still has to be applied to every single user found.  If you had only 300 users to search through instead of 5000, that would save the time.  Try to see if you can get as granular as possible without having to go through AD.   You have it pretty much figured out though.

HTH,
Dale Harris
0
 
LVL 2

Author Comment

by:bndit
ID: 36539815
Thanks Dale....unfortunately, I have to crawl the entire AD domain because OU are not in place at the time, but I totally agree with you. Once the OU structure is in place I'll tweak the script to target smaller user sets. I made the change and seems to be working Ok...not sure how much "faster" it is but I'll go with your suggestion.

Thanks again.
0
 
LVL 2

Author Closing Comment

by:bndit
ID: 37149378
thank you.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question