Skip navigation

JSI Tip 8495. The ChgSrv.bat script can modify all users homeDrive, homeDirectory, and/or profilePath.


Using DSQUERY and DSMOD, I have scripted ChgSrv.bat to modify all users homeDrive, homeDirectory, and/or profilePath attributes.

The syntax for using ChgSrv.bat is:

ChgSrv \[H:OldSrv1:NewSrv1 D:OldDrv:NewDrv\] \[P:OldSrv2:NewSrv2\]

Where:

H:OldSrv1:NewSrv1 will modify the homeDirectory, changing the existing OldSrv1 to NewSrv1.
                  Both components may be null. When NOT nul, they must start with \\. If a $sam$ string is detected,
                  it will be replaced with the sAMAccountName.

D:OldDrv:NewDrv   will modify the homeDrive, changing the existing OldDrv letter to NewDrv. 
                  Both components may be null. When NOT null, they must be a single character.

P:OldSrv2:NewSrv2 will modify the profilePath, changing the existing OldSrv2 to NewSrv2. Both components may be null. When NOT null, they must start with \\. If a $sam$ string is detected, it will be replaced with the sAMAccountName.

Sample Valid Usage:

chgSrv H::\\ServerName\Home\$sam$ D::H P::\\ServerName\Profiles\$sam$ will change all users whose homeDrive, homeDirectory attributes are null, and all users whose profilePath attribute is null.

chgSrv H:\\Server1:\\Server2 D:H:H will change all users whose homeDrive is H and homeDirectory is on Server1 to a homeDirectory on Server2, leaving their homeDrive as H.

chgSrv P:\\ServerName\Profiles\$sam$: will change all users whose profilePath attribute is \\ServerName\Profiles\UserName to a local profile, leaving their homeDrive and homeDirectory where they are.

ChgSrv.bat contains:

@echo off
setlocal
if \{%1\}

\{\} goto syntax set sam= set Holdsrv=# set Hnewsrv=# set Poldsrv=# set Pnewsrv=# set Holddrv=# set Hnewdrv=# set param=%1 for /f "Tokens=1-3 Delims=:" %%a in ('@echo %1') do ( set type=%%a set old=%%b set new=%%c ) if "%param:~1,2%" EQU "::" set new=%old%&set old= if /i "%type%" NEQ "D" if "%new%" NEQ "" if "%new:~0,2%" NEQ "\\" goto syntax if /i "%type%" NEQ "D" if "%old%" NEQ "" if "%old:~0,2%" NEQ "\\" goto syntax if /i "%type%" EQU "H" set Holdsrv=%old%&set Hnewsrv=%new%&goto test2 if /i "%type%" EQU "P" set Poldsrv=%old%&set Hnewsrv=%new%&goto test2 if /i "%type%" EQU "D" set Holddrv=%old%&set Hnewdrv=%new%&goto test2 goto syntax :test2 if \{%2\}

\{\} goto test3 set param=%2 for /f "Tokens=1-3 Delims=:" %%a in ('@echo %2') do ( set type=%%a set old=%%b set new=%%c ) if "%param:~1,2%" EQU "::" set new=%old%&set old= if /i "%type%" NEQ "D" if "%new%" NEQ "" if "%new:~0,2%" NEQ "\\" goto syntax if /i "%type%" NEQ "D" if "%old%" NEQ "" if "%old:~0,2%" NEQ "\\" goto syntax if /i "%type%" EQU "H" set Holdsrv=%old%&set Hnewsrv=%new%&goto test3 if /i "%type%" EQU "P" set Poldsrv=%old%&set Pnewsrv=%new%&goto test3 if /i "%type%" EQU "D" set Holddrv=%old%&set Hnewdrv=%new%&goto test3 goto syntax :test3 if \{%3\}==\{\} goto doit set param=%3 for /f "Tokens=1-3 Delims=:" %%a in ('@echo %3') do ( set type=%%a set old=%%b set new=%%c ) if "%param:~1,2%" EQU "::" set new=%old%&set old= if /i "%type%" NEQ "D" if "%new%" NEQ "" if "%new:~0,2%" NEQ "\\" goto syntax if /i "%type%" NEQ "D" if "%old%" NEQ "" if "%old:~0,2%" NEQ "\\" goto syntax if /i "%type%" EQU "H" set Holdsrv=%old%&set Hnewsrv=%new%&goto doit if /i "%type%" EQU "P" set Poldsrv=%old%&set Pnewsrv=%new%&goto doit if /i "%type%" EQU "D" set Holddrv=%old%&set Hnewdrv=%new%&goto doit goto syntax :doit if "%Hnewsrv%" EQU "" set Hnewsrv=nul if "%Pnewsrv%" EQU "" set Pnewsrv=nul if "%Hnewdrv%" EQU "" set Pnewsrv=nul if "%Hnewdrv%" EQU "#" if "%Hnewsrv%" NEQ "#" goto syntax if "%Hnewdrv%" NEQ "#" if "%Hnewsrv%" EQU "#" goto syntax set qry1=dsquery * domainroot -filter set qry2= "(&(objectCategory=Person)(objectClass=User))" set qry3= -attr distinguishedName sAMAccountName homeDrive homeDirectory profilePath -L -limit 0 set qry=%qry1%%qry2%%qry3% set OK=N for /f "Tokens=1*" %%a in ('%qry%') do ( set attr=%%a set value=%%b call :test1 ) endlocal goto :EOF :err @echo Logic - User:%user% goto :EOF :syntax @echo ChgSrv H:OldSrv1:NewSrv1 D:OldDrv:NewDrv P:OldSrv2:NewSrv2 endlocal goto :EOF :test1 if /i "%attr%" EQU "distinguishedName:" set user=%value%&goto :EOF if /i "%attr%" EQU "sAMAccountName:" set sam=%value%&goto :EOF if /i "%attr%" EQU "homeDirectory:" set home=%value%&goto :EOF if /i "%attr%" EQU "homeDrive:" set drive=%value%&goto :EOF if /i "%attr%" NEQ "profilePath:" goto err set prof=%value% set OK=N set newhome=%home% set newprof=%prof% set newdrv=%drive% if "%drive%" NEQ "" ( if "%Holddrv%" NEQ "" call set newdrv=%%drive:%Holddrv%=%Hnewdrv%%% ) ELSE ( if "%Holddrv%" EQU "" call set newdrv=%Hnewdrv% ) call set old=%Holdsrv% call set new=%Hnewsrv% if "%old%" NEQ "" call set old=%%old:$sam$=%sam%%% if "%new%" NEQ "" call set new=%%new:$sam$=%sam%%% if "%home%" NEQ "" ( if "%old%" NEQ "" call set newhome=%%home:%old%=%new%%% ) ELSE ( if "%old%" EQU "" call set newhome=%new% ) call set old=%Poldsrv% call set new=%Pnewsrv% if "%old%" NEQ "" call set old=%%old:$sam$=%sam%%% if "%new%" NEQ "" call set new=%%new:$sam$=%sam%%% if "%prof%" NEQ "" ( if "%old%" NEQ "" call set newprof=%%prof:%old%=%new%%% ) ELSE ( if "%old%" EQU "" call set newprof=%new% ) if "%newhome%" EQU "#=#" set newhome= if "%newprof%" EQU "#=#" set newprof= if "%newdrv%" EQU "#=#" set newdrv= if "%newhome%" EQU "#" set newhome= if "%newprof%" EQU "#" set newprof= if "%newdrv%" EQU "#" set newdrv= if "%home%" NEQ "%newhome%" set OK=Y if "%prof%" NEQ "%newprof%" set OK=Y if "%drive%" NEQ "%newdrv%" set OK=Y if "%OK%" EQU "N" goto :EOF if "%newhome%" EQU "nul" if "%Holdsrv%" NEQ "" set newhome=-hmdir ""&goto out1 if "%newhome%" NEQ "" if "%Holdsrv%" NEQ "" set newhome=-hmdir "%newhome%"&goto out1 if "%newhome%" NEQ "" if "%Holdsrv%" EQU "" set newhome=-hmdir "%newhome%" :out1 if "%newprof%" EQU "nul" if "%Poldsrv%" NEQ "" set newprof=-profile ""&goto out2 if "%newprof%" NEQ "" if "%Poldsrv%" NEQ "" set newprof=-profile "%newprof%"&goto out2 if "%newprof%" NEQ "" if "%Poldsrv%" EQU "" set newprof=-profile "%newprof%" :out2 if "%newdrv%" EQU "nul" if "%Holddrv%" NEQ "" set newdrv=-hmdrv ""&goto out3 if "%newdrv%" EQU ":" if "%Holddrv%" NEQ "" set newdrv=-hmdrv ""&goto out3 if "%newdrv%" NEQ "" set newdrv=%newdrv::=%: if "%newdrv%" NEQ "" if "%Holddrv%" NEQ "" set newdrv=-hmdrv %newdrv%&goto out3 if "%newdrv%" NEQ "" if "%Holddrv%" EQU "" set newdrv=-hmdrv %newdrv% :out3 if /i "%sam%" NEQ "test" goto :EOF @echo dsmod user "%user%" %newdrv% %newhome% %newprof% dsmod user "%user%" %newdrv% %newhome% %newprof%



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