Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding users & computers to Active Directory

Status
Not open for further replies.

acl03

MIS
Joined
Jun 13, 2005
Messages
1,077
Location
US
This script will create users or computers in active directory.

Don't forget that you need to set 2 variables at the top of the script:

FQDomainName
defaultPassword


To exit this script at any time, just leave an input box blank, or press cancel. There are three options in this script.

1 - add users - this will cycle through the userid, firstname, lastname, and description fields over and over.

2 - add computers - this cycles through the computer name input box over and over

3 - skip these menus - this will jump to the bottom of the script, where you can pre-code all the computers and users you want to add following the two examples i gave. (In the massUpdate sub at the end)

This script creates users and/or computers in the deafult users/computers container:

Code:
''''''''''''''''''''''''''''''''''''''
'Written by Andrew Levine - 2/2005
''''''''''''''''''''''''''''''''''''''

'This script will add users or computers to active directory.
'Program will repeat until a field is left blank or cancel is pressed

'set Fully Qualified Domain Name
FQDomainName="INSERT FQ DOMAIN NAME"  'this will be after the @ in the user principal name
defaultPassword = "INSERT PASSWORD"



Set objRootDSE = GetObject("LDAP://rootDSE")

'Set OU in which to add Computers (from the bottom up)
Set computerOU = GetObject("LDAP://cn=Computers," & objRootDSE.Get("defaultNamingContext"))

'Set OU in which to add Users
Set userOU = GetObject("LDAP://cn=Users," & objRootDSE.Get("defaultNamingContext"))

sub addComputer (name)
    strComputer = name
    Const ADS_UF_PASSWD_NOTREQD = &h0020
    Const ADS_UF_WORKSTATION_TRUST_ACCOUNT = &h1000
    Set objComputer = computerOU.Create("Computer", "cn=" & strComputer)
    objComputer.Put "sAMAccountName", strComputer & "$"
    objComputer.Put "userAccountControl", _
    ADS_UF_PASSWD_NOTREQD Or ADS_UF_WORKSTATION_TRUST_ACCOUNT
    objComputer.SetInfo
end sub


sub addUser(shortName, firstname, lastname, description)
    Set usr = userOU.Create("user", "CN=" & firstname & " " & lastname)
    usr.Put "sAMAccountName", ShortName
    usr.Put "userPrincipalName", shortName & "@" & FQDomainName
    usr.Put "GivenName", firstname
    usr.Put "Sn", lastname
    usr.Put "description", description
    usr.Put "displayName", firstname & " "  & lastname
    usr.SetInfo
    usr.SetPassword defaultPassword
    usr.AccountDisabled = False
    usr.SetInfo
end sub

done=0

Sub checkEmpty (userInput)
    If userInput = "" Then
        Wscript.quit
    End if
End sub


Sub addComputers
    While done=0
        cn = InputBox ("Enter computer to add. Leave blank to exit.", "Add Computers")
        checkEmpty cn
        addComputer cn
    Wend
End Sub

Sub addUsers
    While done=0
        sn = InputBox ("Enter User ID:", "New User - Leave blank to exit.")
        checkEmpty sn
        fn = InputBox ("Enter First Name:", "New User - Leave blank to exit.")
        checkEmpty fn
        ln = InputBox ("Enter Last Name:", "New User - Leave blank to exit.")
        checkEmpty ln
        de = InputBox ("Enter Description:", "New User - Leave blank to exit.")
        checkEmpty de
        addUser sn, fn, ln, de
    Wend
End Sub

message = "Please pick an option:"     & vbCrLf & _
                "(Press cancel or leave blank to quit.)"     & vbCrLf & vbCrLf & _
                "1 - Add Users"                 & vbCrLf &  _
                "2 - Add Computers"                 & vbCrLf & _
                "3 - Skip These Menus"                 & vbCrLf & _
                vbCrLf & vbCrLf & "Please enter a number:"

Do
    userChoice = InputBox(message, "Add Users & Computers")
    select case userChoice
        case "1"
            addUsers
        case "2"
            addComputers
        case "3"
            massUpdate
        case ""
            WScript.Quit
        case else
            msgbox "Invalid entry. Please try again."
        end select
loop while userChoice <> "" and userChoice <> "0"


Sub massUpdate

    'addUser "userID", "fName", "lName", "description"
    'addComputer "Computer1"
    
    
    Wscript.quit
End Sub



Thanks,
Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top