Move groups from 1 ou to another. I have the user script. Can anyone help me with a group change.

Hi,

Move groups from 1 ou to another. I have the user script. Can anyone help me with a group change.
The below script works fine for Users
Can the same be changed to work on groups.

Regards
Sharath

:: SCRIPT START
@ECHO OFF
SETLOCAL EnableDelayedExpansion

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

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

LVL 11
bsharathAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DDBCommented:
can you use shell script with FOR LOOP

::to delete user from Group
net group <groupname> <userName> /domain /delete

::to Add user
net group <groupname> <userName> /domain /add

something like this
@echo off
for /f "delims=|" %%i in (userList.txt) do (
net group <groupnametoremove> %%i /domain /delete
net group <groupnametoadd> %%i /domain /delete)

???
0
RobSampsonCommented:
Try this, with the Group Names in groupnames.txt

Regards,

Rob.
:: SCRIPT START 
@ECHO OFF 
SETLOCAL EnableDelayedExpansion 
 
:: Following variables required to be set with actual values 
SET NewOU=M00 
 
IF EXIST GroupMoveRpt.txt DEL /F /Q GroupMoveRpt.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 GroupNames.txt') DO ( 
      ECHO Processing: %%u 
      DSQuery Group -name "%%u" |Find /I "CN=" >NUL 
      IF NOT ErrorLevel 1 ( 
            DSQuery Group -name "%%u" |DSMove -newparent !OUDN! >>GroupMoveRpt.txt 
      )ELSE (ECHO *ERROR* '%%u' Not Found in Active Directory.)) 
 
ECHO.&ECHO Script complete. Check 'GroupMoveRpt.txt' file. 
:EndScript 
IF EXIST OUTmpVar.txt DEL /F /Q OUTmpVar.txt 
ENDLOCAL 
EXIT /B 0 
:: *** SCRIPT END ***

Open in new window

0
bsharathAuthor Commented:
Thanks Rob
But groups are in the root Domain
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

RobSampsonCommented:
If you run this from a command prompt to find a specific group name, what is the output:
adfind -gcb -f "&(objectclass=group)(name=YourGroupName)" distinguishedName

Replace YourGroupName with one of the group names in the root domain, and see if it finds it.

Regards,

Rob.
0
bsharathAuthor Commented:
Yes 1 object found
0
RobSampsonCommented:
OK, try this.

Regards,

Rob.
:: SCRIPT START  
@ECHO OFF  
SETLOCAL EnableDelayedExpansion  
  
:: Following variables required to be set with actual values  
SET NewOU=OU=TestOU,DC=Maroondah,DC=Local
  
IF EXIST GroupMoveRpt.txt DEL /F /Q GroupMoveRpt.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 GroupNames.txt') DO (     
      ECHO Processing: %%u
      adfind -gcb -f "&(objectclass=group)(name=%%u)" -dn | find /I "dn:" >NUL
      IF NOT ErrorLevel 1 (
        adfind -gcb -f "&(objectclass=group)(name=%%u)" -dn | find /I "dn:" > GroupDN.txt
      	for /f "tokens=*" %%k in (GroupDN.txt) DO (
      			for /f "tokens=1 delims=;" %%l in ("%%k") DO (
      				for /f "tokens=2 delims=:" %%m in ("%%l") do (
      					rem echo moving %%m to !OUDN!
      					DSMove %%m -newparent !OUDN! >>GroupMoveRpt.txt
      				)
      			)
      	)    
      )ELSE (ECHO *ERROR* '%%u' Not Found in Active Directory.)
)   
ECHO.&ECHO Script complete. Check 'GroupMoveRpt.txt' file.  
:EndScript  
IF EXIST OUTmpVar.txt DEL /F /Q OUTmpVar.txt  
IF EXIST GroupDN.txt DEL /F /Q GroupDN.txt  
ENDLOCAL  
EXIT /B 0  
:: *** SCRIPT END ***

Open in new window

0
bsharathAuthor Commented:
Rob i get this

   Processing: Coi_fs

AdFind V01.36.00cpp Joe Richards (joe@joeware.net) February 2007


AdFind V01.36.00cpp Joe Richards (joe@joeware.net) February 2007

dsmove failed:`Distribution' is an unknown parameter.
type dsmove /? for help.
Script complete. Check 'GroupMoveRpt.txt' file.
Press any key to continue . . .
0
RobSampsonCommented:
Ah, that group must have spaces. I forgot the quotes.

Try this.

Regards,

Rob.
:: SCRIPT START   
@ECHO OFF   
SETLOCAL EnableDelayedExpansion   
   
:: Following variables required to be set with actual values   
SET NewOU=OU=TestOU,DC=Maroondah,DC=Local 
   
IF EXIST GroupMoveRpt.txt DEL /F /Q GroupMoveRpt.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 GroupNames.txt') DO (      
      ECHO Processing: %%u 
      adfind -gcb -f "&(objectclass=group)(name=%%u)" -dn | find /I "dn:" >NUL 
      IF NOT ErrorLevel 1 ( 
        adfind -gcb -f "&(objectclass=group)(name=%%u)" -dn | find /I "dn:" > GroupDN.txt 
        for /f "tokens=*" %%k in (GroupDN.txt) DO ( 
                        for /f "tokens=1 delims=;" %%l in ("%%k") DO ( 
                                for /f "tokens=2 delims=:" %%m in ("%%l") do ( 
                                        rem echo moving "%%m" to !OUDN! 
                                        DSMove "%%m" -newparent !OUDN! >>GroupMoveRpt.txt 
                                ) 
                        ) 
        )     
      )ELSE (ECHO *ERROR* '%%u' Not Found in Active Directory.) 
)    
ECHO.&ECHO Script complete. Check 'GroupMoveRpt.txt' file.   
:EndScript   
IF EXIST OUTmpVar.txt DEL /F /Q OUTmpVar.txt   
IF EXIST GroupDN.txt DEL /F /Q GroupDN.txt   
ENDLOCAL   
EXIT /B 0   
:: *** SCRIPT END ***

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bsharathAuthor Commented:
Thanks Rob works perfect
Any help on others
0
bsharathAuthor Commented:
Hi Rob..
I dont know why groups are not moving but the log shows success.
Any help please...Is this specific to a group type like distribution or security or mail enabled only ?
0
RobSampsonCommented:
The DSMove command should not be showing success if it doesn't actually move the group.....do you have to wait for replication to occur before you can see that it has moved?

With a group that hasn't moved, is it a Distribution group, Security group, or mail enabled group?  Can you check a few?

Rob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.