Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with my Do While Loop 1

Status
Not open for further replies.

jmb2000

Technical User
Feb 11, 2004
11
AU
Hi guys, I have a script to search AD and return usernames that are found if they are not in a specific OU. I have managed to get it to take input from an excel document but am having problems with it looping back to the top in order to run the search on next entry in worksheet. Is there a limit on the amount of information you can put in a loop statement? If so what command should I use instead? I have tried a goto command but again it just causes errors (I dont pretend to know what I'm doing by the way :) ) any light that could be shed would be great.

Thanks

John
 
The script is about 200 lines long do you want me to post the whole thing?
 
The skeleton so we can have a look at the logic, and where you havez problem.
 
This is the Do While to Loop part (which is most of the script)

Do While objSheet.Cells(intRow, 1).Value <> &quot;&quot;
strSearch = objSheet.Cells(intRow, 3).Value

'Get the local Computer Name
strServerName = WScript.CreateObject(&quot;WScript.Network&quot;).ComputerName

'Get the Default Domain Naming Context
strDefaultDomainNC = GetObject(&quot;LDAP://RootDSE&quot;).Get(&quot;DefaultNamingContext&quot;)

If (IsEmpty(strDefaultDomainNC)) Then
' Wscript.Echo(&quot;&quot;)
Wscript.Echo(&quot;Error: Did not get the Default Naming Context&quot;)
Call Cleanup(2)
End If

'Set up the ADO connection required to implement the search.
Set objADOConn = CreateObject(&quot;ADODB.Connection&quot;)

objADOConn.Provider = &quot;ADsDSOObject&quot;
'Connect using current user credentials
objADOConn.Open &quot;Active Directory Provider&quot;

Set objADOCommand = CreateObject(&quot;ADODB.Command&quot;)
Set objADOCommand.ActiveConnection = objADOConn

'Format search criteria using SQL syntax
strADSQuery = &quot;SELECT samAccountName, givenName, sn, AdsPath FROM 'LDAP:// &quot; & _
strDefaultDomainNC & &quot;' WHERE samAccountName = '&quot; & strSearch & &quot;'&quot;

objADOCommand.CommandText = strADSQuery

'Execute the search
Set objQueryResultSet = objADOCommand.Execute

If (objQueryResultSet.EOF) Then
' Wscript.Echo(&quot;&quot;)
objTextFile.WriteLine (&quot;User &quot; & strSearch & &quot; was not found&quot;)
objTextFile.WriteBlankLines(1)
Call Cleanup(4)
End If

intCount = 0
While Not objQueryResultSet.EOF
strAdsPath = objQueryResultSet.Fields(&quot;AdsPath&quot;)
intCount = intCount + 1
objQueryResultSet.MoveNext
Wend

objADOConn.Close

' Search for object
Set objConnection = CreateObject(&quot;ADODB.Connection&quot;)
objConnection.Open &quot;Provider=ADsDSOObject;&quot;

Set objCommand = CreateObject(&quot;ADODB.Command&quot;)
objCommand.ActiveConnection = objConnection

' Search string
objCommand.CommandText = _
&quot;<LDAP://OU=Staging,dc=test,dc=local>;(objectCategory=user)&quot; & _
&quot;;distinguishedName,name;subtree&quot;

Set objRecordSet = objCommand.Execute

' If no errors search for user
If Err.Number = 0 then

While Not objRecordSet.EOF
if objRecordSet.Fields(&quot;Name&quot;) = strSearch then
boolResult = True
End if
objRecordSet.MoveNext
Wend
End if

objConnection.Close

'If a single user was returned, demonstrate binding to the user object
If intCount = 1 Then
Set objUser = GetObject(strAdsPath)

If boolResult = false Then
objTextFile.WriteLine (&quot;sAMAccountName: &quot; & objUser.Get(&quot;sAMAccountName&quot;))
objTextFile.WriteLine (&quot;The AdsPath is: &quot; & objQueryResultSet.Fields(&quot;AdsPath&quot;))
objTextFile.WriteLine Now
objTextFile.WriteBlankLines(1)
Else
objTextFile.WriteLine (&quot;Non Anomalous user&quot;)
End If
End If

Set objUser = Nothing
intRow = intRow + 1

Loop

Be gentle :) but thanks for replies already mate
 
And what is the problem (Symptom, expected behaviour, ...) ?
Anyway, what is the initial value of intRow ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Sorry didnt explain it well to start with, intRow is set to 2 the line before the Do While command as this is where the first line of usernames start, and the problem is when run and the username is not found the script stops (at the moment because I have no Goto or call command in the if statement) I have tried creating a line :-
Call ReLoop
then put
Sub ReLoop
just before the Loop command itself and have tried a
GoTo ReLoop
then
ReLoop:

but neither will skip down to start the cycle again it just causes errors
 
PS dont mean to be cheeky but can you see a reason why the ADSpath is not returned? It did work originally but somewhere along the way it stopped returning it?
 
No GoTo instruction in VBScript.
Just use nested If Else. (Tip: indent your code)
What is supposed Cleanup(4) do ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
All I need to do is make the script skip from the line
objTextFile.WriteBlankLines(1)
down to just before the loop command so that if the user is not found it will still try the next entry

Cleanup(4) is just where I had it going to the end of the script if user wasn't found as this started as a single run script, but now I want to take that line out and put in the command to skip.
 
Same suggestion again: use Else after (or instead) Cleanup(4) and move the End If just before the inRow increment.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Excellent, sorry it took me a couple of goes to understand quite what you meant but just moved the end to the end and put in an else and works beautifully.

Cheers mate

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top