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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
RobSampsonConnect With a Mentor Commented:
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
bsharathAuthor Commented:
Thanks Rob
But groups are in the root Domain
0
 
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
 
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
All Courses

From novice to tech pro — start learning today.