Announcement

Collapse
No announcement yet.

Creating Global Groups from a text file

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

  • Creating Global Groups from a text file

    I have been at this for a couple of hours and just can't figure what is going wrong.

    Code:
    '==========================================================================
    ' AUTHOR: David R
    ' DATE  : 9/2/2009
    '==========================================================================
    
    Const ForReading = 1
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile _
    ("davidtestgrlst.txt",ForReading)
    
    
    strText = objTextFile.ReadAll
    
    objTextFile.Close
    
    arrGroups = Split(strText, vbCrLf)
    
    Dim objAD, objGroup, objNewGroup, objUser, objRootDSE
    Dim objDomain, objOU
    Dim strGroup, strNewGroup, strPath, strUser
    Dim strOU, strDNSDomain
    Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
    Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000
    
    Set objRootDSE = GetObject("LDAP://RootDSE")
    strDNSDomain = objRootDSE.Get("DefaultNamingContext")
    strOU ="OU=LDAPApplications,OU=Groups,"
    
    strPath ="LDAP://" & strOU & strDNSDomain
    Set objOU = GetObject(strPath)
    
    For Each strGroup in arrGroups
    
    On Error Resume Next
     
    Set objNewGroup = objOU.Create("Group", "cn="& GroupName)
    objNewGroup.Put "sAMAccountName", strGroup
    objNewGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _
    ADS_GROUP_TYPE_SECURITY_ENABLED
    objNewGroup.SetInfo
    
    Next
    If anyone can point me to my error that would be great. The script runs but doesn't create the group.

    Thanks,
    David

  • #2
    Re: Creating Global Groups from a text file

    ok so here is an update:

    I removed "On error resume next" and got this error Error: An invalid DN Syntax has been specified Source: (null)

    Thanks,

    Comment


    • #3
      Re: Creating Global Groups from a text file

      I spent 4 hours on

      Comment


      • #4
        Re: Creating Global Groups from a text file

        This will get you on track.. I mostly use excel to import things...
        In this script you only need to change ditrubtion groups to global groups property

        hxxp://social.technet.microsoft.com/Forums/en-US/ITCG/thread/1f9320e0-afaf-4387-8de5-97e4e80d5f9d

        bio...

        Comment


        • #5
          Re: Creating Global Groups from a text file

          I don't see you appending the full path to cn=groupname -- it should (IIRC) go through OU and DC as well:
          cn=groupname,ou=targetou,dc=company,dc=corp

          (obviously put your own info in)
          Tom Jones
          MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
          PhD, MSc, FIAP, MIITT
          IT Trainer / Consultant
          Ossian Ltd
          Scotland

          ** Remember to give credit where credit is due and leave reputation points where appropriate **

          Comment


          • #6
            Re: Creating Global Groups from a text file

            Here,
            Code:
            Set objNewGroup = objOU.Create("Group", "cn="& GroupName)
            where "cn="& GroupName should be "cn="& strGroup because "GroupName" is an undefined variable.


            Furhermore, to prevent possible other issues you can consider some the sample code in the script below.
            Code:
            Const ForReading = 1
            Dim objTextFile, strGroup, intSize
            
            With CreateObject("Scripting.FileSystemObject")
               Set objTextFile = .OpenTextFile _
                  ("c:\groupnames.txt", ForReading)
               intSize = 0
               Do Until objTextFile.AtEndOfStream
                  strGroup = trim(objTextFile.ReadLine)
                  If strGroup <> Empty then
                    ReDim Preserve arrGroups(intSize)
                    arrGroups(intSize) = strGroup
                    intSize = intSize + 1
                  End If
               Loop
               objTextFile.Close: Set objTextFile = Nothing
            End With
            If not IsArray(arrGroups) _
               Then wscript.echo "No Groups!": wscript.quit(0)
            
            '## check point ##
            ' For Each strGroup in arrGroups
            '    wsh.echo "["& strGroup &"]"
            ' Next        '#####
            
            Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
            Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000
            Dim objRootDSE, strDNSDomain, adsPathOU, objOU
            Dim objNewGroup
            
            Set objRootDSE = GetObject("LDAP://RootDSE")
            strDNSDomain = objRootDSE.Get("DefaultNamingContext")
            
            adsPathOU = "LDAP://OU=LDAPApplications,OU=Groups," & strDNSDomain
            
            On Error Resume Next
            Set objOU = GetObject(adsPathOU)
            If err.Number <> 0 Then wscript.echo _
               "specified OU does not exist!": wscript.quit(0)
            
            For Each strGroup in arrGroups
               Set objNewGroup = objOU.Create("Group", "cn=" & strGroup)
               objNewGroup.Put "sAMAccountName", strGroup
               objNewGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _
                 ADS_GROUP_TYPE_SECURITY_ENABLED
            
               objNewGroup.SetInfo
            Next
            
            wscript.echo "done"
            \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

            Working...
            X