VBscript Help please - creating AD groups

Discussion in 'Scripting' started by Clubsprint, Sep 17, 2008.

  1. Clubsprint

    Clubsprint Guest

    G'day all
    I've hobled a script together from the MS examples but it's not working.
    I want to create a list of groups in an OU.
    I run the script and it asks for the input and then does nothing.

    ------start script--------

    On Error Resume Next

    Dim OrgUnit, GroupName, fso, f, objOU, objGroup

    Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
    Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000

    GroupName = InputBox("Enter the name of the Group Name you wish to create")

    Set objOU = GetObject("LDAP://ou=Workstation
    Groups,ou=Workstations,dc=int,dc=dept,dc=gov")
    Set objGroup = objOU.Create("Group", "cn=ChairmanWS")

    objGroup.Put "sAMAccountName", "HRStaff"
    objGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _
    ADS_GROUP_TYPE_SECURITY_ENABLED
    objGroup.SetInfo

    ------start script--------


    what does the part
    objGroup.Put "sAMAccountName", "HRStaff"
    do? I'm not sure how to change this to make it relevent to my site
    I'd really like to create the groups from a list in a text file but I've not
    beenn able to work that out

    thanks in advance
     
    Clubsprint, Sep 17, 2008
    #1
    1. Advertisements

  2. First, remove "On Error Resume Next". There is no need for it here and it
    suppresses error messages. This makes troubleshooting nearly impossible.

    The statement you refer to assigns the value "HRStaff" to the sAMAccountName
    attribute. This attribute must have a value that is unique in the domain.
    The statement that creates the group assigns a value to the cn attribute
    (Common Name). This value must be unique in the OU.

    A likely problem is that you have run the script more than once and an error
    was raised when you attempted to create a group with duplicate cn or
    sAMAccountName. Notice that you prompt for GroupName, but then never use it.
    Perhaps you want to use (watch line wrapping):
    ==============
    Option Explicit
    Dim GroupName, objOU, objGroup

    Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
    Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000

    GroupName = InputBox("Enter the name of the Group Name you wish to create")

    Set objOU = GetObject("LDAP://ou=Workstation
    Groups,ou=Workstations,dc=int,dc=dept,dc=gov")
    Set objGroup = objOU.Create("Group", "cn=" & GroupName)

    objGroup.Put "sAMAccountName", GroupName
    objGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _
    ADS_GROUP_TYPE_SECURITY_ENABLED
    objGroup.SetInfo
    ========
    I added "Option Explicit" so you must declare all variables (in Dim
    statements). This helps troubleshooting.

    If you are creating groups from a list you can read names from a text file
    using the FileSystemObject, then in the loop where you read each line of the
    file (using the ReadLine method), create a group with each name (I would
    check and skip blank lines, which are common in text files).
     
    Richard Mueller [MVP], Sep 17, 2008
    #2
    1. Advertisements

  3. Clubsprint

    Clubsprint Guest

    Thanks Richard
    You were much help I spent half a day finding how to use the FSO and
    readline
    method you mentioned. It worked finally. Of course I could have done it
    manually
    but why use a shovel when you can have so much more fun learning how to
    drive
    a front end loader.
    I've included the script for anyone that's interested. Now to populate the
    groups. haha.
    Mark
    Aus
    -------------------start script------------------------
    Option Explicit
    Dim OrgUnit, GroupName, fso, f, objOU, objGroup
    DIM sFile, oFSO, sText, oFile
    Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
    Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    sFile = "c:\groups\groupws.txt"
    If oFSO.FileExists(sFile) Then
    Set oFile = oFSO.OpenTextFile(sFile, 1)
    Do While Not oFile.AtEndOfStream
    GroupName = oFile.ReadLine
    If Trim(GroupName) <> "" Then
    Set objOU = GetObject("LDAP://ou=Workstation
    Groups,ou=Workstations,dc=int,dc=dept,dc=gov")
    Set objGroup = objOU.Create("Group", "cn="& GroupName)
    objGroup.Put "sAMAccountName", GroupName
    objGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _
    ADS_GROUP_TYPE_SECURITY_ENABLED
    objGroup.SetInfo
    End If
    Loop
    oFile.Close
    Else
    WScript.Echo "c:\groups\groupws.txt does not exist."
    End If
    -------------------end script------------------------
     
    Clubsprint, Sep 18, 2008
    #3
  4. You got it, looks good. The only suggestion I have is bind the objOU object
    outside the Do While loop. It never changes, so there is no need to
    repeatedly bind to the same object (binding to remote objects, as in AD, is
    the slowest operation in most scripts). Minor point.
     
    Richard Mueller [MVP], Sep 18, 2008
    #4
  5. Clubsprint

    Clubsprint Guest

    Cheers, thanks gor your help.
    Mark'

     
    Clubsprint, Sep 23, 2008
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.