Solved

Windows Power Shell Script to search for multiple users by sAMAccountName

Posted on 2009-03-30
5
807 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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 …

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now