• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 589
  • Last Modified:

VBscript Set LDAP object doesn't exist - how to handle the error

Hello,
I have a list of usernames in a CSV file that I need to retrieve LDAP properties for.  I'm using the following code.  If the object doesn't exist, how can I handle the error to truncate the loop and move onto the next user in the CSV file?


Set objDataFile = objFSO.OpenTextFile(strInputFile,ForReading)
Do until objDataFile.AtEndofStream
	arrStr = Split(objDataFile.ReadLine,",")
	strSAMAcctName = arrStr(2)
 
	Set User = GetObject ("LDAP://CN=" & strSAMAcctName & StaffOU)
     
	        OutFile1.WriteLine User.sAMAccountName & "," & User.carLicense & User.displayName & "," & User.FirstName & "," & User.LastName
 
loop

Open in new window

0
jloomans
Asked:
jloomans
  • 2
  • 2
2 Solutions
 
sirbountyCommented:

Set objDataFile = objFSO.OpenTextFile(strInputFile,ForReading)
 
Do until objDataFile.AtEndofStream
        arrStr = Split(objDataFile.ReadLine,",")
        strSAMAcctName = arrStr(2)
        On Error Resume Next 'Added this line to skip error occurrence 
        Set User = GetObject ("LDAP://CN=" & strSAMAcctName & StaffOU)
        If Err.Number = 0 Then
          OutFile1.WriteLine User.sAMAccountName & "," & User.carLicense & User.displayName & "," & User.FirstName & "," & User.LastName
        Else 
          'Need to report on errors?
          'Use wscript.echo Err.Description if you want to see the error on screen.
        End If
       On Error Goto 0 'Reset error count
loop

Open in new window

0
 
David LeeCommented:
Hi, jloomans.

Use On Error Resume Next and test the Err.Number. Something like the code below.  The Err.Number is usually 0 if no error occured.
Set objDataFile = objFSO.OpenTextFile(strInputFile,ForReading)
On Error Resume Next
Do until objDataFile.AtEndofStream
    arrStr = Split(objDataFile.ReadLine,",")
    strSAMAcctName = arrStr(2)
    Set User = GetObject ("LDAP://CN=" & strSAMAcctName & StaffOU)
    If Err.Number <> 0 Then
        OutFile1.WriteLine User.sAMAccountName & "," & User.carLicense & User.displayName & "," & User.FirstName & "," & User.LastName
    End If
Loop

Open in new window

0
 
David LeeCommented:
LOL.  SB type faster than I do.
0
 
sirbountyCommented:
:^)
0
 
jloomansAuthor Commented:
Thanks! This should work.
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now