Link to home
Start Free TrialLog in
Avatar of NBCOLLEGE
NBCOLLEGE

asked on

Convert from VB Script to VB6

I have the following to add users any ideas how to convert it from script to VB6?

On Error Resume Next

Dim WshShell

Dim importFilename
Dim exceptionFilename
Dim container
Dim stafforstudent
Dim whichsite
Dim domain
Dim env
Dim userObject
Dim fileServer
Dim fso
Dim fileHandle
Dim fileHandle2
Dim lineOText
Dim readUN
Dim readFN
Dim readSN
Dim readDOB
Dim readAdd1
Dim readAdd2
Dim readAdd3
Dim readPOBox
Dim readTel
Dim readCO
Dim firstName
Dim lastName
Dim space
Dim suffix

Dim adBound
Dim ouBound
Dim ouGroups

Set WshShell = WScript.CreateObject("WScript.Shell")
Set env=WshShell.Environment("PROCESS")

importFilename=InputBox("Please enter the name of the file to import:","Import Users")
If importFilename="" Then WScript.Quit(1)
exceptionFilename=InputBox("Please enter the name of the exception file that will contained failed additions:","Import Users",Left(importFilename,Len(importFilename)-4)+"_exceptions.csv")
If exceptionFilename="" Then WScript.Quit(1)

suffix="nbcollege.ac.uk"

stafforstudent=InputBox("Please enter Staff or Students:","Import Users","Students")
If stafforstudent="" Then WScript.Quit(1)
whichsite=InputBox("Please enter the which site to create the users E.G Broadwater,Durrington,etc:","Import Users","Broadwater")
If whichsite="" Then WScript.Quit(1)
fileserver=InputBox("Please enter the location of the home directory E.G wdfs01 or bwfs01:","Import Users","bwfs01")
If fileserver="" Then WScript.Quit(1)

domain="LDAP://nbcollege.ac.uk"

WScript.Echo "Connecting to "+domain+"..."

Set adBound=GetObject(domain)
If Err Then
  WScript.Echo "Unable to connect!"
  WScript.Sleep(5000)
      WScript.Quit(1)
End If

WScript.Echo "Connected to "+domain

Set ouBound=adBound.GetObject("Organizational Unit", "ou="+whichsite+",ou="+stafforstudent+",ou=users,ou=resources")
If Err Then
  WScript.Echo "Unable to bind to org unit!"
  WScript.Sleep(5000)
      WScript.Quit(1)
End If

Set fso=CreateObject("Scripting.FileSystemObject")

Set fileHandle=fso.OpenTextFile(importFilename,1)
If Err Then
  WScript.Echo "Unable to open import file!"
  WScript.Sleep(5000)
      WScript.Quit(1)
End If

Set fileHandle2=fso.CreateTextFile(exceptionFilename,1)
If Err Then
  WScript.Echo "Unable to create exception file!"
      fileHandle.Close
  WScript.Sleep(5000)
      WScript.Quit(1)
End If

While Not(fileHandle.AtEndOfStream)
  lineOText=fileHandle.ReadLine
  If lineOText<>"" Then
    readUN=GetField(lineOText)
    readSN=GetField(lineOtext)
    readFN=GetField(lineOText)
    readDOB=GetField(lineOText)
    readAdd1=GetField(lineOText)
    readAdd2=GetField(lineOText)
    readAdd3=GetField(lineOText)
    readPOBox=GetField(lineOText)
    readTel=GetField(lineOText)
    readCO=GetField(lineOText)

    If readFN="" Then readFN="-"
    If readSN="" Then readSN="-"
    If readDOB="" Then readDOB="-"
    If readAdd1="" Then readAdd1="-"
    If readAdd2="" Then readAdd2="-"
    If readAdd3="" Then readAdd3="-"
    If readPOBox="" Then readPOBox="-"
    If readTel="" Then readTel="-"
    If readCO="" Or readCO="---" Then readCO="No Course Code"    

    WScript.Echo "Username:       "+readUN
    WScript.Echo "Surname:        "+readSN
    WScript.Echo "Firstname:      "+readFN
    WScript.Echo "DOB:                "+readDOB
    WScript.Echo "Address1:       "+readAdd1
    WScript.Echo "Address2:       "+readAdd2
    WScript.Echo "Address3:       "+readAdd3
    WScript.Echo "POBOx:          "+readPOBox
    WScript.Echo "Tel:            "+readTel
    WScript.Echo "Course Code:    "+readCO

            WScript.Echo "First Name:     "+firstName
            WScript.Echo "Surname:        "+lastName

    Set userObject=ouBound.Create("User","cn="+readUN+" - "+readFN+" "+readSN)
            If Err Then
              WScript.Echo "Unable to create user!"
              fileHandle2.WriteLine readUN+","+readSN+","+readDOB+","+readAdd1+","+readAdd2+","+readAdd3+","+readPOBox+","+readTel+","+readCO
                  Err.Clear
            Else
      userObject.UserPrincipalName=readUN+"@"+suffix
      userObject.FullName=readFN+" "+readSN
      If firstName<>"" Then userObject.GivenName=firstName
      userObject.Firstname=readFN
      userObject.LastName=readSN
      userObject.SamAccountName=readUN
      userObject.pager=readDOB+", Date of Birth"
      userObject.streetAddress=readAdd1+"                  "+readAdd2+"                  "+readAdd3
      userObject.postalCode=readPOBox
      userObject.homePhone=readTel
      userObject.HomeDrive="H:"
      userObject.Description=readCO+"  - Course"
      userObject.HomeDirectory="\\"+fileServer+"\"+stafforstudent+"$\"+readUN+"\Home"
      userObject.ProfilePath="\\"+fileServer+"\"+stafforstudent+"$\"+readUN+"\Profile"
  '    userObject.TerminalServicesProfilePath="\\"+fileServer+"\"+stafforstudent+"$\"+readUN+"\TSProfile"
      userObject.SetInfo
                  If Err Then
                    WScript.Echo "Unable to set user info!"
                    fileHandle2.WriteLine readUN+","+readSN+","+readDOB+","+readAdd1+","+readAdd2+","+readPOBox+","+readTel+","+readCO
                        Err.Clear
                  Else
        userObject.SetPassword "password"
                userObject.PwdLastSet=0
                userObject.AccountDisabled=False
                userObject.SetInfo

    fso.CreateFolder "\\"+fileServer+"\"+stafforstudent+"$\"+readUN
    fso.CreateFolder "\\"+fileServer+"\"+stafforstudent+"$\"+readUN+"\Home"
    fso.CreateFolder "\\"+fileServer+"\"+stafforstudent+"$\"+readUN+"\Profile"
    fso.CreateFolder "\\"+fileServer+"\"+stafforstudent+"$\"+readUN+"\Home\My Documents"
    fso.CreateFolder "\\"+fileServer+"\"+stafforstudent+"$\"+readUN+"\Home\My Documents\My Pictures"

'    WshShell.Run "cmd.exe /c echo y|cacls \\"+fileServer+"\"+stafforstudent+"$\"+readUN+" /t /g "+readUN+":c 'administrators:f SYSTEM:f",0

WshShell.Run "cmd.exe /c echo y|xcacls \\"+fileServer+"\"+stafforstudent+"$\"+readUN+" /t /g "+readUN+":c administrators:f sec-ManageStudentData:c Sec-Readstudentdata:r SYSTEM:f ",0

        WScript.Echo "The user, home directory and profile directory have been successfully created."
        WScript.Echo "Adding user to groups..."

Set ouGroups=GetObject("LDAP://OU=Course Groups,OU=Groups,OU=Resources,DC=nbcollege,DC=ac,DC=uk")

        Set groupObject=RSCGetGroup(readCO,ouGroups)
        groupObject.Add userObject.ADsPath
        WScript.Echo "User has been added to group '"+readCO+"'"
            End If


End If
End If
Wend

fileHandle.Close
fileHandle2.Close

WScript.Sleep(5000)

Function GetField(ByRef inputText)

Dim comma

comma=Instr(inputText,",")

If comma=0 Then
  GetField=inputText
  inputText=""
Else
  GetField=Left(inputText,comma-1)
  inputText=Mid(inputText,comma+1)
End If

End Function

Function RSCGetGroup(groupname,ByRef ouBound)

On Error Resume Next

Set RSCGetGroup=ouBound.GetObject("Group","cn="+groupname)

If Err Then
  Err.Clear
  WScript.Echo "Creating group '"+groupname+"'"
  Set RSCGetGroup=ouBound.Create("Group","cn="+groupname)
  If Err Then
    WScript.Echo "Unable to create group '"+groupname+"' ("+CStr(Err.Number)+")"
    WScript.Echo "Please add the user to the appropriate groups using the User Management utility"

    Err.Clear
    Exit Function
  End If
  RSCGetGroup.SamAccountName=groupname
  RSCGetGroup.SetInfo
  If Err Then
    WScript.Echo "Unable to set group info '"+groupname+"' ("+CStr(Err.Number)+")"
    WScript.Echo "Please add the user to the appropriate groups using the User Management utility"

    Err.Clear
    Exit Function
  End If
Else
  WScript.Echo "Located group '"+groupname+"'"
End If


End Function
Avatar of Xadine
Xadine

Pretty generic question. What specifically in that code can't you replicate in VB6?
ASKER CERTIFIED SOLUTION
Avatar of DominicCronin
DominicCronin
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
NBCOLLEGE:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.