?
Solved

Windows Power Shell Script to search for multiple users by sAMAccountName

Posted on 2009-03-30
5
Medium Priority
?
816 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 71

Accepted Solution

by:
Chris Dent earned 2000 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 71

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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
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 video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

864 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