?
Solved

Windows Power Shell Script to search for multiple users by sAMAccountName

Posted on 2009-03-30
5
Medium Priority
?
812 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
[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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

This article shows the method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

770 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