[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1450
  • Last Modified:

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

0
Gavin5511
Asked:
Gavin5511
1 Solution
 
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

Featured Post

Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now