sqdge Posted September 7, 2007 Posted September 7, 2007 OK, so I'm really quite new to this. However, I know that it is necessary. I have been trying to work out how to design a little widget that will add a user (don't ask why, just accept that I need to do it!) I think I've got pretty close, but an error keeps on popping up. It looks like it relates to the password bit, but I'm not quite sure (it's on line 31). I'm sure one of you will solve it instantly...or suggest I stick to something else. Anyway, here's the code (why am I so nervous?) Option Explicit Dim objRootLDAP, objContainer, objUser, objShell, objOU, objFSO, objFolder Dim strUser, strName, strLast, strNum, strContainer, strPwd, strDirectory Dim intPwdValue Const ADS_UF_NORMAL_ACCOUNT = 512 strName = InputBox("First Name") strLast = InputBox("Last Name") strNum = InputBox("Number") strUser ="s"& Lcase(Left(strLast,3)) & strNum strContainer = "OU=Students ," strPwd = "foot" strDirectory = "F:\Students\2007\" intPwdValue = 0 ' Bind to Active Directory, Users container. Set objRootLDAP = GetObject("LDAP://rootDSE") Set objContainer = GetObject("LDAP://" & strContainer & _ objRootLDAP.Get("defaultNamingContext")) ' Build the actual User. Set objUser = objContainer.Create("User", "cn=" & strUser) objUser.Put "sAMAccountName", strUser objUser.Put "givenName", strName objUser.Put "sn", strLast objUser.Put "displayName", strName & " " & strLast objUser.Put "userAccountControl", ADS_UF_NORMAL_ACCOUNT objUser.SetPassword(strPwd) ObjUser.SetInfo objUser.AccountDisabled = FALSE objUser.SetInfo ObjUser.Put "pwdLastSet", intPwdValue objUser.SetInfo Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.CreateFolder(strDirectory & strUser)
Iain Posted September 7, 2007 Posted September 7, 2007 You need to put in a objUser.SetInfo before setting the password and the user account control . e.g. objUser.SetInfo objUser.Put "userAccountControl", ADS_UF_NORMAL_ACCOUNT objUser.SetPassword(strPwd) objUser.SetInfo
sqdge Posted September 7, 2007 Author Posted September 7, 2007 You need to put in a objUser.SetInfo before setting the password and the user account control . e.g. objUser.SetInfo objUser.Put "userAccountControl", ADS_UF_NORMAL_ACCOUNT objUser.SetPassword(strPwd) objUser.SetInfoBrilliant! I knew it would be simple.
sqdge Posted September 7, 2007 Author Posted September 7, 2007 OK, so now I can create the user and a folder. The next trick is to add permissions to that folder. I did try a piece of code but it didn't want to see to work. I put it down to the fact that I was putting strUser instead of DOMAIN\strUser However, I couldn't find a way of writing that down so that it was happy. Any ideas? On another note, when I am going for objUser.Put - I can't find the write terminology to define the User Logon Name (it defines the pre-windows 2k one fine, but I would quite like the other box filled out). I would also like to tell it to connect the H: drive to the user's home folder. Any help would, as always, be gratefully appreciated.
Iain Posted September 7, 2007 Posted September 7, 2007 The user logon name is defined by setting the userPrincipalName. However the string must include the domain name in the form [email protected] (assuming you use sch.uk, maybe .local?) objUser.Put "userPrincipalName", strUser & "@domainname.sch.uk" What code are you trying to use to set the folder permissions?
sqdge Posted September 7, 2007 Author Posted September 7, 2007 What code are you trying to use to set the folder permissions? Set objShell = WScript.CreateObject("Wscript.Shell") objShell.Run "cacls.exe strFull /e /g strDomain & strUser:F" strFull = the full path to the folder strDomain = The school Domain
Iain Posted September 7, 2007 Posted September 7, 2007 Unfortunatly vbscript does not substitute variable names in strings (like you can in perl / PHP / etc.), so you need to break the string up and concatenate the variables with the other parts using the & operator. E.g. objShell.Run "cacls.exe " & strFull & " /e /g " & strDomain & "\" & strUser & ":F" In this situation, I often find it useful to define the command as a string, which allows you to echo it for debugging purposes: strCmd = "cacls.exe " & strFull & " /e /g " & strDomain & "\" & strUser & ":F" WScript.Echo strCmd objShell.Run strCmd Hope this helps, Iain.
sqdge Posted September 7, 2007 Author Posted September 7, 2007 Hope this helps I'll find out on Tuesday - by which time, someone will have probably done everything manually. Thanks for all your help on this.
Iain Posted September 7, 2007 Posted September 7, 2007 Sorry forgot to mention earlier, to set the home drive use: objUser.Put "homeDrive","" objUser.Put "homeDirectory",":"
sqdge Posted September 11, 2007 Author Posted September 11, 2007 Sorry forgot to mention earlier, to set the home drive use: objUser.Put "homeDrive","" objUser.Put "homeDirectory",":" Thanks for that, it all works swimmingly - bar two things. Firstly, when I look into User and Computers, the "Name" that is being displayed is their username. However, I define their display name as being their first and last name. Any idea how to change that? Secondly, do you know how to script joining a particular group? I need these kids to be part of a group called "Students" so that they inherit the group policy for it. Cheers.
Iain Posted September 11, 2007 Posted September 11, 2007 I believe that the default name displayed in ADCU is whatever the CN for the object is, which in the case of your script has been set to strUser. So you will need to change what you set the CN to if you want to change this. Adding a object to a group is quite simple first you need to bind to the group object using the GetObject method (same as you have done for binding to the students OU). You can then add the user to the group using the group's Add method. E.g. strGroupDN = "LDAP://," & objRootLDAP.Get("defaultNamingContext") ' e.g. = cn=students,OU=something Set objGroup = GetObject(strGroupDN) strUserDN = "LDAP://cn=" & strUser & "," & strContainer & objRootLDAP.Get("defaultNamingContext") ' Assuming cn=strUser objGroup.Add(strUserDN) Hope this makes sense, Iain.
sqdge Posted September 12, 2007 Author Posted September 12, 2007 I believe that the default name displayed in ADCU is whatever the CN for the object is, which in the case of your script has been set to strUser. So you will need to change what you set the CN to if you want to change this. Adding a object to a group is quite simple first you need to bind to the group object using the GetObject method (same as you have done for binding to the students OU). You can then add the user to the group using the group's Add method. E.g. strGroupDN = "LDAP://," & objRootLDAP.Get("defaultNamingContext") ' e.g. = cn=students,OU=something Set objGroup = GetObject(strGroupDN) strUserDN = "LDAP://cn=" & strUser & "," & strContainer & objRootLDAP.Get("defaultNamingContext") ' Assuming cn=strUser objGroup.Add(strUserDN) Hope this makes sense, Iain. It kind of made sense. However, when I tried it, it wasn't happy. I had a route around on the internet for an alternative version - but that isn't happy either. Any ideas? Set objRootLDAP = GetObject("LDAP://RootDSE") strDNSDomain = objRootLDAP.Get("DefaultNamingContext") Set objUser = GetObject("LDAP://" & strUser & strContainer & strDNSDomain) Set objGroup = GetObject("LDAP://" & strGroup & strContainer & strDNSDomain) objGroup.add(objUser.ADsPath) FYI: strUser ="s"& Lcase(Left(strLast,3)) & strNum strContainer = "OU=Year5,OU=Students ," strGroup = "cn=Students ,"
Iain Posted September 12, 2007 Posted September 12, 2007 What errors are you getting? Where is the group that you are trying to add the users to? Looking at the code, the group should be called Students and it should be in the same OU as the users: ou=Year5,ou=Students,. Is this really the case or is it in the parent OU, Students? Iain.
apeo Posted September 12, 2007 Posted September 12, 2007 Try this: Set oDomain = GetObject("WinNT://" & DomainName Set oGroup = oDomain.GetObject("Group", GroupName) oGroup.Add ("WinNT://" & DomainName & "/" & AccountName) Set oGroup=Nothing Ok this has be roughly ripped from my hta so i might have missed something but i think its all there.
sqdge Posted September 13, 2007 Author Posted September 13, 2007 What errors are you getting? Where is the group that you are trying to add the users to? Looking at the code, the group should be called Students and it should be in the same OU as the users: ou=Year5,ou=Students,. Is this really the case or is it in the parent OU, Students? Iain. I get the "Can't find object on the server" error. I am certain that I am pointing to the right place (I made a mistake earlier - the group is the parent directory in a place called "Groups"). Anyway, this is the code so far: strGroup = "LDAP://cn=students,OU=Groups," & objRootLDAP.Get("defaultNamingContext") ' e.g. = cn=students,OU=something Set objGroup = GetObject(strGroup) strUserDN = "LDAP://cn=" & strUser & "," & strContainer & objRootLDAP.Get("defaultNamingContext") ' Assuming cn=strUser objGroup.Add(strUserDN) There must be something obvious I am missing. Thanks.
Iain Posted September 13, 2007 Posted September 13, 2007 Is the error occuring when binding to the group object or when adding the user to the group? Try using WScript.Echo strUserDN and WScript.Echo strGroup to see what these variables are set to. Looking at your code the group should be called students and in an OU called Groups which is in the root of the directory. The user objects should should be in an OU called Year5, which is in an OU called Students, which is in the directory root. Is this correct? Iain.
sqdge Posted September 13, 2007 Author Posted September 13, 2007 Is the error occuring when binding to the group object or when adding the user to the group? Try using WScript.Echo strUserDN and WScript.Echo strGroup to see what these variables are set to. Looking at your code the group should be called students and in an OU called Groups which is in the root of the directory. The user objects should should be in an OU called Year5, which is in an OU called Students, which is in the directory root. Is this correct? Iain. I am definitely pointing to the right areas for the user and group. Having transferred my code to NoteTab Lite, I now know that it has a problem with the strUserDN line - claiming that there is no such object on the server. I can't seem to use the WScript.Echo because the program stops as soon as it gets to the problem, and I can only put the echo after those lines.
Iain Posted September 13, 2007 Posted September 13, 2007 You can put a WScript.Echo anywhere you like in a script. If you want to know the value of strUserDN before it is used, put the echo command before the line objGroup.Add(strUserDN) If you want to know the vale of strGroup, put the echo command before the line Set objGroup = GetObject(strGroup) Iain.
sqdge Posted September 13, 2007 Author Posted September 13, 2007 You can put a WScript.Echo anywhere you like in a script. If you want to know the value of strUserDN before it is used, put the echo command before the line objGroup.Add(strUserDN) If you want to know the vale of strGroup, put the echo command before the line Set objGroup = GetObject(strGroup) Iain. It seems to return back all the correct settings: The user is in an OU called "Year5" within an OU called "Students", and the group is called "students" in an OU called "Groups". Yet still it returns this "there is no such object on the server" error.
Iain Posted September 13, 2007 Posted September 13, 2007 The two OUs 'Students' and 'Groups' aren't inside any other OUs are they? It may be worth downloading an LDAP browser, such as this (needs a java > 1.2.2) If you use this to navigate to a user object in the Year5 OU and take a look at the distinguishedName attribute, this should be the same (excluding the ldap:// and different cn) as the output for strUserDN. Do the same for the Students group object and strGroup. Iain
Iain Posted September 13, 2007 Posted September 13, 2007 Also try running the script in the Microsoft Script Debugger, this allows you to set breakpoints and check variable values on the fly. Use either wscript //X or cscript //X to start the debugger. Iain.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now