We help IT Professionals succeed at work.
Get Started

vbscript doesn't finish For loop when run as a scheduled task

zuvbir asked
Last Modified: 2009-12-16
I have a vbscript that loops through an array.

For each item it reads a file, creates AD accounts, then sends and email notification.

When I run the script manually.  It works as expected and I get an email for each item in the array.

But when it runs as a scheduled task, I only get one email.  It seems to stop after the first item.


Below is an  version of the script. I took out domain specific names.
'  EDIT Site and Server Name

'On Error Resume Next

Option Explicit

Const ForReading = 1

Dim objOU, objOUString, objGroupString2, objUser, intUAC, fso, strLine, arrInput, objConnection, objCommand, objRecordset
Dim tsInputFile, tsInputFileString, SitesArray, Site, HomePathString, objGroup, ObjGroupString
Dim fldUserHomedir, wshShell, CreateCount

' Set up ADODB connection to search AD for user account.

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

SitesArray = Array( "ramo", "fran" )

For Each Site In SitesArray

MsgBox Site
CreateCount = 0

If Site = "ramo" Then

      objOUString = "LDAP://OU=students,OU=users,OU=Ramona,dc=domainname,dc=org"
      tsInputFileString = "Students_Ramona.txt"
      HomePathString = "\\polk\studenthome$\"
      ObjGroupString = "LDAP://cn=_USER_Ramona_Student,ou=Students,ou=users,ou=Ramona,dc=domainname,dc=org"
      ObjGroupString2 = "ou=Students,ou=users,ou=Ramona,dc=domainname,dc=org"
ElseIf Site = "fran" Then

      objOUString = "LDAP://OU=students,OU=users,OU=Frank,dc=domainname,dc=org"
      tsInputFileString = "Students_Frank.txt"
      HomePathString = "\\pierce\studenthome$\"
      ObjGroupString = "LDAP://cn=_USER_Frank_Student,ou=Students,ou=users,ou=Frank,dc=domainname,dc=org"
      ObjGroupString2 = "ou=Students,ou=users,ou=Frank,dc=domainname,dc=org"

End If

Set objOU = GetObject(objOUString)

Set fso = CreateObject("Scripting.FileSystemObject")

'get file  if exists

Set tsInputFile = fso.OpenTextFile("C:\vbscr\ReadData\" & tsInputFileString, ForReading, False)

' while loop until there are no more lines

While Not tsInputFile.AtEndOfStream

      ' read line, split into array
      strLine = tsInputFile.Readline
      arrInput = Split(strLine, ",")
      objCommand.CommandText = "SELECT AdsPath FROM 'LDAP://dc=domainname,dc=org' WHERE objectCategory='user' AND samAccountName='" & arrInput(0) & "'"

      Set objRecordSet = objCommand.Execute

      If Not objRecordSet.EOF Then
              'MsgBox "Dude, Im in thers already " & arrInput(1)
  'MsgBox arrInput(0)
      Set objUser = objOU.Create("user","cn=" & arrInput(0))
      objUser.Put "sAMAccountName", arrInput(0)
      objUser.Put "userPrincipalName", arrInput(0) & "@domainname.org"

      ' write the newly created object out from property cache
      ' read all properties including ones set by system on creation
      intUAC = objUser.Get("userAccountControl")
      objUser.put "userAccountControl",  intUAC XOR ADS_UF_DONT_EXPIRE_PASSWD

      ' set password
      objUser.SetPassword arrInput(3)
      ' set properties
      objUser.AccountDisabled = False
      objUser.FirstName = arrInput(2)
      objUser.LastName = arrInput(1)
      objUser.DisplayName = arrInput(1) & ", " & arrInput(2) & " Grade " & arrInput(4)
      objUser.PasswordRequired = True
      ' set drive
      objUser.HomeDirectory = HomePathString & arrInput(0)
      objUser.Put "homeDrive", "Z:"

      'MsgBox ObjGroupString

      Set objGroup = GetObject (ObjGroupString)
      objGroup.PutEx ADS_PROPERTY_APPEND, "member", Array("cn=" & arrInput(0) & "," & objGroupString2)

      'Create Dir.
      'MsgBox HomePathString & arrInput(0)
      If Not fso.FolderExists(HomePathString & arrInput(0)) Then
            Set fldUserHomedir = fso.CreateFolder(HomePathString & arrInput(0))
      End If
      ' set rights
      Set wshShell = WScript.CreateObject("Wscript.Shell")
      wshShell.Run "cacls " & HomePathString & arrInput(0) & " /E /G " & arrInput(0) & ":F", 0, False
      ' stop referencing this user
      Set objUser = Nothing
      ' MsgBox "Added account " & arrInput(0) & " " & arrInput(1)
            CreateCount = CreateCount + 1
      End If

Call SendEmailNotification("AD Student Account Maintenance", CreateCount & " Accounts were created for " & Site)

'MsgBox "All Done" & CreateCount & " accounts were created"


'close file

Function SendEmailNotification(Subject, TextBody)

Dim iMsg, iConf, Flds

set iMsg = CreateObject("CDO.Message")
set iConf = CreateObject("CDO.Configuration")

Set Flds = iConf.Fields

With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "jackson.domainname.org"
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10  
End With

With iMsg
    Set .Configuration = iConf
    .To = "dLastname@domainname.org"
    .From = "vbsript@somewhere.com"
    .Subject = Subject
    .TextBody = TextBody
End With

Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing

End Function


Watch Question
Top Expert 2007
This problem has been solved!
Unlock 2 Answers and 6 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE