How to output the full OU path for a number of AD accounts.

Hi there,

I want to output the full OU path for serveral AD accounts. I'm running a DSQuery that searches particular OUs, and then  a DSGet to output certain fields to txt files (-display, -samid,-title etc). The query is attached.

That part works fine. But I can't work out how to output the OU path for those same accounts. Can anyone help?

@ECHO off
REM ***************************************************************************
 
REM The following OUs are INCLUDED:
REM - Generic Accounts
REM - Service Accounts
REM - Telephony Accounts
REM - Training Accounts
REM - Vendor Dial-In Accounts
REM The following OUs are EXCLUDED:
REM - Dead Users
REM - To Delete
REM 
REM 
REM ***************************************************************************
 
REM Script Initialization Section
title Generic User Accounts
color 0c
set logfile=error.log
set output1=%temp%\genericusers1.csv
set output2=%temp%\genericusers2.csv
set output3=%temp%\genericusers3.csv
set outputtmp=%temp%\genericusers.tmp
set limit=4000
 
REM Main Processing Section
if exist %temp%\*.csv erase %temp%\*.csv 2>>%logfile% >nul
if exist %temp%\*.tmp erase %temp%\*.tmp 2>>%logfile% >nul
cls
echo.
echo Processing. Please wait...
echo.
 
dsquery user ou=uk,dc=corp,dc=ad -limit %limit% | findstr /c:"OU=Generic Accounts" /c:"OU=Service Accounts" /c:"OU=Telephony Accounts" 
 
/c:"OU=Training Accounts" /c:"OU=Vendor Dial-In Accounts" | dsget user -display -samid -title -distinguishedName>> %output1%
dsquery user ou=uk,dc=corp,dc=ad -limit %limit% | findstr /c:"OU=Generic Accounts" /c:"OU=Service Accounts" /c:"OU=Telephony Accounts" 
 
/c:"OU=Training Accounts" /c:"OU=Vendor Dial-In Accounts" | dsget user -mgr  >> %output2%
 
 
cls
echo.
echo Done.
echo.
pause
cls
echo.
echo Formatting. Please wait...
echo.
for /f "skip=1 delims==, tokens=1-2" %%a in (%output2%) do (
 
	echo %%b >> %outputtmp%
 
 	for %%c in (%outputtmp%) do if %%~zc equ 0 (
 
 		echo. >> %output3%
 
	)
 
	echo %%b >> %output3%
 
)
 
cls
echo.
echo Done. Hit any key to display the output files.
echo.
pause
start notepad %output1%
start notepad %output3%
title %comspec%
color
cls
 
REM Subroutine and Procedure Section

Open in new window

LVL 1
Gavin5511Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Farhan KaziSystems EngineerCommented:
Check following code this may help you to get OU path.



@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET OUs="Generic Accounts","Service Accounts","Telephony Accounts","Training Accounts","Vendor Dial-In Accounts"
FOR %%v IN (%OUs%) DO (
	SET OUPath=OU=%%~v,OU=UK,DC=CORP,DC=AD
	ECHO OU Path: !OUPath! >>File.txt
	DSQuery * "!OUPath!" -Filter "(samAccountType=805306368)" -Attr displayName sAMAccountName title distinguishedName -Limit 0 >>File.txt)
	EXIT /B 0

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.