visual basic - command order sequence

Posted on 2013-01-31
Last Modified: 2013-03-15
hi ive successfully created for the 1st time the following on windows 2003:

- a 'ou, group & user account' - via visual basic

ive now added some other information to go in a 'domain user account' but no matter where i put it, it appears not to work & im aware there is obviously some order to it but have not got a clue!!

in order to get the extras ive added, i accessed 'adsiedit.msc' & ticked a box in order to see what they were on another user account that i created manually.

qns1.  can anyone help me to put in order & some advice maybe ?

note: ive been doing some reading & whatching videos to gain some basic understanding as always wondered about these so-called 'scripts' & begginning to understand why some can be useful somewhat!!
Question by:mikey250
  • 4
  • 3
  • 2
  • +1

Expert Comment

ID: 38839696
Try this:

Set objOU = GetObject("LDAP://OU=Coding Samples,dc=itservices,")
Set objUser = objOU.Create("User", "cn=Test User7")
objUser.Put "sAMAccountName", "Test User7"

objUser.SetPassword "adminpw!1"

objUser.AccountDisabled = FALSE
objUser.Department = "Engineering Department"
objUser.Description = "User Account"
objUser.FaxNumber = "01902356756"
objUser.FullName = "James C. Foster"
objUser.TelephoneHome = "01902356756"
objUser.TelephoneMobile = "07814789674"
objUser.TelephoneNumber = "01902 356756"
objUser.EmailAddress = ""
objUser.Manager = "CN=james c.foster,OU=ACCTS,DC=itservices,"

Open in new window

When using the objUser, you can only set IADsUser interface properties..

See this link for a list.

After the user is created, you could then modify other AD attributes.  See link below

Author Comment

ID: 38840007
hi shorvath, (i will have a look through the url's so thanks)!!!!

i assumed it would create inside my 'coding samples' i have created already for the others i did, individually but i wanted to created all at once the: ou, group & user, but got the following message:


line 4
char 1
error - the object already exists
code - 80071392
source - (null)

Expert Comment

ID: 38840706
well , if the user already exists, it's going to throw and error.

there are lots of ways to test if a user exists, and then add if he/she does not...

Author Comment

ID: 38851575
hi shorvath, apologies for taking time to respond. you will have to forgive my slowness as all i see is code that dont make no sense to me yet!!!!!!!!!!

step 1

i deleted user from: coding samples but still states:

line 19
char 1
error - there is no such object on the server

but user account: test user8 - does appear with red (x)

step 2

deleted 'coding samples'
created manually 'coding samples'

step 3
ran script - but still below shows:

line 19
char 1
error - there is no such object on the server

but user account: test user8 - does appear with red (x)

step 4

added 'coding samples3' in vbs script but same issue:

line 19
char 1
error - there is no such object on the server

but user account: test user8 - does appear with red (x)
LVL 65

Accepted Solution

RobSampson earned 500 total points
ID: 38886979
Hi, here is a VBScript I have used to create a user account, which also checks whether the accounts exists or not.

Change the top five lines to suit, and it should work.



strFirstName = "TestFirstName"
strLastName = "TestSurname"
strUserName = "TUser"
strPassword = "pword"
strOU = "OU=Coding Samples,"

strFullName = strFirstName & " " & strLastName

' Bind to Active Directory.
Set objRootLDAP = GetObject("LDAP://rootDSE")

If Trim(strOU) = "" Then
	strOU = ","
	If Left(strOU, 1) = "," Then strOU = Mid(strOU, 2)
	If Right(strOU, 1) <> "," Then strOU = strOU & ","
End If

WScript.Echo "About to create:" & VbCrLf &_
	strFullName & vbCrLf &_
	strUserName & VbCrLf &_
	strPassword & VbCrLf &_
	"LDAP://" & strOU & objRootLDAP.Get("defaultNamingContext")
' This will add the user to eg. dommain.local\1st grade\Users
Set objContainer = GetObject("LDAP://" & strOU & objRootLDAP.Get("defaultNamingContext")) 

' Check if the user already exists
On Error Resume Next
Set objNewUser = GetObject("LDAP://cn=" & strFullName & "," & strOU & objRootLDAP.Get("defaultNamingContext")) 
If Err.Number = 0 Then
	MsgBox "User " & strFullName & " already exists."
	On Error GoTo 0
	On Error GoTo 0

	' Build the actual User.
	' Attributes listed here:
	Set objNewUser = objContainer.Create("User", "cn= " & strFullName)
	objNewUser.Put "userPrincipalName", strUserName & "@" & Replace(Replace(objRootLDAP.Get("defaultNamingContext"), ",", "."), "DC=", "")
	objNewUser.Put "sAMAccountName", strUserName
	objNewUser.Put "givenName", strFirstName
	objNewUser.Put "sn", strLastName
	objNewUser.Put "displayName", strFullName
	objNewUser.SetPassword strPassword
	objNewUser.AccountDisabled = False
End If

Open in new window

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud


Author Comment

ID: 38908889
hi robsampson, i have not forgotten this and i will come back to this question, just finishing something else off!!  appreciated.
LVL 65

Expert Comment

ID: 38911902
That's OK.  I'll be around ;-)
LVL 46

Expert Comment

by:Martin Liss
ID: 38987109
I've requested that this question be deleted for the following reason:

The question has either no comments or not enough useful information to be called an "answer".
LVL 65

Expert Comment

ID: 38987110
I believe my comment ID 38886979 provides a decent answer.

Author Comment

ID: 38989839
hi robsampson, yes in the end i got it to work and updated my server at the same time with some unknown missing updates.

although that was not my question really it was after creating an 'ou & a user account' i wanted to add all other company details in the user account such as mobile phone, email address, name of company.

because of this i used the 'adsiedit.msc' to know what specific word code needs to be used, but when i added it, it would not work!!!

apologies for taking sometime to come back!!!!

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Red error squiggly on 7 27
Help me. 3 46
Duplicating a tab page content to another in TabControl control 11 41
Run a batch file when a Crystal Report is opened 5 15
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now