Link to home
Start Free TrialLog in
Avatar of zuvbir
zuvbir

asked on

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

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.

Suggestions:

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
Const ADS_PROPERTY_APPEND = 3
Const ADS_UF_DONT_EXPIRE_PASSWD = &H10000
Const ADS_SCOPE_SUBTREE = 2

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)
        
        Else
  '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
      
      objUser.SetInfo
      objUser.GetInfo
      
            
      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:"

      
      objUser.SetInfo
      'MsgBox ObjGroupString

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

      
      '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
      
Wend


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

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

Next

'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  
    .Update
End With

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

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

End Function

tsInputFile.Close


Avatar of sirbounty
sirbounty
Flag of United States of America image

What account are you running it under?
Also - see if there are any clues in C:\Windows\SchedLog.txt
Avatar of zuvbir
zuvbir

ASKER

Running under my admin account.  Same account I am logged in  on when I run it manually.  There is no schedlog.txt on system.
Misspelled it...
c:\windows\SchedLgU.Txt
Avatar of zuvbir

ASKER

Not that one either.  Script does run at 11:00 pm as scheduled.
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America 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
SOLUTION
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