Scripts Thread, AD Bulk User Importer in Coding and Web Development; Ok I have been promising to produce something like this so after the morning gardening this has been my therapy ...
Ok I have been promising to produce something like this so after the morning gardening this has been my therapy :P
This is a GUI front end for a script I produced in VB script last year.
The full features of the original script included:
Import information from a CSV file.
UserName generation.
Creation of home directories and setting of permission and ownership (Using cacls and the windows version of CHOWN)
**** The author takes no responsibility for any damage that could be caused by this script USE AT YOUR OWN RISK!!!!!!!!! ****
Current version 1.0
This must be run on a Windows XP machine that is a member of a domain
At the moment it will read the following information from a CSV file (in this order)
Surname, First Name, UserName, Password
It is also assumed that the OU already exists and you must specify a group
that must be in the same OU as the users are going. This will be made more flexible later
As I gradually migrate the code over I will be adding the following features:
Add more fields to be inputed through the GUI such as:
home drive path, home drive letter
Other functions to include the creation of home directory and setting ownership and permissions.
Username generation.
Smarten up the HTML so it doesnt look like a year 7 produced it :P
Edit 05/05/2006
Ok its looking quiete good now. The error checking is mostly in and seems to work ok. Check out the avi on the second page. I have also added a visual log.
I will post the current version once I have got rid of a few bugs
I have most of the code already to slot in. It will just need a little rearranging. The only thing I would have to write from scratch would be the username generation routine as mine is hard coded to our naming scheme but that shouldnt be too hard.
ChrisH:
I made a single-user vbscript which did this sort of thing all-in-one so that I didn't have to mess around when a new pupil came to the school in the middle of a school year. It's quite handy for quickly creating test accounts too.
On top of the basics, it made the home folder, set permissions on it and created a share on it (as well as setting all the properties of their account I needed e.g. group memberships*). I did some bits of it by calling cacls.exe and rmtshare.exe with WshShell.Run rather than pure VB, because I didn't have any great plans on distributing it to anyone else.
The only thing I never got round to was creating their mailbox in Exchange, because that needed more advanced veebee voodoo. If I look at it again, I'll probably use this: http://www.joeware.net/win/free/tools/exchmbx.htm
because laziness generally wins against elegance!
If you want the code, PM me - it's so heavily customised for our particular setup that it wouldn't be much use as a starting point for people, but you might be able to lift bits you like.
One feature request would be to let the csv file have column headers, so that you could make some parameters optional. It's worth bearing in mind though that a lot of properties of user accounts can be done in bulk in WS2003's ADUC by multi-selecting so there's not much of a time-saving aspect after you consider that you have to generate the csv file in the first place.
*yes, I know that isn't technically a property of the user account!
My original script that is fully working is much the same as yours. I used cacls for permissions and the the windows version of CHOWN for ownership.
I didnt even think about Exchange thought so I will put that on list.
That is a good suggestion about the column headers for the CSV file I do have them currently on the version I am using but they are more of a guide when you first start putting data into it. I will make it more intelligent though as you suggested.
The thing im struggling on at the moment is the error trapping. To check the existence of a user an object in AD I use something like this:
Code:
Set ObjParent = Getobject (OUPath) 'Get the container
MsgBox Err.Number
If Err.Number <> 0 Then
CheckOUExists = False
Msgbox "The OU was not found try again"
Else
CheckOUExists = True
MsgBox "OU Exists"
Err.Clear
End If 'Error handling
This works fine in a VB script but put the when ran this way it doesnt like it and it comes up with a Internet Explorer error. Using "On Error Resume Next" doesnt work in this context either.
Can someone enlighten me as to what kind of construct I need to use.
Ok its looking like error checking needs to be done is JS I think?
Does anyone have any input on this?
(and I thought this was going to be a simple transition )
With two exceptions, everything I just told you about handling the window.onerror event in pages scripted with JavaScript applies equally to those scripted with VBScript. The first difference is the exact syntax for indicating a window.onerror handler in VBScript:
Code:
<SCRIPT language="VBScript">
Function window_onerror ( message, url, line )
window_onerror = true
End Function
</SCRIPT>
As you can see, the VBScript syntax for installing an event handler requires naming a function in the convention object_event. In this case we want to install a handler for the onerror event on the window object, so we create a VBScript function named window_onerror. Also, in VBScript, return values are indicated by assignment to a variable named after the function.
I haven't tested that, so caveat scriptor, but I guess you could use the stuff on that page to pull some information out about the error and process it.
Thanks I had got halfway there by having it generate a generic error but just needed to pass "message" to the function so it came up with the proper error which it seems to be doing quite well.
Oh well least I learned something new today
Ok I have made a slight update. I have updated the code and the download above.
I have added fields for a group to add the users to and given the option to disable users on creation.
You can also now choose an OU at any depth as long as you specify the path.
If you have a top level OU called students and a year7 OU in there then you must type in
Code:
year7,students
No space either side of the comma. So basically your working your way backwards to the top level. If you press test it will check of the OU exists and display the LDAP path to you.
I'm still working on the error correction as it seems I have to use java script for this which I wasnt expecting (if anyone knows better let me know)
This can break in several parts including:
The OU doesnt exist or you types the path wrong.
The group field is left blank (You must put something at the moment).
The group object is not in the same place as the users are going to (will make that more flexible later).
The user objects already exist.
The code might be a bit sloppy in places but I will tidy up once everything is working.
Right well after seeing the code for another HTA which was using normal VBS error control I thought wtf!!! I then realised shortly before kicking myself very hard that the reason the error checking was not working was because all of my VB code was in Functions and Subs so my On error resume next statement that I had just plonked above them all had no effect ops:
So this is much better for me and will spend some time on it tommorow and release something a little prettier with more functionality and more inportant a little more stable
Ok this is going along nicely at the moment. All the error checking code is working nicely and I have a visual log that runs as the script executes. I havent updated the first post as I still have a few more bugs to iron out.
I am attaching a html file with a mock up of the interface. If anyone wants to mess around with the styles and make it look a bit better then please be my guest (so I can concentrate on the coding ) . Just makes sure it fits within the current width dimensions of the left and right content divs.
Once I get this running to my satisfaction I will work on the next phase which will be more intelligent routines from reading from the CSV file. This will involve a first screen where you select the file and it reads the column headers. It will then on the second screen add the common fields that arent accounted for in the CSV file.
This will allow more flexibility to allow different values for fields like description read in from the file or the same for all if input from the GUI.
PS I just noticed its broken under firefox as it's been running under a HTA in an explorer process I didnt notice :P