Avatar of bsharath
bsharath
Flag for India asked on

Code that moved AD users to specific OU. Can the same code be changed to move Contacts.

Hi,

Code that moved AD users to specific OU. Can the same code be changed to move Contacts.
Can anyone help me with this.

Regards
Sharath
:: SCRIPT START
@ECHO OFF
SETLOCAL EnableDelayedExpansion

:: Following variables required to be set with actual values
SET NewOU=TempOU

IF EXIST UserMoveRpt.txt DEL /F /Q UserMoveRpt.txt
ECHO %NewOU%|FIND /I "OU=">NUL
IF NOT ERRORLEVEL 1 (
    DSQuery OU "%NewOU%" 2>NUL |FIND /I "OU=" >NUL
    IF NOT ERRORLEVEL 1 (DSQuery OU "%NewOU%" >OUTmpVar.txt &SET /P OUDN=<OUTmpVar.txt
    ) ELSE (ECHO Invalid OU distinguished name. &Goto :EndScript)
)ELSE (
    DSQuery OU -Name "%NewOU%" |FIND /I "OU=" >NUL
    IF NOT ERRORLEVEL 1 (DSQuery OU -Name "%NewOU%" >OUTmpVar.txt &SET /P OUDN=<OUTmpVar.txt
    )ELSE (ECHO Invalid OU Name. &Goto :EndScript)
)

FOR /F "delims=*" %%u IN ('TYPE UserIDs.txt') DO (
      ECHO Processing: %%u
      DSQuery User -samID "%%u" |Find /I "CN=" >NUL
      IF NOT ErrorLevel 1 (
            DSQuery User -samID "%%u" |DSMove -newparent !OUDN! >>UserMoveRpt.txt
      )ELSE (ECHO *ERROR* '%%u' Not Found in Active Directory.))

ECHO.&ECHO Script complete. Check 'UserMoveRpt.txt' file.
:EndScript
IF EXIST OUTmpVar.txt DEL /F /Q OUTmpVar.txt
ENDLOCAL
EXIT /B 0
:: *** SCRIPT END ***

Open in new window

Scripting LanguagesShell ScriptingWindows Batch

Avatar of undefined
Last Comment
bsharath

8/22/2022 - Mon
tegmine_it

No, it can't, contacts are not stored in AD
RobSampson

Hi, see if this works. It should read contact ids from a file called ContactIDs.txt

Rob.
:: SCRIPT START 
@ECHO OFF 
SETLOCAL EnableDelayedExpansion 
 
:: Following variables required to be set with actual values 
SET NewOU=TempOU
 
IF EXIST ContactMoveRpt.txt DEL /F /Q ContactMoveRpt.txt 
ECHO %NewOU%|FIND /I "OU=">NUL 
IF NOT ERRORLEVEL 1 ( 
    DSQuery OU "%NewOU%" 2>NUL |FIND /I "OU=" >NUL 
    IF NOT ERRORLEVEL 1 (DSQuery OU "%NewOU%" >OUTmpVar.txt &SET /P OUDN=<OUTmpVar.txt 
    ) ELSE (ECHO Invalid OU distinguished name. &Goto :EndScript) 
)ELSE ( 
    DSQuery OU -Name "%NewOU%" |FIND /I "OU=" >NUL 
    IF NOT ERRORLEVEL 1 (DSQuery OU -Name "%NewOU%" >OUTmpVar.txt &SET /P OUDN=<OUTmpVar.txt 
    )ELSE (ECHO Invalid OU Name. &Goto :EndScript) 
) 
 
FOR /F "delims=*" %%u IN ('TYPE ContactIDs.txt') DO ( 
      ECHO Processing: %%u 
      DSQuery Contact -samID "%%u" |Find /I "CN=" >NUL 
      IF NOT ErrorLevel 1 ( 
            DSQuery Contact -samID "%%u" |DSMove -newparent !OUDN! >>ContactMoveRpt.txt 
      )ELSE (ECHO *ERROR* '%%u' Not Found in Active Directory.)) 
 
ECHO.&ECHO Script complete. Check 'ContactMoveRpt.txt' file. 
:EndScript 
IF EXIST OUTmpVar.txt DEL /F /Q OUTmpVar.txt 
ENDLOCAL 
EXIT /B 0 
:: *** SCRIPT END ***

Open in new window

bsharath

ASKER
Thanks Rob

I get this

dsquery failed:`-samID' is an unknown parameter.
type dsquery /? for help.*ERROR* 'Abhishek Avi' Not Found in Active Directory.

Can we query by full name
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
RobSampson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
bsharath

ASKER
Thanks a lot Rob.. :-)
RobSampson

No problem.  Thanks for the grade.

Regards,

Rob.
bsharath

ASKER
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
bsharath

ASKER
Rob can we have email id as the base to move them
bsharath

ASKER