JSI Tip 8553. How can I rename a user's distinguished name, from the command-line, in my Windows Server 2003 domain?

Using the LDIFDE.EXE tool, and the DSGET Active Directory command-line tool, I have scripted RenameUser.bat to rename the distinguished name of a user on a Windows Server 2003 domain controller, in a Windows Server 2003 domain.

The syntax for using RenameUser.bat is:

RenameUser "CurrentDN" "NewDN"

Where:

"CurrentDN" is the user's current distinguished name.
"NewDN"     is the user's new distinguished name.

Examples:

RenameUser "CN=TEST Rename,CN=Users,DC=JSIINC,DC=COM" "CN=Rename TEST,CN=Users,DC=JSIINC,DC=COM"

RenameUser "CN=Rename TEST,CN=Users,DC=JSIINC,DC=COM" "CN=Rename Test,OU=OU_TEST,DC=JSIINC,DC=COM"

RenameUser.bat contains:

@echo off
if \{%2\}==\{\} @echo Syntax: RenameUser "CurrentDN" "NewDN"&goto :EOF
setlocal
set cDN=%1
set nDN=%2
set OK=N
for /f "Tokens=*" %%a in ('dsget user %cDN% ^|findstr /I /C:"dsget succeeded"') do (
 set OK=Y
)
If "%OK%" EQU "N" @echo RenameUser - %cDN% not found.&endlocal&goto :EOF
for /f "Tokens=*" %%a in ('dsget user %nDN% ^|findstr /I /C:"dsget succeeded"') do (
 set OK=N
)
If "%OK%" EQU "N" @echo RenameUser - %nDN% already exists.&endlocal&goto :EOF
set cDN=%cDN:"=%
set nDN=%nDN:"=%
(
@echo dn: %cDN%
@echo changeType: modrdn
@echo newRdn: %nDN%
@echo deleteOldRdn:1
) >"%TEMP%\RenameUser.ldf"
set OK=N
for /f "Tokens=*" %%a in ('ldifde -I -f "%TEMP%\RenameUser.ldf" ^|findstr /I /C:"The command has completed successfully"') do (
 set OK=Y
)
if "%OK%" EQU "N" @echo RenameUser "%cDN%" "%nDN%" failed.
del /q "%TEMP%\RenameUser.ldf"
endlocal



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish