'*************************************************************
'		Script Witten by Larry Heintz       	    
'		January 2006 www.windowsadminscripts.com
' This is an updated version of the script. Instead of passing
' the creation information, you are prompted to type the info
' via the command line. If you do not enter a servername there
' is a function that will grab the local computer and use that
' for the servername. If you do not enter a username or password
' the script dies. If you do not enter a group it will default
' to the Users group. You must be logged on as Admin to add a
' user to the local host or you will need to be a Admin
' of the remote computer you are trying to add the user to.
'*************************************************************

'Dim Stuff
Dim objStdOut,objStdIn,ServerName,UserName,Password,GroupName
Dim container,user,Flags,Group
'Set Stuff
Set objStdOut = Wscript.stdOut
Set objStdIn = Wscript.stdIn

'Asks for ServerName
objStdOut.write "Server to add user to:"
ServerName = lcase(objStdIn.readline)
If ServerName = "" then
	ServerName = getComputer()
Elseif ServerName = "." then
	ServerName = getComputer()
Elseif ServerName = "local" then
	ServerName = getComputer()
Else
	ServerName = ServerName
End If

'Asks for UserName
objStdOut.write "Username to add:"
UserName = objStdIn.readline
If Username = "" then
	wscript.echo "*** The script has ended due to no username being added. ***"
	wscript.quit
Else
	UserName = UserName
End if

'Asks for Password
objStdOut.write "Username's Password:"
Password = objStdIn.readline
If Password = "" then
	wscript.echo "*** The script has ended due to no password being added. ***"
	wscript.quit
Else
	Password = Password
End if

'Asks for Group
objStdOut.write "Username added to what group:"
GroupName = objStdIn.readline
If GroupName = "" then
	GroupName = "Users"
Else
	GroupName = GroupName
End If

'Connect to server and create user
Set container = GetObject("WinNT://" & ServerName)
Set user = Container.Create("User",UserName)
	User.SetInfo
set container = nothing
set user = nothing
'Sets Pass, Change Pass at Next Login and Password Never Expires
Set User = GetObject("WinNT://" & ServerName & "/" & UserName & ",User")
	Call User.SetPassword(Password)
	User.Put "PasswordExpired", 0	'0 = NOT SET User must change pass at next login 1 = SET User must change pass at next login
	Flags = User.Get("UserFlags")
	User.Put "UserFlags", Flags XOR &H10000
	User.SetInfo
set User = nothing
'Puts User in Group
Set User = GetObject("WinNT://" & ServerName & "/" & UserName & ",User")
Set Group = GetObject("WinNT://" & ServerName & "/" & GroupName & ",Group")
	Group.Add(User.ADsPath)
	Group.SetInfo
set User = nothing
set Group = nothing

wscript.echo "The User:" & UserName & " has been added to GroupName:" & GroupName & " on Server:" & ServerName & "."
objStdIn.close
objStdOut.close
wscript.quit
	
Function getComputer()
'Get computer name 
	Dim objNet
	Set objNet = WScript.CreateObject("WScript.Network") 
	getComputer = objNet.ComputerName 
	Set objNet = Nothing 
End Function

