Announcement

Collapse
No announcement yet.

Add a user to the managed by

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Add a user to the managed by

    Hi,

    I need a script which will let me add a user to the managed by distribution Lists.

    I have the username and the group name (tabulation separated) in a spreedsheet file need to add the user to these groups as managed by.

    Group1 user
    Group2 user
    Group3 user

    Thanks

    Thomas

  • #2
    Re: Add a user to the managed by

    The name of the Group is unique in the domain, so the group names that are in the file are usefull. But what is "User", which attribute(s) is that referring too? (see also this thread: http://forums.petri.com/showthread.php?t=43428)

    What you meant by "tabulation separated in a spreedsheet". Is the file a text-formatted file containing tab separated values what is opened by Excel - Or, is it really a XLS-formatted file and on the worksheet there is just one column which contain two tab separated values at every row??


    \Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Add a user to the managed by

      is the same user who will manage multiple distribution lists.
      It's just that "Is the file a text-formatted file containing tab separated values what is opened by Excel"


      The Excel has two columns: A= Group name and B = sAMAccountName.

      I want the sAMAccountName to be imported into the Managed By attribut at the Group name.

      Thomas
      Last edited by tdbchess; 22nd January 2010, 13:51.

      Comment


      • #4
        Re: Add a user to the managed by

        Here is a script that read the text-file containing tab-separated-values and, get te name values on each line.
        Then it translates the sAMAcountNames to the distinguishedName of the object.

        It uses the distinguishedName of the group to make a LDAP binding to the object. Then it uses the distinguishedName notation of the user to add as the Manager of the group.

        Assuming the group names in your list are also the 'sAMAcountName' (pre-Windows 2000 Name) of the groups. But just in case, if the groupname could not be found the script will re-try to find the provided group name as 'common name' this time.

        Code:
        ' Constants for the NameTranslate object.
        Const ADS_NAME_INITTYPE_GC = 3
        Const ADS_NAME_TYPE_NT4 = 3
        Const ADS_NAME_TYPE_1779 = 1
        
        Const ForReading = 1
        
        Dim oConnection, strDNSDomain
        
        ' Tab separated txt-file
        sTargetFile = "C:\path\file name"
        
        ' Determine DNS domain name.
        Set objRootDSE = GetObject("LDAP://RootDSE")
        strDNSDomain = objRootDSE.Get("defaultNamingContext")
        
        ' Retrieve the NetBIOS name of the domain.
        Set objTrans = CreateObject("NameTranslate")
        objTrans.Init ADS_NAME_INITTYPE_GC, ""
        objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
        strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)
        ' Remove trailing backslash.
        strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1)
        
        ' The values in the file are tab separated,
        ' Read lines from file and break into Values
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFile = objFSO.OpenTextFile(sTargetFile, ForReading)
        Do Until objFile.AtEndOfStream
           strLine = trim(objFile.ReadLine)
        
           If Len(strLine) > 0 Then
             arVal = split(strLine, vbTab)
        
             strGroup = arVal(0)
             strUser = arVal(1)
        
             If Left(strGroup, 1) = chr(34) and right(strGroup, 1) = chr(34) _
               then strGroup = mid(strGroup, 2, len(strGroup)-2)
             If Left(strUser, 1) = chr(34) and right(strUser, 1) = chr(34) _
               then strUser = mid(strUser, 2, len(strUser)-2)
        
             strUserDn = TranslateToDN(strNetBIOSDomain & "\" & strUser)
        
             strGroupDn = TranslateToDN(strNetBIOSDomain & "\" & strGroup)
        
             If trim(strGroupDn) = "" then _
               strGroupDn = FindObject("group", "cn", strGroup)
        
             If Len(trim(strGroupDn)) > 0 Then
               ' Bind to the Group object in Active Directory with the LDAP provider.
               Set objGroup = GetObject("LDAP://" & strGroupDn)
        
               objGroup.Put "managedBy", strUserDn
               objGroup.SetInfo
             End If
        
           End If
        Loop
        
        Set objRootDSE = Nothing
        Set objFile = Nothing
        
        
        Function TranslateToDN(NTaccountname)
           On Error Resume Next
           objTrans.Set ADS_NAME_TYPE_NT4, NTaccountname
           If Err.number = 0 Then
             TranslateToDN = objTrans.Get(ADS_NAME_TYPE_1779)
             ' Escape any "/" characters with backslash escape character.
             ' All other characters that need to be escaped will be escaped.
             TranslateToDN = Replace(TranslateToDN, "/", "\/")
           Else
             Err.clear
             TranslateToDN = Empty
           End If
           On Error Goto 0
        End Function
        
        
        Function FindObject(sObjectClass, sAttribute, strVal)
           Dim oRecordset
           dim oc
           Select Case LCase(sObjectClass)
             Case "user"
                 oc = "(objectClass=user)(objectCategory=person)"
             Case "computer"
                 oc = "(objectClass=computer)"
             Case "group"
                 oc = "(objectClass=group)"
             Case Else
                 oc = Empty
           End Select
        
           dim at
           Select Case LCase(sAttribute)
             Case "cn", "common name"
                 at = "(cn=" & strVal & ")"
             Case "upn", "userprincipalName"
                 at = "(userprincipalName=" & strVal & ")"
             Case "samacountname"
                 at = "(sAMAcountName=" & strVal & ")"
             Case Else
                 at = Empty
           End Select
        
           FindObject = Empty
           If Not IsObject(oConnection) Then
             Set oConnection = CreateObject("ADODB.Connection")
             oConnection.Provider = "ADsDSOObject"  'The ADSI OLE-DB provider
           End If
        
           Set oRecordset = CreateObject("ADODB.Recordset")
           oConnection.Open "ADs Provider"
        
           ' LDAP Search
           strQuery = "<LDAP://" & strDNSDomain & ">;(&" & oc _
             & at & ");distinguishedName;subtree"
           On Error Resume Next
           Set oRecordset = oConnection.Execute(strQuery)
        
           If oRecordset.EOF And oRecordset.BOF Then
             rem "No matching group found " & sCommonName
           Else
              While Not oRecordset.EOF
               'bind to the Group object using LDAP provider
               FindObject = oRecordset.Fields("distinguishedName").Value
               FindObject = Replace(FindObject, "/", "\/")
               oRecordset.MoveNext
             Wend
           End If
           oConnection.Close
           Set oRecordset = Nothing
        End Function
        \Rems

        ~
        Similar Post:
        import data from excel
        ~
        Last edited by Rems; 31st January 2010, 20:19.

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Add a user to the managed by

          Thank you for the the response.

          Comment


          • #6
            Re: Add a user to the managed by

            Hello, thank you for youre help but the script doesnt check the tick "Manager can update membership list"

            How can i modify in the script to check this choice ?

            Thanks

            Comment

            Working...
            X