VBScript to create users from commanline without further input.

Posted on 2007-07-30
Last Modified: 2009-07-29
I am trying to write a script that will allow my administrative users to create a single user from the command line without being prompted for input after pressing enter.  I would like to, using switches or data entered after the name of the vbscript input the information for user creation.

For example:
createuser firstname lastname logonname

or createuser /firstname=firstname /lastname=lastname /logonname=logonname

I have the script for all of the actions (creating home directory, user account default group memberships), but I can't figure out how to get it down to a system as simple as that and I have users that want to work in this way. I can't seem to find documentation on how to do this, or how to get these inputs in as my variables.

Question by:comoore
    LVL 12

    Accepted Solution

    Taking command line parameters using VBScript uses the WScript.Arguments collection.  Each parameter passed in from the command line is added to the collection in order.  Here's some sample code on cycling through command line parameters:

    Dim firstname, lastname, logonname
    Dim i
    For i=0 To WScript.Arguments.Count
        If InStr(WScript.Arguments.Item(i), "/firstname=") > 0 Then
            firstname = Right(WScript.Arguments.Item(i), 11)
        ElseIf InStr(WScript.Arguments.Item(i), "/lastname=") > 0 Then
            lastname = Right(WScript.Arguments.Item(i), 10) > 0 Then
        ElseIf InStr(WScript.Arguments.Item(i), "/logonname=") Then
            logonname = Right(WScript.Arguments.Item(i), 11)
            WScript.Echo("Invalid Parameter: " & WScript.Arguments.Item(i))
        End If
    LVL 25

    Expert Comment

    by:Ron M
    have you tried using "dsadd"

    dsadd user /?
    Description:  Adds a user to the directory.
    Syntax:  dsadd user <UserDN> [-samid <SAMName>] [-upn <UPN>] [-fn <FirstName>]
            [-mi <Initial>] [-ln <LastName>] [-display <DisplayName>]
            [-empid <EmployeeID>] [-pwd {<Password> | *}] [-desc <Description>]
            [-memberof <Group ...>] [-office <Office>] [-tel <Phone#>]
            [-email <Email>] [-hometel <HomePhone#>] [-pager <Pager#>]
            [-mobile <CellPhone#>] [-fax <Fax#>] [-iptel <IPPhone#>]
            [-webpg <WebPage>] [-title <Title>] [-dept <Department>]
            [-company <Company>] [-mgr <Manager>] [-hmdir <HomeDir>]
            [-hmdrv <DriveLtr:>] [-profile <ProfilePath>] [-loscr <ScriptPath>]
            [-mustchpwd {yes | no}] [-canchpwd {yes | no}]
            [-reversiblepwd {yes | no}] [-pwdneverexpires {yes | no}]
            [-acctexpires <NumDays>] [-disabled {yes | no}]
            [{-s <Server> | -d <Domain>}] [-u <UserName>]
            [-p {<Password> | *}] [-q] [{-uc | -uco | -uci}]

    LVL 65

    Expert Comment


    If you have existing scripts to do parts of this, then netsmithcentral's approach is probably easier to implement.

    I'm assuming that in your "creating home directory" script, you have some code that prompts the user for the username, with something like:
    strUsername = InputBox("Enter username to create home directory for:", "Username for Home Directory")
    ' then code to create user based on strUserName

    So what you could do in place of that is:
    If WScript.Arguments.Count = 1 Then ' correct amount of parameters were passed
       strUserName = WScript.Arguments(0)
       strUsername = InputBox("Enter username to create home directory for:", "Username for Home Directory")
    End If
    ' then code to create user based on strUserName

    And then, the command to run the script would be:
    wscript.exe c:\createhomedir.vbs jsmith

    Now, you could go about trying to combine all of your scripts, or you make the above change to each one, then use a "parent" file to call all of them, such as:
    strUserName = InputBox("Enter username to create:", "New User to Create")
    strHomeShare = InputBox("Enter the path to home folder share to create the home folder on:", "Home Directory Share", "\\server\share\")
    strUserGroups = InputBox("Enter comma separated list of groups to add user to:", "User Groups")

    Set objShell = CreateObject("WScript.Shell")
    objShell.Run "Wscript C:\Scripts\CreateUser.vbs " & strUserName, 1, True
    objShell.Run "WScript C:\Scripts\CreateHomeDir.vbs " & strHomeShare & " " & strUserName, 1, True
    objShell.Run "WScript C:\Scripts\AddUserToGroups.vbs " & strUserGroups, 1, True
    Set objShell = Nothing

    MsgBox "Done"



    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    Suggested Solutions

    This is an explanation of a simple data model to help parse a JSON feed
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    745 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

    14 Experts available now in Live!

    Get 1:1 Help Now