Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

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

Avatar of DDB
DDB
Flag of United States of America image

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)

???
Avatar of RobSampson
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

Avatar of bsharath

ASKER

Thanks Rob
But groups are in the root Domain
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.
Yes 1 object found
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

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 . . .
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Rob works perfect
Any help on others
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 ?
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.