Solved

Creating Domain user accounts script

Posted on 2004-08-11
3
1,518 Views
Last Modified: 2008-09-18
Hi Experts,

I am looking for a VBScript example of how to create a Windows Domain user account from a computer within the domain.

Currently I have a script that was creating a user account in a specifed OU and group but the server (Win2k3) that was running the script was the Domain Controller. With our new network design the domain controller is running on a separate server and the computer running the script is simply a member of the domain. The domain controller is using Active Directory and the server that will be running the script does not. The server executing the script is running an application that requires to create new user accounts within the domain. The scripting server is and scripts will be executed by the Domain Controller Administrator account as that is login i use.

I am not a skilled VB programmer but can work with a script to include command line supplied arguments to replace values for DOMAIN, OU, GROUP, PASSWORD. The user account password should never expire.

Many thanks,

Greg
0
Comment
Question by:greghess
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 11780074
Hi, Greg.

I'm a little confused.  If you already have a script that creates users, then it should work regardless of where it's run from.  The only requirement I know of is that the script be run under an account that has sufficient permissions to create accounts in the domain.  Are you saying that the script you currently have does not work when run from a computer other than the domain controller?  If so, can you post the script?  It might be something that could be fixed rather than coming up with a new script.  Or is it that your current script used fixed parameters and you'd like one with flexible parameters?

0
 

Author Comment

by:greghess
ID: 11784085
Hi thanks for your reply,

Sorry for my confusing post.

Yes my script that I was using befor does not work on any other computer other than the Domain Controller. Below you will find my current impl.

'Declare all variables
option explicit

'Dont fail on error
on error resume next

' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

dim args
dim bExists
dim sDomain, sOU, sGroup, sUserName, sPassword
dim sDNSDomain, sNetBIOSDomain, sGroupPath
dim objRoot, objDomain, objOU, objContainer, objGroup, objLeaf, objTrans

' Get the arguments
set args = wscript.arguments
call processCommandLine(args, sDomain, sOU, sGroup, sUserName, sPassword)

      
set objRoot = GetObject("LDAP://rootDSE")
set objDomain = GetObject("LDAP://" & objRoot.Get("defaultNamingContext"))      

'Create the user      
set objContainer = GetObject("LDAP://OU=" & sOU &"," & _
objRoot.get("defaultNamingContext"))
call displayErrorAndBail(err, TRUE, "Unable to get OU")
set objLeaf = objContainer.create("User", "cn=" & sUserName)
call displayErrorAndBail(err, TRUE, "Unable to create user")
objLeaf.put "samAccountName", sUserName
objLeaf.setInfo
call objLeaf.setPassword(sPassword)
objLeaf.AccountDisabled = FALSE
objLeaf.AllowLogon = 1
objLeaf.SetInfo
      
'Add user to specified group
set objTrans = CreateObject("NameTranslate")
sDNSDomain = objRoot.Get("DefaultNamingContext")
objTrans.Init ADS_NAME_INITTYPE_GC, ""
objTrans.Set ADS_NAME_TYPE_1779, sDNSDomain
sNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)
' Remove trailing backslash.
sNetBIOSDomain = Left(sNetBIOSDomain, Len(sNetBIOSDomain) - 1)
objTrans.Set ADS_NAME_TYPE_NT4, sNetBIOSDomain & "\" & sGroup
      
sGroupPath = objTrans.Get(ADS_NAME_TYPE_1779)
call displayErrorAndBail(err, TRUE, "Unable to find group")
set objGroup = GetObject("LDAP://" & sGroupPath)
objGroup.Add(objLeaf.AdsPath)
objGroup.SetInfo

'All Done            
wscript.echo "Added Windows User:" & sUserName & " to OU:" & sOU & " group:" & sGroup

'Clean up
set objRoot = Nothing
set objDomain = Nothing
set objOU = Nothing
set objContainer = Nothing
set objGroup = Nothing
set objLeaf = Nothing
set objTrans = Nothing

'******************************************************************
' Sub to display the usage for the script
'******************************************************************
sub displayUsage
  WScript.Echo "USAGE: cscipt.exe createUserAccount.vbs DOMAIN_NAME OU GROUP USER_NAME PASSWORD"
  WScript.Echo VbCrLf & "Where: DOMAIN_NAME is the computer domain name"
  WScript.Echo "GROUP is the user group th add this user account to"
  WScript.Echo "OU is the users organizational unit"
  WScript.Echo "USER_NAME is the userId"
  WScript.Echo "PASSWORD valid user password"
end Sub

'****************************************************************
' Sub to process the command line
'****************************************************************
sub processCommandLine(args, sDomain, sOU, sGroup, sUserName, sPassword)
   if( args.Count < 4 ) then
      WScript.Echo "ERROR: Wrong number of arguments."
      Call displayUsage ( )
      WScript.Quit 1
   else
      sDomain = args(0)
      sOU = args(1)
      sGroup = args(2)
      sUserName = args(3)
      sPassword = args(4)
   end if
end sub

'*****************************************************************
' Error processing sub.  Takes 3 args,
' 1. is the error object
' 2. is a flag, TRUE means terminate if an error was found,
'    FALSE- Display an error, clear the error object, continue
' 3. A user defined error text to display with the error information
'
sub displayErrorAndBail( oErr, bFlag, sMsg )
   if( oErr.Number <> 0 ) then
      '
      ' We have an error, display the error text and the error number
      ' along with the error description
      '
      WScript.Echo sMsg
      WScript.Echo "ERROR Number: " & hex( oErr.Number ) & " has occurred. "
      WScript.Echo oErr.Description
      oErr.Clear
      if( bFlag ) then
        WScript.Echo "Terminating script "
        WScript.Quit 1
      end if
    end if
end sub

When running the script as follows I get this error:

cscript.exe createUserAccount.vbs WIN2K goglobal clients jdoe test123test***!

Error MSG:
Unable to get OU
Error 1A8 has occurred
Object required
Terminating script

Thanks for your help,

Greg
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

730 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