Solved

Windows Power Shell Script to search for multiple users by sAMAccountName

Posted on 2009-03-30
5
808 Views
Last Modified: 2012-05-06
I need help editing the power shell script below in such a way that in line #1 I can search using 100s of different partial account names as instead of just one.

Currently I must search for "(&(objectCategory=User)(sAMAccountName=*BLEVAN))"   then run the script again with a different partial name in there like "(&(objectCategory=User)(sAMAccountName=*VMERKEL))"  

In its current form,  I would have to save this script 100000 times and in each script replace the *BLEVAN with a different paritial account name.

How can I edit this script so that a single script will allow me to perform a search using many partial sAMAccount names and display all those names on a list? I am guessing some kind of loop structure is involved and a list of partial SAM Account names is required, but since I am not a programmer or scripter, I am hoping someone can provide me with some guidance.

$strFilter = "(&(objectCategory=User)(sAMAccountName=*BLEVAN))"  <---- I have more than 100 different "*partofsamname" that I need to search for, for example *BLEVAN, *VMERKEL, *RODREGEZ, ETC.. I want to include all of these in a single powershell script
 
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
 
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
 
$colProplist = "name"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
 
$colResults = $objSearcher.FindAll()
 
foreach ($objResult in $colResults)
    {$objItem = $objResult.Properties; $objItem.name}    <--- I want to write the list of users to a file

Open in new window

0
Comment
Question by:ACECORP
  • 3
  • 2
5 Comments
 
LVL 35

Expert Comment

by:Joseph Daly
ID: 24019608
What exactly are you trying to do. I may be able to suggest another method if powershell isnt a must.

It seems to me like you are just trying to do a

dsquery user - samid *xxxxx
0
 
LVL 35

Expert Comment

by:Joseph Daly
ID: 24019642
So doing a little bit of testing I came up with the following. The script below looks at a file.txt which contains the list of partial usernames.
for /F "tokens=1" %%h IN (file.txt) Do dsquery user -samid "%%h"

Open in new window

0
 
LVL 35

Expert Comment

by:Joseph Daly
ID: 24019648
And as far as the format of the text file. It is just a simpe txt file one entry per line.

I have starting and trailing wildcard characters but you can modify that to suit your need.
*lyjo*
*men*

Open in new window

0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 24019696

First, can I recommend you grab Quest's tools from here:

http://www.quest.com/powershell/activeroles-server.aspx

Because that'll replace all the directory searcher code unless it's important you do it natively.

Then if you have the list in a file you could do this. You'll need to pop in the rest of the attributes you're looking for, or just remove Select-Object entirely if you want the lot.

Chris
$Results = @()
Get-Content <FileName> | %{ $Results += Get-QADUser $_ | Select-Object DN, Name }
$Results | Export-CSV -Path "out.csv"

Open in new window

0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 24019782

For that matter you can just do the below, which has the advantage of being lovely and short although you'll get a lot of clutter in your output.

Chris
Get-Content file.txt | %{ Get-QADUser $_ } | Export-CSV out.csv

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Last week, our Skyport webinar on “How to secure your Active Directory” (https://www.experts-exchange.com/videos/5810/Webinar-Is-Your-Active-Directory-as-Secure-as-You-Think.html) provided 218 attendees with a step-by-step guide for identifying Acti…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

786 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