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.Connec tion")
Set objCommand = CreateObject("ADODB.Comman d")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnectio n = objConnection
objCommand.Properties("Pag e Size") = 1000
objCommand.Properties("Sea rchscope") = 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=use rs,OU=Ramo na,dc=doma inname,dc= org"
tsInputFileString = "Students_Ramona.txt"
HomePathString = "\\polk\studenthome$\"
ObjGroupString = "LDAP://cn=_USER_Ramona_St udent,ou=S tudents,ou =users,ou= Ramona,dc= domainname ,dc=org"
ObjGroupString2 = "ou=Students,ou=users,ou=R amona,dc=d omainname, dc=org"
ElseIf Site = "fran" Then
objOUString = "LDAP://OU=students,OU=use rs,OU=Fran k,dc=domai nname,dc=o rg"
tsInputFileString = "Students_Frank.txt"
HomePathString = "\\pierce\studenthome$\"
ObjGroupString = "LDAP://cn=_USER_Frank_Stu dent,ou=St udents,ou= users,ou=F rank,dc=do mainname,d c=org"
ObjGroupString2 = "ou=Students,ou=users,ou=F rank,dc=do mainname,d c=org"
End If
Set objOU = GetObject(objOUString)
Set fso = CreateObject("Scripting.Fi leSystemOb ject")
'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=o rg' 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("userAccountCo ntrol")
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(HomePathS tring & arrInput(0)) Then
Set fldUserHomedir = fso.CreateFolder(HomePathS tring & arrInput(0))
End If
' set rights
Set wshShell = WScript.CreateObject("Wscr ipt.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(Subj ect, TextBody)
Dim iMsg, iConf, Flds
set iMsg = CreateObject("CDO.Message" )
set iConf = CreateObject("CDO.Configur ation")
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
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.Connec
Set objCommand = CreateObject("ADODB.Comman
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnectio
objCommand.Properties("Pag
objCommand.Properties("Sea
SitesArray = Array( "ramo", "fran" )
For Each Site In SitesArray
MsgBox Site
CreateCount = 0
If Site = "ramo" Then
objOUString = "LDAP://OU=students,OU=use
tsInputFileString = "Students_Ramona.txt"
HomePathString = "\\polk\studenthome$\"
ObjGroupString = "LDAP://cn=_USER_Ramona_St
ObjGroupString2 = "ou=Students,ou=users,ou=R
ElseIf Site = "fran" Then
objOUString = "LDAP://OU=students,OU=use
tsInputFileString = "Students_Frank.txt"
HomePathString = "\\pierce\studenthome$\"
ObjGroupString = "LDAP://cn=_USER_Frank_Stu
ObjGroupString2 = "ou=Students,ou=users,ou=F
End If
Set objOU = GetObject(objOUString)
Set fso = CreateObject("Scripting.Fi
'get file if exists
Set tsInputFile = fso.OpenTextFile("C:\vbscr
' 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=o
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("userAccountCo
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(HomePathS
Set fldUserHomedir = fso.CreateFolder(HomePathS
End If
' set rights
Set wshShell = WScript.CreateObject("Wscr
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(Subj
Dim iMsg, iConf, Flds
set iMsg = CreateObject("CDO.Message"
set iConf = CreateObject("CDO.Configur
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
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
c:\windows\SchedLgU.Txt
ASKER
Not that one either. Script does run at 11:00 pm as scheduled.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Also - see if there are any clues in C:\Windows\SchedLog.txt