+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
Scripts Thread, VBScript Error in Coding and Web Development; OK, so I'm really quite new to this. However, I know that it is necessary. I have been trying to ...
  1. #1

    Join Date
    Mar 2007
    Location
    Cambridge, UK & Reno, USA
    Posts
    339
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0

    VBScript Error

    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?)

    Code:
    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)

  2. #2
    Iain's Avatar
    Join Date
    Oct 2006
    Location
    Warwickshire
    Posts
    187
    Thank Post
    28
    Thanked 93 Times in 53 Posts
    Rep Power
    31

    Re: VBScript Error

    You need to put in a
    Code:
    objUser.SetInfo
    before setting the password and the user account control . e.g.
    Code:
    objUser.SetInfo
    objUser.Put "userAccountControl", ADS_UF_NORMAL_ACCOUNT
    objUser.SetPassword(strPwd)
    objUser.SetInfo

  3. #3

    Join Date
    Mar 2007
    Location
    Cambridge, UK & Reno, USA
    Posts
    339
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0

    Re: VBScript Error

    Quote Originally Posted by Iain
    You need to put in a
    Code:
    objUser.SetInfo
    before setting the password and the user account control . e.g.
    Code:
    objUser.SetInfo
    objUser.Put "userAccountControl", ADS_UF_NORMAL_ACCOUNT
    objUser.SetPassword(strPwd)
    objUser.SetInfo
    Brilliant! I knew it would be simple.

  4. #4

    Join Date
    Mar 2007
    Location
    Cambridge, UK & Reno, USA
    Posts
    339
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0

    Re: VBScript Error

    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
    Code:
    strUser
    instead of
    Code:
    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.

  5. #5
    Iain's Avatar
    Join Date
    Oct 2006
    Location
    Warwickshire
    Posts
    187
    Thank Post
    28
    Thanked 93 Times in 53 Posts
    Rep Power
    31

    Re: VBScript Error

    The user logon name is defined by setting the userPrincipalName. However the string must include the domain name in the form username@domainname.sch.uk (assuming you use sch.uk, maybe .local?)

    Code:
    objUser.Put "userPrincipalName", strUser & "@domainname.sch.uk"
    What code are you trying to use to set the folder permissions?

  6. #6

    Join Date
    Mar 2007
    Location
    Cambridge, UK & Reno, USA
    Posts
    339
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0

    Re: VBScript Error

    Quote Originally Posted by Iain
    What code are you trying to use to set the folder permissions?
    Code:
    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

  7. #7
    Iain's Avatar
    Join Date
    Oct 2006
    Location
    Warwickshire
    Posts
    187
    Thank Post
    28
    Thanked 93 Times in 53 Posts
    Rep Power
    31

    Re: VBScript Error

    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.

    Code:
    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:

    Code:
    strCmd = "cacls.exe " & strFull & " /e /g " & strDomain & "\" & strUser & ":F"
    WScript.Echo strCmd
    objShell.Run strCmd
    Hope this helps,

    Iain.

  8. #8

    Join Date
    Mar 2007
    Location
    Cambridge, UK & Reno, USA
    Posts
    339
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0

    Re: VBScript Error

    Quote Originally Posted by Iain
    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.

  9. #9
    Iain's Avatar
    Join Date
    Oct 2006
    Location
    Warwickshire
    Posts
    187
    Thank Post
    28
    Thanked 93 Times in 53 Posts
    Rep Power
    31

    Re: VBScript Error

    Sorry forgot to mention earlier, to set the home drive use:

    Code:
    objUser.Put "homeDrive","<drive letter>"
    objUser.Put "homeDirectory","<path to home drive>:"

  10. #10

    Join Date
    Mar 2007
    Location
    Cambridge, UK & Reno, USA
    Posts
    339
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0

    Re: VBScript Error

    Quote Originally Posted by Iain
    Sorry forgot to mention earlier, to set the home drive use:

    Code:
    objUser.Put "homeDrive","<drive letter>"
    objUser.Put "homeDirectory","<path to home drive>:"
    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.

  11. #11
    Iain's Avatar
    Join Date
    Oct 2006
    Location
    Warwickshire
    Posts
    187
    Thank Post
    28
    Thanked 93 Times in 53 Posts
    Rep Power
    31

    Re: VBScript Error

    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.
    Code:
    strGroupDN = "LDAP://<GROUP DN>," & objRootLDAP.Get("defaultNamingContext") ' e.g. <GROUP DN> = 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.

  12. #12

    Join Date
    Mar 2007
    Location
    Cambridge, UK & Reno, USA
    Posts
    339
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0

    Re: VBScript Error

    Quote Originally Posted by Iain
    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.
    Code:
    strGroupDN = "LDAP://<GROUP DN>," & objRootLDAP.Get("defaultNamingContext") ' e.g. <GROUP DN> = 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?

    Code:
    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:

    Code:
    strUser ="s"& Lcase(Left(strLast,3)) & strNum
    strContainer = "OU=Year5,OU=Students ,"
    strGroup = "cn=Students ,"

  13. #13
    Iain's Avatar
    Join Date
    Oct 2006
    Location
    Warwickshire
    Posts
    187
    Thank Post
    28
    Thanked 93 Times in 53 Posts
    Rep Power
    31

    Re: VBScript Error

    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,<rest of default naming context e.g. dc=xxx,dc=yyy etc.>.
    Is this really the case or is it in the parent OU, Students?

    Iain.

  14. #14
    apeo's Avatar
    Join Date
    Sep 2005
    Location
    Lost
    Posts
    1,612
    Thank Post
    95
    Thanked 115 Times in 111 Posts
    Rep Power
    41

    Re: VBScript Error

    Try this:

    Code:
    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.

  15. #15

    Join Date
    Mar 2007
    Location
    Cambridge, UK & Reno, USA
    Posts
    339
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0

    Re: VBScript Error

    Quote Originally Posted by Iain
    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,<rest of default naming context e.g. dc=xxx,dc=yyy etc.>.
    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:

    Code:
    strGroup = "LDAP://cn=students,OU=Groups," & objRootLDAP.Get("defaultNamingContext") ' e.g. <GROUP DN> = 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.

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. VBScript / SQL Server
    By Gatt in forum Scripts
    Replies: 7
    Last Post: 23rd September 2011, 03:21 PM
  2. VBScript IDE + Debugger
    By Geoff in forum Scripts
    Replies: 4
    Last Post: 13th September 2007, 08:56 PM
  3. another VBScript question!
    By StewartKnight in forum Coding
    Replies: 4
    Last Post: 3rd May 2007, 05:41 PM
  4. VBScript
    By StewartKnight in forum Coding
    Replies: 5
    Last Post: 1st May 2007, 11:04 AM
  5. vbscript arguements ?
    By mac_shinobi in forum Scripts
    Replies: 1
    Last Post: 20th February 2007, 10:29 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •