AD Cleanup task

pryles2000
pryles2000 used Ask the Experts™
on
I'm looking for a script that can query Active Directory for accounts that exist in a list. I.E. Txt file or xls file.

cheers!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
when you say accounts that exist in a list how are they formatted?

file.txt contents:
user1
user2
user3

or are they actually the DN of the object in AD?
file.txt contents:
cn=user1,ou=users,dc=domain,dc=local
cn=user2,ou=users,dc=domain,dc=local
cn=user3,ou=users,dc=domain,dc=local

are you running server 2003/2008?

you can use the dsquery utility to query AD

i.e.

dsquery user -limit 0 | findstr /i "user1"

dsquery user -limit 0 | findstr /i "user2"

something like this will either find the user1/user2

so in a loop to be quick and dirty

for /F %i in (file.txt) do (
dsquery user -limit 0 | findstr /i %i >> output.txt
)

~Regards

Author

Commented:
I can format the list anyway we need to....The job requires parcing AD for user accounts from a list provided by HR. If the accounts don't exist then we have to check to see if the Home folder has been cleaned up.
If you already have a script that does this I'd like to see how you laid it out.

cheers!

Commented:
Paste the script below into a text file with a .cmd extension.  Customize the value of the list variable on line 4 with the location of a text file containing the usernames to look up, one per line.

Running the script will query for the information and write the results to a comma-delimited text file.


@echo off
setlocal enabledelayedexpansion
 
set list=c:\files\list.txt
set report=report.csv
 
echo Username,User Exists,Home Folder Exists > "%report%"
 
for /F %%G in ('type "%list%"') do (
 set userfound=false
 set folderfound=false
 
 net user %%G /domain > NUL 2>&1 && set userfound=true
 if exist "%homeshare%\..\%%G" set folderfound=true
 echo %%G,!userfound!,!folderfound! >> "%report%"
)

Open in new window

Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Author

Commented:
Can youy tell me how do I pasue this execution....it runs in a dos box then ends with no result....Can I run it with a verbos switch?

Commented:
The script will write results to a comma-delimited text file.  Assuming you haven't altered the value of the report variable on line 5, the file will be named report.csv and will be located in the same directory as the script.  You can open it in Excel or any text editor.

Author

Commented:
your right...it does....
thanks a ton for all your help!

Commented:
You did not accept my solution correctly.

Author

Commented:
actually I found a bug....it checks for Home folder but what if its a share and not a home folder...do I just change it to %homeshare%? Would that work?

Commented:
The %homeshare% variable is equal to the location of the current user's home folder.  %homeshare%\.. is one level up from that.  Assuming that all of your users' home folders are hosted under the same directory, "%homeshare%\..\%%G" should point to each listed user's home folder.

I don't understand what you mean by, "what if its a share and not a home folder".

Author

Commented:
In AD on our 2003 domain under the users account info under the profile tab there are two options. User Profile and then Home folder. Under Home folder you can choose Local path or you can choose connect. This connect option allows you to specify a Drive letter and a share usually a UNC path.
When connect is specified with a UNC path the %homeshare% variable doesnt return the proper result.
For instance I ran it against users I know have shares located using H: \\servername\user$ but the report.csv file reported that the home share didnt exist.

Did that make sense?

Commented:
For those users, does the correct result show up on the User profile or Home directory lines if you run the command net user <username> /domain?

Author

Commented:
Here is what returned fron Net user myname /domain

The request will be processed at a domain controller for domain secret.com

User name                    pjones
Full Name                    Penny Jones
Comment                      Edmonton, Centre
User's comment
Country code                 (null)
Account active               Yes
Account expires              Never

Password last set            6/26/2009 8:35:33 AM
Password expires             9/24/2009 8:35:33 AM
Password changeable          6/29/2009 8:35:33 AM
Password required            Yes
User may change password     Yes

Workstations allowed         All
Logon script
User profile
Home directory               \\filer\pjones$
Last logon                   7/20/2009 8:20:46 AM

Logon hours allowed          All

Does this help solve the mystery?
It does tell me the home folder....is there a differant variable to enumerate this?

Author

Commented:
Here is what I'm getting back in the CSV file.

Username      User Exists      Home Folder Exists
hfane      FALSE      false
gchin      FALSE      false
cbishop      FALSE      false
pjones      TRUE      false

Commented:
Ok, try this revision.


@echo off
setlocal enabledelayedexpansion
 
set list=c:\files\list.txt
set report=report.csv
 
echo Username,User Exists,Home Folder Exists,Home Folder > "%report%"
 
for /F %%G in ('type "%list%"') do (
 set userfound=false
 set folderfound=false
 set home=
 
 for /F "tokens=3*" %%H in ('net user %%G /domain 2^>NUL ^| find "Home directory"') do (
  set home=%%H
  set userfound=true
 )
 
 if exist "!home!" set folderfound=true
 echo %%G,!userfound!,!folderfound!,!home! >> "%report%"
)

Open in new window

Author

Commented:
Sweeeet....so if the home folder does exist I can add line 21: move \\filer\!home! \\filer\old

would this work?
Commented:
The !home! variable is equal to the full UNC path of the folder, so you'd want to use
move "!home!" \\filer\old

Author

Commented:
You Guys Rock

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial