简体   繁体   中英

VBS Active Directory (2003) Move users from one set of groups to another

I'm using this VBS to move a flat list of user from one group to another. So far so good. I'm a rookie when it comes to VB. The challenge is that I have 20 different Sync Groups (Sync01-Sync20) and 20 Mig groups (Mig01-Mig20). I need an extention of the code that identifies witch Sunc group the users are member of. Then "translate" this into the correct Mig group. Any one? :)

DIM objGroup, objGroup2, objRootLDAP, objFSO, objInput, objConnection, objCommand 
DIM strUser 

On Error Resume Next 

Set objRootLDAP = GetObject("LDAP://rootDSE") 
Set objConnection = CreateObject("ADODB.Connection") 
objConnection.Open "Provider=ADsDSOObject;" 
Set objCommand = CreateObject("ADODB.Command") 
objCommand.ActiveConnection = objConnection 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objInput = objFSO.OpenTextFile("users.txt") 
Set objGroup = GetObject("LDAP://cn=Sync01,ou=Huset,dc=bb,dc=net") 
Set objGroup2 = GetObject("LDAP://cn=Mig01,ou=Huset,dc=bb,dc=net") 

Do Until objInput.AtEndOfStream 
strUser = ObjInput.ReadLine 

objCommand.CommandText = "<LDAP://dc=bb,dc=net>;(&(objectCategory=person)(sAMAccountName=" & strUser & "));distinguishedName,userAccountControl;subtree" 

Set objRecordSet = objCommand.Execute 

If objRecordSet.RecordCount = 0 Then 
    MsgBox strUser & " was not found!" & VbCrLf & "Skipping", VbOkOnly,"User Not Found" 
Else 
    strDN = objRecordSet.Fields("distinguishedName") 
    Set objUser = GetObject("LDAP://" & strDN) 
    objGroup.Remove(objUser.AdsPath) 
    objGroup2.Add(objUser.AdsPath)
End If 
Loop 

WScript.Echo "Complete"

If all you want is transfer group members from each Sync group to the correspondig Mig group, something like this should do:

Set fso = CreateObject("Scripting.FileSystemObject")

Set userlist = CreateObject("Scripting.Dictionary")
userlist.CompareMode = vbTextCompare
Set f = fso.OpenTextFile("users.txt")
Do Until f.AtEndOfStream
  userlist.Add f.ReadLine, True
Loop
f.Close

domain = GetObject("LDAP://rootDSE").Get("defaultNamingContext")

For i = 1 To 20
  n = Right("0" & i, 2)
  Set gSync = GetObject("LDAP://CN=Sync" & n & ",OU=Huset," & domain)
  Set gMig  = GetObject("LDAP://CN=Mig" & n & ",OU=Huset," & domain)
  For Each m In gSync.Members
    Set user = GetObject(m.ADsPath)
    If userlist.Exists(user.sAMAccountName) Then
      gMig.Add(m.ADsPath)
      gSync.Remove(m.ADsPath)
    End If
  Next
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM