Copy and append domain group privledges from old account to new account

I am in need of having to copy and / or append domain group privledges from old usernames to new usernames.  There are 2000 accounts that need to be done.  I have a text file of the old name and the new name to read from but have no idea how to incorporate that into anything useful.

I do have a batch file that uses windows 2003 tools to get this done.  It is a simple blah.bat old name new name and it does it all.  (Script below)  Does anyone know where I can find a script to do what I need.   VB would be better as it would tie in with some other things being done but I am not picky at this point at all.

Here is the current batch file that is doing it with win2k3 tools

@echo off
if {%2}=={} @echo Syntax: Call CopyDomGroups From Add_or_Replace [To]&goto :EOF
set from=%1
set ar=%2
set to=%username%
if not {%3}=={} set to=%3
if /i "%ar%" EQU "a" goto arok
if /i "%ar%" NEQ "r" @echo Syntax: Call CopyDomGroups From MergeReplace [To]&goto finish
for /f "Tokens=*" %%u in ('dsquery user -samid %from%') do set fdn=%%u
if not defined fdn @echo CopyDomGroups %from% not found.&goto finish
for /f "Tokens=*" %%u in ('dsquery user -samid %to%') do set tdn=%%u
if not defined tdn @echo CopyDomGroups %to% not found.&goto finish
if /i "%ar%" EQU "a" goto add
for /f "Tokens=*" %%a in ('dsget user %fdn% -memberof') do @echo %%a>>%TEMP%\CopyDomGroups.tmp
for /f "Tokens=*" %%b in ('dsget user %tdn% -memberof ^|findstr /i /l /v /g:%TEMP%\CopyDomGroups.tmp') do set DN=%%b&call :rparse
for /f "Tokens=*" %%a in ('dsget user %tdn% -memberof') do @echo %%a>>%TEMP%\CopyDomGroups.tmp
for /f "Tokens=*" %%b in ('dsget user %fdn% -memberof ^|findstr /i /l /v /g:%TEMP%\CopyDomGroups.tmp') do set DN=%%b&call :aparse
if exist %TEMP%\CopyDomGroups.tmp del /a %TEMP%\CopyDomGroups.tmp
goto :EOF
dsmod group %DN% -rmmbr %tdn% >nul
goto :EOF
dsmod group %DN% -addmbr %tdn% >nul

Thanks in advance for any and all help tossed in my direction :)

Ypto Gink
Who is Participating?
mpemberton5Connect With a Mentor Commented:
If you have VB.NET it should be pretty easy.

1. Read in your list of old users and new users into two arrays
2. For each user (each array entry)
   a. Read the old users' DirectoryEntry
   b. Read the new users' DirectoryEntry
   c. Query the groups that the old user is associated with
   d. Add the new user to each group (filter out any that you don't want)
   e. move to next user (array entry)

Here's some code that will return the groups that the passed user belongs into:
    Public Function GetUserGroupMembership(ByVal strUser As String) As StringCollection
        Dim groups As StringCollection = New StringCollection

            Dim obEntry As DirectoryEntry = New DirectoryEntry(yourLDAPpath)
            Dim srch As DirectorySearcher = New DirectorySearcher(obEntry, "(&(objectClass=user)(samAccountName=" & strUser & "))")
            Dim res As SearchResult = srch.FindOne()

            Dim obUser As DirectoryEntry = New DirectoryEntry(res.Path)
            Dim obGroups As Object = obUser.Invoke("Groups")

            Dim ob As Object
            For Each ob In obGroups
                Dim obGpEntry As DirectoryEntry = New DirectoryEntry(ob)
            'End If

        Return groups
    End Function

And here is some code that will allow you to add a user to a group:
Dim MyGroup As DirectoryEntry = ADHelper.GetUser(userName)
MyGroup.Properties("member").Add(currUser.Properties("distinguishedName").Value) ' Add user to group

That should give you enough ammo to tackle this project.  Do some research on the use of DirectoryServices works.  It's relatively easy.  Let me know if you have any additional questions.

bstolteAuthor Commented:
Sorry for the late response, I actually gave up but did not want to leave you hanging out there.  Thanks for your effort and sorry about my own shortcomings on making this work.  Thanks.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.