Skip navigation

JSI Tip 10466. How can I change the user name (sAMAccountName) of all my users?


Using AdFind.exe and AdMod.exe, I have created a sample script to change all my user's logon names (SAMID) to be the user's sn (surname), with all spaces and punctuation squeezed out, followed by the first letter of each string composed of the givenName and initials, where , and . and space begin a new string.

Examples

givenName        initials       sn               New sAMAccountName
Jerold                          Schulman         SchulmanJ
Jennifer         V.             Schulman         SchulmanJV
John Harry Paul  A.J.           Doe,III          DoeIIIJHPAJ
AllNewSAMID.BAT contains:
@echo off
setlocal
for /f "Tokens=*" %%a in ('adfind -default -nodn -csv -csvdelim " " -nocsvheader -f "&(objectcategory=person)(objectclass=user)" distinguishedName sAMAccountName sn givenName initials') do (
 call :who %%a
)
endlocal
goto :EOF
:who
set DN=%1
set Usr=%2
if /i %Usr% EQU "SQLSERVER" goto :EOF
set ln=%3
set ln=%ln:"=%
set ln=%ln:,=%
set ln=%ln:.=%
set ln=%ln: =%
set name=%4 %5 %6 %7 %8 %9
set name=%name:"=%
set name=%name:,= %
set name=%name:.= %
call :parse %ln% %name%
goto :EOF
:parse
set /a cnt=0
if \{%1\} EQU \{\} goto :EOF
set SAMID=%1
shift
:parse1
if \{%1\}==\{\} goto parse2
set /a cnt=%cnt% + 1
set char=%1
set Array(%cnt%)=%char:~0,1%
shift
goto parse1
:parse2
if %cnt% EQU 0 goto :setSAM
if %cnt% EQU 1 set SAMID=%SAMID%%Array(1)%&goto setSAM
set /a seq=0
:parse3
set /a seq=%seq% + 1
if %seq% GTR %cnt% goto setSAM
call set char=%%Array(%seq%)%%
set SAMID=%SAMID%%char%
goto parse3
:setSAM
if /i "%SAMID%" EQU %Usr% goto :EOF
admod -b %DN% "sAMAccountName::%SAMID%"



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