• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

More Scripting Help - Creating AD Users with Email

Hey,
I am a bit lost here.  I am writing a script to add a user to AD.  Here is what I have.
________________________________________________
dim strDomainName, InputOU, InputUN, InputSN, InputDN, InputDsc, objOu, objUser, InputGrp, objGroup, InputGrpOu

InputOU = InputBox("Enter OU")
InputUN = InputBox("Enter User Name")
InputGiven = InputBox("Enter First Name")
InputSN = Inputbox("Enter Last Name")
InputDsc = InputBox("Enter Description")
InputGrp = InputBox("Enter Group")
InputGrpOU = "CN=Users"            'InputBox ("Enter OU of Group")
Set objOU = GetObject("LDAP://ou=" & InputOU & ",dc=JSC,dc=com")

Set objUser = objOU.Create("User", "cn=" & InputGiven & " " & InputSN)
objUser.Put "userPrincipalName", InputUN
objUser.Put "sAMAccountName", InputUN
objUser.Put "givenName", InputGiven
objUser.Put "sn", InputSN
objUser.Put "displayName", InputGiven & " " & InputSN
objUser.Put "Description", InputDsc
objUser.SetInfo
objUser.SetPassword "TESTPW"
objUser.AccountDisabled = False

'Add to group
Set objGroup = GetObject("LDAP://CN=" & InputGrp & "," & InputGrpOU & ",dc=JSC,dc=com")
objGroup.add(objUser.ADsPath)
____________________________________________________________

My user is being created in the correct place.  But shows up disabled.  I also do not know how to add an email address for him in my domain.
0
jsctechy
Asked:
jsctechy
  • 15
  • 12
1 Solution
 
sirbountyCommented:
Your .setinfo needs to be the 'last' item when updating the properties - else those are missed...

You have:
objUser.SetInfo
objUser.SetPassword "TESTPW"
objUser.AccountDisabled = False

it should be:
objUser.SetPassword "TESTPW"
objUser.AccountDisabled = False
objUser.SetInfo

You can add the mail info using:
objUser.mail=objUser.SamAccountName & "@domain.com" 'remember to use SetInfo
0
 
jsctechyAuthor Commented:
I get an error that way.

Line 19 Chr 1
objUser.SetPassword "TESTPW" 'meets pw req
0
 
sirbountyCommented:
What's the error?
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
jsctechyAuthor Commented:
Line: 19
Chr:  1
Error:  There is no such object on the server.
Code: 80072030
Source: (null)

Seems as when trying to set the PW, the object doesn't exist yet.
0
 
sirbountyCommented:
Ah - you can use a couple of SetInfo's for that...


Set objUser = objOU.Create("User", "cn=" & InputGiven & " " & InputSN)
objUser.SetInfo
objUser.Put "userPrincipalName", InputUN
objUser.Put "sAMAccountName", InputUN
objUser.Put "givenName", InputGiven
objUser.Put "sn", InputSN
objUser.Put "displayName", InputGiven & " " & InputSN
objUser.Put "Description", InputDsc
objUser.SetPassword "TESTPW"
objUser.AccountDisabled = False
objUser.SetInfo
0
 
jsctechyAuthor Commented:
hey- I did it the original way I posted, but at the end of the setpassword and accountdisabled, I setinfo again
0
 
jsctechyAuthor Commented:
Cool- Thanks.
So for email, I can just do this
objUser.mail=objUser.SamAccountName & "@JSC.com"
0
 
sirbountyCommented:
Yep yep...
Cool. :^)
0
 
jsctechyAuthor Commented:
Thanks.  I will try that.
0
 
sirbountyCommented:
Good luck! :^)
0
 
jsctechyAuthor Commented:
Hey,
1 Q-
If I use  -    objUser.mail=objUser.SamAccountName & "@JSC.com"
Does that create the mailbox on the exchange server, or just add an email in the email address field?
I want it to create the mailbox, same as when I manually create a user.  After specifying a password, the next screen will be to create an email box (checkbox), with also Server and Mailbox Store options.
0
 
sirbountyCommented:
That, I've never done (the above only adds the mail property)...however according to this site, it seems to be fairly simple:

http://techtasks.com/code/viewbookcode/1237

Try adding:

objUser.CreateMailbox objUser.adsPath

to the end of the script.

http://searchvb.techtarget.com/tip/0,289483,sid8_gci1086268,00.html gives another good example.
0
 
jsctechyAuthor Commented:
I am confused....
Should I do something like this?

Dim oIADSUser
Dim MStore
strDefaultNC = "DC=EnterYourDomainName,DC=com"
Set oIADSUser = GetObject("LDAP://CN=" & InputUN & "," & "OU=" & InputOU & "," & "DC=JSC,DC=com")

oIADSUser.CreateMailbox "LDAP://CN=First Storage Group,
CN=InformationStore,
CN=JSCEMAIL,
CN=Servers,
CN=First Administrative Group,
CN=Administrative Groups,
CN=EnterYourSMTPOrganisationName, -????????
CN=Microsoft Exchange,
CN=Services,
CN=Configuration,
DC=JSC,DC=com"
oIADSUser.SetInfo

0
 
jsctechyAuthor Commented:
unsure how to get the full DN for the exchange server info.
Right now I am trying from command prompt to get the info-
CMD>ldifde -d cn=existinguser,ou=someou,dc=domain,dc=root -f con
0
 
sirbountyCommented:
Again, Im not really familiar with the Exchange piece of this, but I was thinking it would be:

Set objUser = objOU.Create("User", "cn=" & InputGiven & " " & InputSN)
objUser.SetInfo
objUser.Put "userPrincipalName", InputUN
objUser.Put "sAMAccountName", InputUN
objUser.Put "givenName", InputGiven
objUser.Put "sn", InputSN
objUser.Put "displayName", InputGiven & " " & InputSN
objUser.Put "Description", InputDsc
objUser.SetPassword "TESTPW"
objUser.AccountDisabled = False
objUser.CreateMailbox objUser.adsPath 'does this work?
objUser.SetInfo
0
 
jsctechyAuthor Commented:
I think I got it.....

homeMDB:
 CN=Mailbox Store (JSEMAIL),CN=First Storage Group,CN=InformationStore,CN=JSEMAIL,CN=Servers,CN=First Administrative Group,CN=Administrative Groups,CN=JSC,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=JSC,DC=com
0
 
sirbountyCommented:
Or what about:

Set objUser = objOU.Create("User", "cn=" & InputGiven & " " & InputSN)
objUser.SetInfo
objUser.Put "userPrincipalName", InputUN
objUser.Put "sAMAccountName", InputUN
objUser.Put "givenName", InputGiven
objUser.Put "sn", InputSN
objUser.Put "displayName", InputGiven & " " & InputSN
objUser.Put "Description", InputDsc
objUser.SetPassword "TESTPW"
objUser.AccountDisabled = False
objUser.CreateMailbox objUser.DN 'does this work?
objUser.SetInfo
0
 
jsctechyAuthor Commented:
Hi,
No-
objUser.CreateMailbox is not supported...?  This is for Exchange 2003 on a 2000 Domain
0
 
sirbountyCommented:
According to this it is - but perhaps the reference was setup incorrectly..
http://www.msexchange.org/articles/Scripting-Exchange-VBScript-ADSI-Part2.html

Set objUser = objOU.Create("User", "cn=" & InputGiven & " " & InputSN)
objUser.SetInfo
'[...]
objUser.AccountDisabled = False
objUser.CreateMailbox objUser.DN 'does this work?
objUser.SetInfo
'adding this...of course, I don't know what these settings are though...
'  would've though simply using the DN to the newly created user would do it?
Set objMailbox = objUser
  objMailbox.CreateMailbox "LDAP://CN=" & MDBName & _
                               ",CN=" & StorageGroup & _
                               ",CN=InformationStore" & _
                               ",CN=" & Server & _
                               ",CN=Servers" & _
                               ",CN=" & AdminGroup & _
                               ",CN=Administrative Groups" & _
                               ",CN=" & Organization & _
                               ",CN=Microsoft Exchange,CN=Services" & _
                               ",CN=Configuration," & DomainDN
objUser.SetInfo
0
 
jsctechyAuthor Commented:
getting an error on this:

Set objMailbox = objUser
0
 
sirbountyCommented:
Unfortunately, I don't have a test domain to try this out on...
I'm just not sure what to do from here as I've not worked with creating mailboxes before...sorry.
0
 
jsctechyAuthor Commented:
Here is what I have right now
____________________________

dim strDomainName, InputOU, InputUN, InputSN, InputDN, InputDsc, objOu, objUser, InputGrp, objGroup, InputGrpOu, strHomeMDB, objMailbox

InputOU = InputBox("Enter OU")
InputUN = InputBox("Enter User Name")
InputGiven = InputBox("Enter First Name")
InputSN = Inputbox("Enter Last Name")
InputDsc = InputBox("Enter Description")
InputGrp = InputBox("Enter Group")
InputGrpOU = "CN=Users"            'InputBox ("Enter OU of Group")
Set objOU = GetObject("LDAP://ou=" & InputOU & ",dc=JSC,dc=com")
'Set objMailbox = objUser
strHomeMDB = "CN=Mailbox Store (JSEMAIL),CN=First Storage Group,CN=InformationStore,CN=JSEMAIL,CN=Servers,CN=First Administrative Group,CN=Administrative Groups,CN=JSINC,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=JSC,DC=com"
Set objUser = objOU.Create("User", "cn=" & InputGiven & " " & InputSN)
objUser.Put "userPrincipalName", InputUN
objUser.Put "sAMAccountName", InputUN
objUser.Put "givenName", InputGiven
objUser.Put "sn", InputSN
objUser.Put "displayName", InputGiven & " " & InputSN
objUser.Put "Description", InputDsc
objUser.SetInfo
objUser.SetPassword "arkansas"
objUser.AccountDisabled = False
objUser.Put "physicalDeliveryOfficeName", InputOU
objUser.Put "homeDirectory", "\\Jsfile\%username%"
objUser.Put "homedrive", "H:"
objUser.Put "ScriptPath", "logon.cmd"
objUser.SetInfo
'create mailbox
objUser.CreateMailBox "LDAP://" & strHomeMDB
objUser.SetInfo

'Add to group
Set objGroup = GetObject("LDAP://CN=" & InputGrp & "," & InputGrpOU & ",dc=JSC,dc=com")
objGroup.add(objUser.ADsPath)
____________________________
0
 
jsctechyAuthor Commented:
Oh- maybe someone else will answer.  I am searching online and find stuff that doesn't seem to work w/ my Exchange/AD versions.
0
 
jsctechyAuthor Commented:
Figured it out.....!  Finally
_____________________________
strHomeMDB = "CN=Mailbox Store (JSEMAIL),CN=First Storage Group,CN=InformationStore,CN=JSEMAIL,CN=Servers,CN=First Administrative Group,CN=Administrative Groups,CN=JSCINC,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=JSC,DC=com"
'create mailbox
objUser.Put "msExchHomeServerName", "/o=JSINC/ou=First Administrative Group/cn=Configuration/cn=Servers/cn=JSEMAIL"
objUser.Put "mailNickname", InputUN
objUser.Put "mail", InputUN & "@JSC.com"
objUser.Put "homeMDB", strHomeMDB
objUser.Put "mDBUseDefaults", True
objUser.SetInfo
________________________________________
0
 
sirbountyCommented:
Awesome!
Glad you got it sorted. :^)
0
 
jsctechyAuthor Commented:
Thanks- points go to you (obviously) for the reason I started to post.

Thanks again- will be posting more shortly :)
0
 
sirbountyCommented:
Thanx! :
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

  • 15
  • 12
Tackle projects and never again get stuck behind a technical roadblock.
Join Now