• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 460
  • Last Modified:

Find all users and the OU's and the Computers and there Ou

Hi,

I want to see which are users are there in which OU and the same way the computers.
Ex:
Username : OUname
Regards
Sharath
0
bsharath
Asked:
bsharath
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
SPOuedCommented:
Do you mean:
I want to see which users are in which OU? and Which computers are in which OU too?
You can use "Find" in ADUC to search for user or computer objects within OU...
0
 
bsharathAuthor Commented:
I want this to a file. I have all users and computers scattered in different Ou's.
0
 
Farhan KaziSystems EngineerCommented:
You want list of all user ID's and Computer names along with their OU's in following format

FileOne:
UserName : OUname

FileTwo:
ComputerName : OUname

Rite?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
oBdACommented:
This script (no changes necessary) will create two csv files, named like the script, but with -user and -computer, respectively, at the end, and the extension .csv.
Can't test it at the moment, but it should work:

@echo off
setlocal
set LogFile=%~dpnx0-User.csv
call :process user
set LogFile=%~dpnx0-Computer.csv
call :process computer
goto :eof
:process
for /f "delims=" %%a in (dsquery %~1 -limit 0) do set ObjectDN=%%~a
for /f "tokens=2 delims=,=" %%a in ("%ObjectDN%") do set ObjectName=%%a
>>"%LogFile%" echo "%ObjectName%";"%ObjectDN%"
goto :eof
0
 
bsharathAuthor Commented:
Farhankazi
Yes you are correct.
Obda
this does not give any data in the files created.
0
 
Farhan KaziSystems EngineerCommented:
:: ===============
:: READ THIS FIRST
:: ===============
:: * Successful run will generate "UsersOU.txt" and "ComputersOU.txt" file on C: drive root.
:: * Copy and paste following script in notepad and save it with any name having .cmd extension.
:: *** SCRIPT START ***

@Echo Off
SETLOCAL EnableDelayedExpansion

IF EXIST C:\UsersOU.txt DEL /F /Q C:\UsersOU.txt
IF EXIST C:\ComputersOU.txt DEL /F /Q C:\ComputersOU.txt
Echo.
Echo *** USER PROCESS ***
SET Qry=DSQuery * domainroot -Filter "(&(objectCategory=Person)(objectClass=User)(sAMAccountName=*))" -attr sAMAccountName -limit 0 -L
FOR /F "Skip=1 Delims=#" %%u IN ('!Qry!') DO (
      Echo Processing:  %%u
      SET Qry2=DSQuery * domainroot -Filter "(sAMAccountName=%%u)" -attr distinguishedName
      FOR /F "Delims=,= Skip=1 Tokens=4" %%o IN ('!Qry2!') DO (
      Echo %%u : %%o >>C:\UsersOU.txt)
)
Echo.
Echo *** COMPUTER PROCESS ***
SET Qry=DSQuery * DomainRoot -Filter "(&(objectCategory=Computer)(objectClass=User))" -Attr Name -limit 0 -L
FOR /F "Skip=1 Delims=#" %%u IN ('!Qry!') DO (
      Echo Processing:  %%u
      SET Qry2=DSQuery * domainroot -Filter "(&(objectCategory=Computer)(objectClass=User)(Name=%%u))" -attr distinguishedName
      FOR /F "Delims=,= Skip=1 Tokens=4" %%o IN ('!Qry2!') DO (
      Echo %%u : %%o >>C:\ComputersOU.txt)
)
ENDLOCAL
:: *** SCRIPT END ***
0
 
oBdACommented:
Sorry, this works now (script extension in the log name is now dropped, too):

@echo off
setlocal
set LogFile=%~dpn0-User.csv
call :process user
set LogFile=%~dpn0-Computer.csv
call :process computer
goto :eof
:process
for /f "delims=" %%a in ('dsquery %~1 -limit 0') do (
  for /f "tokens=2 delims=,=" %%m in (%%~a) do >>"%LogFile%" echo "%%m";%%a
)
goto :eof
0
 
oBdACommented:
Slight correction necessary, sorry again (those "last second" changes will always get you):

@echo off
setlocal
set LogFile=%~dpn0-User.csv
call :process user
set LogFile=%~dpn0-Computer.csv
call :process computer
goto :eof
:process
for /f "delims=" %%a in ('dsquery %~1 -limit 0') do (
  for /f "tokens=2 delims=,=" %%m in (%%a) do >>"%LogFile%" echo "%%m";%%a
)
goto :eof
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now