Learn how to a build a cloud-first strategyRegister Now


VBScript to create users from commanline without further input.

Posted on 2007-07-30
Medium Priority
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

netsmithcentral earned 1500 total points
ID: 19593656
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 Malmstead
ID: 19594955
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

ID: 19597174

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes

810 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