How can i make VBScript work in ??

Posted on 2006-03-26
Last Modified: 2013-11-25
Im new to VBscript and trying to create a User & email in my AD through a VBscript using In addition, I want to make the username and names Dynamic, meaning that the user will be able to type out the name he wants. Moreover, im trying to trigger the Vbscript when the user Clicks a Submit Button.

Note: Im using Visual Studio 2005.

this is the VBscript Code:

Const UF_SCRIPT = &H1
Const UF_LOCKOUT = &H10

Dim objDomain, objUser, objGroup, intUserFlags, intNewUserFlags

Dim username, firstname, lastnames, Defaultcn, OUcontainer, OUType, group


firstname="Amer"           <==== all those parameters must be entered by the User from the Form and then passed to the VBscript.
lastnames="Bin Test"
group = "grouptest"
fullname= firstname & " " & lastnames

Defaultcn    = "Users"
OUcontainer  = "UsersDirectory"
OUType = "StandardUsers"

Set objDomain = GetObject("LDAP://cn=" & Defaultcn & ",dc=MyDomain,dc=com")
Set objUser = objDomain.Create("user","cn=" & fullname)
objUser.Put "sAMAccountName", username
objUser.Put "userPrincipalName", username & ""

Thanks, I really appreciate your help.
Question by:amersharaf
    LVL 15

    Expert Comment

    You would want to put all of this in your codebehind. works very differently from old asp so I would not even try converting your old code. Rather rewrite it in the new way.

    Take a look at these articles on how to admin AD from


    Author Comment

    Im not sure if i explained the situation clearly.

    What im trying to do here is:

    1- I have a working VBscript File.
    1- I want to Paste the VBscript in just execute the file from and create a Form.
    2- Make the User enter some Values.
    3- Toss the values back to the VBscript File.
    4- when the User Clicks Submit, the VBscript file is executed.

    I forgot to mention that the VBscript File works perfectly when executed outside of

    I hope this clears any confusions.
    LVL 15

    Accepted Solution

    I would advise against it unless someone else knows how to protect that file. Using ASP.NET you have a lot more security on your application.

    If you just pass the variables to a vbscript file to execute then someone can quite easily just call the vbscript file na dpass any variables they want creating as many users as they like without you being able to stop them.

    What I would do is create a ASP.Net web form with all the textboxes you require and a button.

    On button click rewrite the vbscript code into the codebehind and execute it there.

    THis way you have complete control over who and when that code gets executed.

    The links i posted explain how you create the AD interface and use it from there.
    If someone else has some more experience in doing this please feel free to jump in. My knowledge on this is only theoretical.
    LVL 1

    Expert Comment

    I do Agree with GavinMannion, and in addition to what he said, you should consider that the VBScript is client side code which means it will be executed on the users machine - which may be not connected to your domain directly - so you should run your code through server-side code,other possible solution is to make your vbscript a COM component (with some minor efforts) and call this component from your ASP.NET code and pass to it the parameters you need.

    Author Comment

    GavinMannion, how can i do as what you suggested ? I tried rewriting the code, but it seems that the VBscript code doesnt get executed. and i dont know how to pass parameters to the vbscript from the code-behind.

    Bedies, I forgot to mention that I'll be using the Form and VBscript on my Intranet, so security is not a big concern to me.
    LVL 15

    Expert Comment

    If you have rewritten it into the codebehind then to pass the variables you would do something along the lines of

    string username= txtUserName.Text
    string firstname= txtFirstName.Text           <==== all those parameters must be entered by the User from the Form and then

    Even though the form is on the intranet only it doesn't mean someone in the company won't abuse it. Most hacking attacks and destructive actions come from inside your own company.

    If you want you can paste your rewritten code into this thread and someone can point out where you are going wrong.
    LVL 1

    Assisted Solution

    if you will rewrite the code, then check this code snippet, it's a sample to how to create a user on the domain in the .NET using DirectoryServices, wish it helps

    P.S. the code is from this article ( )

    namespace ADUserManager

        class Program


            static void Main(string[] args)


                string userName="John Smith", sAMAccountName="JSmith", passWord="SomeThingCkrit";

                // Step #1

                // Instantiate a Directory Entry Object to represent the User folder

                DirectoryEntry adUserFolder = new DirectoryEntry("LDAP://CN=Users,DC=RSLAB,DC=Local");


                // Step #2

                // Check to make sure the folder is a container object

                if (adUserFolder.SchemaEntry.Name == "container")


                    // Step #3

                    // Create a Directory Entry Object to represent the user

                    DirectoryEntry newADUser = adUserFolder.Children.Add("CN=John Smith", "User");


                    // Step #4

                    // Check to see if there is already someone in the folder with that name (CN)

                    // If so, delete that account first



                        Console.WriteLine("The user: " + userName + " already exists.  The Account will be Overwritten!");


                        // Step 4a: Remove the user object first

                        adUserFolder.Children.Remove(new DirectoryEntry(newADUser.Path));



                    //  Step 5: Add the user optional and required properties (sAMAccountName is ALWAYS REQUIRED!!)



                    //  Step 6: Commit the changes to the AD data store



                    //  Step 7: Set the password for the new account, which can only be done AFTER the account exists!

                    //          We are using the "Invoke" method on the newADUser object, which uses Native AD Object under the hood to set

                    //          the password.  

                    newADUser.Invoke("setPassword", passWord);


                    //  Step 8: Enable the user, if the user wants to, by setting the userAccountControl property

                    //          to the magical value of 0x0200.  The disable-user value is 0x0002

                    newADUser.Properties["userAccountControl"].Value = 0x0200;



                    Console.WriteLine("User: " + userName + " successfully created!");

                    Console.WriteLine("Hit any key to end...");








    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    This video discusses moving either the default database or any database to a new volume.

    759 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

    11 Experts available now in Live!

    Get 1:1 Help Now