Adding Users To Active Directory

Discussion in 'Programming General' started by rx7keg, Jan 15, 2008.

Adding Users To Active Directory
  1. Unread #1 - Jan 15, 2008 at 3:46 PM
  2. rx7keg
    Referrals:
    0

    rx7keg Guest

    Adding Users To Active Directory

    Hey guys, long time user viewer of sythe - i have a question on some for the VB Gods in here :)

    i have a Vbs script witch i have tried to add into a VB6 application
    here is a screen shot i have made of the GUI

    [​IMG]

    Code:
    Option Explicit
    
    'Input Strings
    Dim	strfirstname,strlastname,strdivision,strfunction,stremailaddress,strOffice,strHomeDirectory,strUsername,oWshShell
    
    'Objects
    Dim objRootDSE,objContainer,objUser,oFSO
    
    'Variables
    dim strSubOU,Count,varAnother
    Dim intRdm
    
    'Set the Base OU
    CONST strBaseOU = "ou=nz"
    
    'Set Constants
    CONST strHomeDrive = "H:"
    CONST strFileServer = "NZWELLMEYFP01"
    CONST varPasswordSize = "8"
    CONST strPrincipalNameSuffix = "@tcnz.co.nz"
    CONST WINDOW_STYLE = 2 'Controls how external applications that are called will be displayed
    CONST strDomainCOntroller = "nzwellmeyad01" 'Sets the domain controller to add user to (This should be the dc in the same site as the fileserver
    CONST strPassword = "passw0rd!"
    
    Set oWshShell = Wscript.CreateObject("Wscript.Shell")	
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    
    varAnother = "y"		
    
    Do While Lcase(varAnother) = "y"
    		'***********************************************
    		'*         Prompt for user information         *
    		'***********************************************
    		strfirstname = InputBox("Enter users first name")
    		strlastname = InputBox("Enter users last name")
    		strdivision = InputBox("Enter Users Division" & vbCRLF & vbCRLF & "SS/ES/CS/CF/FR/XX")
    		strfunction = InputBox("Enter Users Function eg Audit,ASG etc" & vbCRLF & vbCRLF & "Refer to AD Users and Computers Managment GUI for list)")
    		stremailaddress = Inputbox("Enter Users email address")
    		Dim VarOffice
    		VarOffice = 0
    		Do while VarOffice < 1 or VaroFFICE > 3
    			varOffice = InputBox("Enter Users Office location" & vbCRLF & vbCRLF & "1) Auckland" & vbCRLF & "2) Wellington" & vbCRLF & "3) Christchurch")
    			Select Case varOffice
    				Case 1
    					strOffice = "Auckland"
    				Case 2
    					strOffice = "Wellington"				
    				Case 3
    					strOffice = "Christchurch"
    				case Else
    					MsgBox ("Invalid Option. TryAgain")
    			End Select
    		Loop
    		
    		If strFunction = Null then
    			strSubOU = "ou=" & strDivision
    		Else
    			strSubOU = "ou=" & strFunction & ",ou=" & strDivision
    		End If
    		'***********************************************
    		'*         Connect to a container              *
    		'***********************************************
    		Set objRootDSE = GetObject("LDAP://rootDSE")
    		
    		If strSubOU = " " Then
    			Set objContainer = GetObject("LDAP://" & strDomainCOntroller & "/" & strBaseOU & ","  & objRootDSE.Get("defaultNamingContext"))
    		Else
    			Set objContainer = GetObject("LDAP://" & strDomainCOntroller & "/" & strSubOU & "," & strBaseOU & "," & objRootDSE.Get("defaultNamingContext"))
    		End If
    		
    		'***********************************************
    		'*       End connect to a container            *
    		'***********************************************
    
    		'Construct Home Directory UNC Path
    		strHomeDirectory = "\\" & strFileServer & "\" & strFirstName & "." & strLastName & "$"
    		
    		'Construct Username
    		strUsername = strFirstName & "." & strLastName
    					
    		'***********************************************
    		'*       Create User				           *
    		'***********************************************
    		
    		Set objUser = objContainer.Create("user", "cn=" & strFirstName & " " & strLastName)
    		objUser.Put "sAMAccountName", strUsername
    		objuser.Put "Displayname", strFirstName & " " & strLastName
    		objuser.Put "givenname", strFirstName
    		objuser.Put "sn", strLastName
    		objuser.Put "userprincipalname", strUsername & strPrincipalNameSuffix
    		objuser.Put "Homedirectory",strHomeDirectory
    		objuser.Put "Homedrive",strHomeDrive
    		objuser.Put "pwdLastSet", 0
    		objUser.Put "physicalDeliveryOfficeName", strOffice
    		objUser.Put "Mail", stremailaddress
    		objUser.SetInfo
    		objUser.SetPassword strPassword
    		objuser.SetInfo
    	
    			
    		'Create share
    		oFSO.CreateFolder("\\" & strFileServer & "\d$\tcHome\" & strFirstName & "." & strLastName)
    		owshShell.Run "c:\windows\rmtshare.exe " & strHomeDirectory & "=d:\eyhome\" & strFirstName & "." & strLastName & " /GRANT ANZ\" & strFirstName & "." & strLastName & ":C",WINDOW_STYLE,True
    		
    	
    		MsgBox ("User (" & strFirstName & "." & strLastName & ") created. Password is: " & strPassword)
    		varAnother = InputBox ("Add another user? Y/N")
    Loop
    Now im wondering if some one could create and application with the script i have posted - as i have tried but i dont know were i am going wrong - this script works well useing adduser.vbs but as soon as i add it into my application interface it fails

    if some one could help that would be grate

    Thanks

    RX7
     
  3. Unread #2 - Jan 15, 2008 at 11:01 PM
  4. Covey
    Joined:
    Sep 9, 2005
    Posts:
    4,510
    Referrals:
    9
    Sythe Gold:
    9
    Discord Unique ID:
    807246764155338833
    Discord Username:
    Covey#1816

    Covey Creator of EliteSwitch
    Retired Sectional Moderator Visual Basic Programmers

    Adding Users To Active Directory

    That is Visual Basic 2005 Code not Visual Basic 6.
    That could be your problem
     
< Mouse mOvement Help | Short cut to stop autoclicker when its clickin in rs? >

Users viewing this thread
1 guest


 
 
Adblock breaks this site