Showing posts with label windows. Show all posts
Showing posts with label windows. Show all posts

Thursday, June 11, 2009

User creation script

A consultant recently asked me for a copy of a script I created for our school to automate user creation. I thought it would be more useful for myself and the world if I just archived it here where I can always find it, and where other folks can use it (I find it much harder generally to find usable sample window code &c. than I do with linux).

These are my first (and hopefully last) scripts in virtual basic scripting. I'm putting them here to make it easier to share them, but these haven't really been prepared properly for sharing (I was simply working from examples until these worked). Obviously it would be worth setting these up with tweakable parameters up top at some future point.

Script for creating students


Create Users



This script creates student user accounts based on a CSV file with the following structure:

username, first, last, group, password


' Script to create student accounts
' Created by Tom Hinkle
' (or, if he's moved on,
'
' Requires file named students.csv in same directory with requisite information.
'
' File format is plain csv (no escaping) as follows:
'
' username, first, last, year-of-grad, password
'
' Note that year-of-grad is used for group names. We expect there to be an OU
' with the name of e.g. YOG2012 for each year-of-grad. Also, student folders
' are named by YOG, so we expect the students folder to contain a folder titled
' e.g. YOG2012

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
upnSuffix = "cpcs.com"
Set objRootDSE = GetObject("LDAP://rootDSE")

dim fs,objTextFile
set fs=CreateObject("Scripting.FileSystemObject")
dim arrStr
set objTextFile = fs.OpenTextFile("students.csv")

Do while NOT objTextFile.AtEndOfStream
arrStr = split(objTextFile.ReadLine,",")
givenName = arrStr(1)
lastName = arrStr(2)
sn = lastName
cn = givenName & " " & lastName
OU = "YOG" & arrStr(3)
samAccountName = arrStr(0)
' Specify the NetBIOS name of the domain.
strNetBIOSDomain = "cpcs"
userPrincipalName = samAccountName & "@" & upnSuffix
homedir = "\\mms1.cpcs.com\students\" & OU & "\" & samAccountName
groupname = "LDAP://ou=" & OU & ",ou=Student,DC=CPCS,DC=com"

Set objContainer = GetObject(groupname)
Set objUser = objContainer.Create("User", "cn=" & cn)
objUser.Put "sAMAccountName", samAccountName
objUser.Put "userPrincipalName", userPrincipalName
objUser.Put "sn", sn
objUser.Put "givenName", givenName
objUser.Put "displayName", givenName & " " & sn
objUser.Put "userAccountControl", 512
objUser.Put "homeDirectory", homedir
objUser.Put "homeDrive", "H"
objUser.Put "scriptPath", "SLogon.bat"

On Error Resume Next
objUser.SetInfo
If (Err.Number 0) Then
Wscript.Echo "Unable to create " & samAccountName
End If
'On Error GoTo 0


objUser.SetInfo
objUser.SetPassword arrStr(4)
objUser.SetInfo



' Create folder, based on:
' http://www.eggheadcafe.com/software/aspnet/30289055/modifying-user-home-direc.aspx


If (objFSO.FolderExists(homedir) = False) Then
' Create folder.
On Error Resume Next
objFSO.CreateFolder homedir
If (Err.Number 0) Then
Wscript.Echo "Unable to create home directory for " & strSAM
End If
On Error GoTo 0
End If
If (objFSO.FolderExists(homedir) = True) Then
' Assign permissions to home directory.
intRunError = objShell.Run("%COMSPEC% /c echo Y| cacls " _
& homedir & " /T /E /C /G " & strNetBIOSDomain _
& "\" & samAccountName& ":F", 2, True)
If (intRunError 0) Then
Wscript.Echo "Unable to assign permissions for " & samAccountName
End If
End If
Loop
objTextFile.Close
set objTextFile = Nothing
set fs = Nothing
wscript.Echo("When you are done, you will want to go into active directory, highlight the new users manually, and add them to the group Students. You'll also need to disallow changing the password. Sorry I didn't get this done programmatically, but it's relatively simple to do manually")

Tuesday, December 9, 2008

Do real people actually use Windows Server?

Tonight I got one of many tech e-mails I get at school. In this case, the issue was related to a printer being down -- actually, a number of printers. If enough printers fail, it's a high priority issue, because it means teachers and students walking long distances to get print outs, and that's a lot of lost time at school (students may be thankful for the exercise and chance to socialize, but that's not really what we're aiming for).

Anyway, I decided it would be worthwhile to go into the server remotely and see what I could about the condition of the printers, so I could prioritize how important it was to get to fixing these printers soon. But the server is a Windows 2003 server. Our standard way of accessing it is over VNC or RDP, both of which can be really slow in the wrong conditions.

I've been waiting a good 30 seconds to get a response to each click remotely, which makes my "quick check" not so quick. This raises the question: why on God's green earth am I "clicking" anything?

There are lots of reasons why it doesn't make sense for a server to run a GUI. Among them is this: it's incredibly inefficient to transmit a picture of a whole desktop, and mouse events, etc., just to find out some basic information. If this thing were simply a *nix commandline, a 1200 baud modem would have sufficed to complete my inquiry in a few minutes. As it is, I'm giving up after a half hour (for what it's worth, I've been doing other things while I wait for responses from RDP). Do real people use this crap?

More and more I've been missing the commandline as I have to do admin work. I know that clicking is (theoretically) faster than typing commands, but between the ease of doing things remotely (I've spent hours programming on a remote system in emacs without noticing any difference from working locally) and the scriptability inherent in the commandline, I'm finding it hard to believe that people who administer machines for a living could ever do without a decent shell as their base of operations.

(Note: I know windows has a command prompt, but it's a pale shadow of bash, and as far as I know out of the box Windows Server does not have an ssh server enabled)