• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 476
  • Last Modified:

Copy Computer Objects To Group

Hello all,

Without giving a ton of details, I'm hoping someone can provide me with a solution to copy many computer objects to a group.  This will be different OUs to different groups and will be setup to run everyday to get new computer objects that have been added.

Let me thank you in advance for any help you may give.
0
NECBP
Asked:
NECBP
  • 3
  • 2
1 Solution
 
RobSampsonCommented:
Hi, how will you know which group to add the computer to?

I assume you will be running this against the default Computers container
CN=Computers,DC=Yourdomain,DC=com

Is the group you want to put them in going to be defined by some part of the computer name?  For example, if the last four letters of a computer are MKTG you will put them in the group
CN=MarketingGroup,OU=Marketing,OU=Departments,DC=YourDomain,DC=com

Regards,

Rob.
0
 
NECBPAuthor Commented:
Yes... In our OU, each unit has a sub OU with a Computers OU.  I also have an OU that contains my Groups.  So, I will edit the script to run on each unit OU/Computer OU and it will then copy those computers to my Groups OU where each unit as a group that is named the same as their OU.  Hopefully, that'll be the best way to keep track of which ones are in the group.   Confusing I know.  :/
0
 
RobSampsonCommented:
I see, so you're not moving any computer objects, you're just adding them as members of the group that has the same name as the OU they're in.  Easy enough then ;-)

Try this out.  I strongly suggest that you test this on a test OU and test Computers and a test Group before putting it into production.

You will see these lines that you need to change:
objOUDefs.Add "LDAP://OU=Computers,OU=Marketing,DC=fabrikam,DC=com", "LDAP://CN=Marketing,OU=Groups,DC=fabrikam,DC=com"
objOUDefs.Add "LDAP://OU=Computers,OU=IT,DC=fabrikam,DC=com", "LDAP://CN=IT,OU=Groups,DC=fabrikam,DC=com"

Open in new window


according to the OU of your computers and the group that you want to put them in.

Also note this:
' Set strMode to "APPEND" or "REPLACE" to specify the group members are replaced or appended
'strMode = "APPEND"
strMode = "REPLACE"

Open in new window


where you can tell the script whether to replace the group membership each time you run it, or append the objects to the existing group.  I would assume in most cases you will just replace it, to keep it updated.

Regards,

Rob.

Const ADS_PROPERTY_CLEAR = 1
Const ADS_PROPERTY_UPDATE = 2
Const ADS_PROPERTY_APPEND = 3

' Set strMode to "APPEND" or "REPLACE" to specify the group members are replaced or appended
'strMode = "APPEND"
strMode = "REPLACE"

Set objOUDefs = CreateObject("Scripting.Dictionary")
' Specify OU as the "key", and the group for computers to go into as the "item"
objOUDefs.Add "LDAP://OU=Computers,OU=Marketing,DC=fabrikam,DC=com", "LDAP://CN=Marketing,OU=Groups,DC=fabrikam,DC=com"
objOUDefs.Add "LDAP://OU=Computers,OU=IT,DC=fabrikam,DC=com", "LDAP://CN=IT,OU=Groups,DC=fabrikam,DC=com"

If UCase(strMode) = "APPEND" Then
	strMode = ADS_PROPERTY_APPEND
ElseIf UCase(strMode) = "REPLACE" Then
	strMode = ADS_PROPERTY_UPDATE
Else	' set a default of append if the mode is unknown
	strMode = ADS_PROPERTY_APPEND
End If

For Each strOUPath In objOUDefs
	Set objOU = GetObject(strOUPath)
	Set objGroup = GetObject(objOUDefs(strOUPath))

	strOUMembers = ""
	For Each objMember In objOU
		If strOUMembers <> "" Then strOUMembers = strOUMembers & "|"
		strOUMembers = strOUMembers & objMember.distinguishedName
	Next
	
	' Add the accounts from the OU to the group
	WScript.Echo "Adding the following members to " & objGroup.CN & ":" & vbCrLf & Join(Split(strOUMembers, "|"), vbCrLf)
	objGroup.PutEx strMode, "member", Split(strOUMembers, "|")
	objGroup.SetInfo
Next

Open in new window

0
 
NECBPAuthor Commented:
Rob... I can't thank you enough!  I really wish there were words to describe your skill.

Thank you!
Thank you!!
Thank you!!!
0
 
RobSampsonCommented:
No problem. Thanks for the grade, and the compliment.  Much appreciated.

Rob.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now