|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
VBScript - Add users to Domain group based on attribute
I'm looking to write a little code I can run weekly that will be able to update a Distribution list so that any new users who have the job title "Contractors" will be placed into an existing DL name "DL_Contractors" Existing members who have had their title changed to anything else should also be removed.
I've been able to write the following to purge all users from the group. If it can be written by a guru that it doesn't need to dump existing members who still match the criteria that would be great! Code:
Const ADS_PROPERTY_CLEAR = 1
Set objGroup = GetObject("LDAP://CN=DL_Conractors,OU=Groups,DC=testdomain,DC=com")
objGroup.PutEx ADS_PROPERTY_CLEAR, "member", 0
objGroup.SetInfo
How do I write the part which enumerates all User objects regardless of their location in AD and if their title = "Contractor" add them to the group. Thank you in advance |
|
#2
|
|||
|
|||
|
I think if you dig through some of my early posts, you'll find code I wrote for a web system using vbscript, which might have some useful info...been too long since I've looked at it but there might be some stuff there that you may find quite useful.
|
|
#3
|
|||
|
|||
|
Quote:
Thanks q97. I quickly browsed your earlier posts and saw the code you wrote for the web system. Looks very slick, but didn't see what I needed. |
|
#4
|
|||
|
|||
|
Update
Below is some code I've found/modified, but I've been unable to make it work properly. I don't get any errors, but it doesn't seem to do anything and it doesn't end.
Code:
On Error Resume Next
strgroup = "LDAP://cn=contractorTest,ou=Groups,dc=domain,dc=com"
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = _
"SELECT ADsPath FROM strgroup WHERE objectCategory='user' " & _
"AND title='Contractor'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Set objGroup = GetObject(strgroupname)
Do Until objRecordSet.EOF
objGroup.Add(objRecordSet.Fields("ADsPath").Value)
objRecordSet.MoveNext
Loop
Thanks in advance |
|
#5
|
|||
|
|||
|
Watch your syntax:
Code:
"SELECT ADsPath FROM strgroup WHERE objectCategory='user' " & _ "AND title='Contractor'" Should be: Code:
"SELECT ADsPath FROM " & strgroup & " WHERE objectCategory='user' " & _ "AND title='Contractor'" Does that work? |
|
#6
|
||||
|
||||
|
you are also missng single quotes around the ldap string...plus if there are no recs returned you do not have a condition...and you are missing the splat(*) for title
i am afraid the code within the do while loop will not do what you want... you'll probably find the MS repository of great use....click active directory then find the example you want...if still having problems...post back...good luck note...you can get the root on-the-fly...see function below... and for debugging...comment out the on error resume next http://www.microsoft.com/technet/sc...t.mspx?mfr=true Code:
Function ReturnRootDSE
Set oRootDSE = GetObject("LDAP://RootDSE")
ReturnRootDSE = oRootDSE.Get("defaultNamingContext")
Set oRootDSE = Nothing
End Function
'On Error Resume Next
strRootDSE ="LDAP://" & ReturnRootDSE
strgroup = "LDAP://cn=contractorTest,ou=Groups," & strRootDSE
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = _
"SELECT ADsPath " & _
"FROM '" & strgroup & "' " & _
"WHERE objectCategory='user' AND title='Contractor*'"
Set objRecordSet = objCommand.Execute
If Not objRecordSet.EOF Then
Set objGroup = GetObject(objRecordSet("adspath"))
Do Until objRecordSet.EOF
objGroup.Add(objRecordSet.Fields("ADsPath").Value)
objRecordSet.MoveNext
Loop
Else
msgBox("No recs")
End If
__________________
Please give respect to those that helped solve an issue by clicking on the reputation icon
Last edited by keep_it_simple : October 21st, 2008 at 03:35 AM. |
![]() |
| Viewing: ASP Free Forums > System Administration > Windows Scripting > VBScript - Add users to Domain group based on attribute |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|