?
Solved

Convert from VB Script to VB6

Posted on 2003-03-26
4
Medium Priority
?
443 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:NBCOLLEGE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 1

Expert Comment

by:Xadine
ID: 8226888
Pretty generic question. What specifically in that code can't you replicate in VB6?
0
 
LVL 6

Accepted Solution

by:
DominicCronin earned 300 total points
ID: 8236835
Copy and paste the code into a .bas module in VB. (Put the stuff that isn't in functions into a function called main, and set the Project properties "Startup object" to "Sub main".
Make sure "Option Explicit" is switched off at the top of the source file. Change the Wscript.Echo commands into Msgbox commands.

That should work well enough. If you want to get full marks for your assignment, you'll need to add types to you Dim statements (and switch option explicit back on) and maybe build a nice user interface.
0
 

Expert Comment

by:CleanupPing
ID: 9446756
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.
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Progress
Starting up a Project
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question