Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing Primary Group for an entire OU

Status
Not open for further replies.

jasongrubbs

IS-IT--Management
Jan 22, 2007
4
US
Hello, I have a unique question/issue.

We are flattening our domain from 1 parent and 2 child domains to 1 main domain and need to move our user accounts. We are utilizing the MoveTree command which is working fine.

When we try to move the accounts using movetree, we get an error about moving accounts that are bound to domain specific groups that are not universal (Domain Users). I found a couple different VB scripts to change the primary group for a user but I have not been able to find a utility to change the primary group for an entire OU.

Any easy way to accomplish this?

This is the code for the script to change one users primary group:
Set objUser = GetObject _
("LDAP://cn=alind,ou=Class of 2021,ou=LPG,dc=acad,dc=pgsd,dc=org")

Set objGroup = GetObject _
("LDAP://cn=Students,dc=acad,dc=pgsd,dc=org")

objGroup.GetInfoEx Array("primaryGroupToken"), 0

objUser.primaryGroupID = objGroup.primaryGroupToken
objUser.SetInfo
 
This script might help you out...basically it loops through a list of usernames within a text file until the text file is empty. So, for you - just create a text file of all the users that you want modified - then run a script similar to this one. Again, I hope this helps out.

------------------------------------------------
option explicit

'Declair constants
Const ForReading = 1

'Declair variables
Dim strScriptPath, strFileName, strDirectory, strUsername
Dim objFileSO, objLogFile, objTextFile, objUser, objGroup

strScriptPath = Wscript.ScriptFullName

'This next line defines the name of the text file.
'In this text file, place a list of all the usernames
'that you wanted edited. Place one name on each line.
'Also, make sure that this text file is saved in the same
'folder that you are running the script from.
strFileName = "OU - List.txt"

Set objFileSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFileSO.GetFile(strScriptPath)
strDirectory = objFileSO.GetParentFolderName(objLogFile) & "\" & strFileName

'Open text file of usernames
Set objTextFile = objFileSO.OpenTextFile(strDirectory, ForReading, True)


'Go through entire list of usernames found within text file
Do until objTextFile.AtEndOfStream

'Read text file line
strUsername = objTextFile.Readline

'***
'Start Your script (slightly modified)
'***
Set objUser = GetObject _
("LDAP://cn=" & strUsername & ",ou=Class of 2021,ou=LPG,dc=acad,dc=pgsd,dc=org")

Set objGroup = GetObject _
("LDAP://cn=Students,dc=acad,dc=pgsd,dc=org")

objGroup.GetInfoEx Array("primaryGroupToken"), 0

objUser.primaryGroupID = objGroup.primaryGroupToken
objUser.SetInfo
'***
'End Your script (slightly modified)
'***

Loop

'Close text file
objTextFile.Close

'Clean up memory
Set strScriptPath = Nothing
Set strFileName = Nothing
Set strDirectory = Nothing
Set strUsername = Nothing
Set objFileSO = Nothing
Set objLogFile = Nothing
Set objTextFile = Nothing
Set objUser = Nothing
Set objGroup = Nothing
 
Hey Keyster86,

Do I have to place the txt file anywhere specific?
 
ok, after reading the vb code, I found where to place the text file and the script works!

I have one more issue, is there some code I can add to remove the user from Domain Users after setting their primary group?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top