Solved

Windows Power Shell Script to search for multiple users by sAMAccountName

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Resolve DNS query failed errors for Exchange
In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

860 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