vb.netstringloopsvariablesstringreader

VB.net Looped ReadLine stops after one line


Ok I have run into an issue trying to do a loop I have a variable that contains a list of servers and when i try to loop it only loops once and stops

I have part of my code below to try and give you an idea what i'm doing although I have removed any data that will point back to what company I work for.

First Part of code, gets a list of servers from the domain

Dim oStartInfo As New ProcessStartInfo("c:\windows\system32\dsquery.exe", "computer " & cnameodd & oservpath & " -o rdn")
        oStartInfo.UseShellExecute = False
        oStartInfo.RedirectStandardOutput = True
        oStartInfo.RedirectStandardError = True
        oStartInfo.CreateNoWindow = True
        oProcess.StartInfo = oStartInfo
        oProcess.Start()
        Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
            sOutput = oStreamReader.ReadToEnd()

Output of sOutPut looks like this (shown in the code below via debug.write("servers: " & sOutPut)

"SERVERxafe01"
"SERVERxafe02"
"SERVERxafe03"
"SERVERxafe04"
"SERVERxafe05"
"SERVERxafe06"

Then I am trying to get the output to loop a command for each server

If sOutput = "" Then
                    Debug.Write("No Servers Found")
                Else
                    Debug.Write("Servers: " & sOutput)
                    Dim reader As New StringReader(sOutput.Replace(Chr(34), ""))
                    While True
                        Dim line = reader.ReadLine()
                        Debug.Write("Line is" & line)
                        If line IsNot Nothing Then
                            Dim command As String = " user " & user & " /server:" & line
                            Dim pStartInfo As New ProcessStartInfo("c:\windows\sysnative\query.exe", command)
                            pStartInfo.UseShellExecute = False
                            pStartInfo.RedirectStandardOutput = True
                            pStartInfo.RedirectStandardError = True
                            pStartInfo.CreateNoWindow = True
                            pProcess.StartInfo = pStartInfo
                            pProcess.Start()
                            Using pStreamReader As System.IO.StreamReader = pProcess.StandardError
                                sOutput = pStreamReader.ReadToEnd()
                                Debug.Write(sOutput & "Error " & command)
                            End Using
                            Using pStreamReader As System.IO.StreamReader = pProcess.StandardOutput
                                sOutput = pStreamReader.ReadToEnd()
                                Debug.Write(sOutput & "Output" & command)
                                Return sOutput
                            End Using
                        End If
                    End While
                End If
            End Using

In the code I have it trying to output what line it is currently processing by doing debug.write, however anytime i run this i only see the first line of sOutPut being used and no other lines being looped, so basically only output of debug.write("Line is :" & line) is

Line is : SERVERxafe01

So I never get it looped through the other servers>

I just write code to try and make my work a little more productive however I don't consider myself a programmer as I get frustrated easily and taking calls while concentration on code makes be a little edgy

So any ideas are welcome, Thank you.


Solution

  • After trying to find more information I found a post that showed different techniques to exit a loop, not what i was looking for but found something that caught my attention in the bottom using statement I had

    Return sOutput
    

    which exits the module so it was stopping the loop

    so with modifications this is now working, i had to put the return statesmen in an if statement so that it only runs return if it gets the data that is needed

      Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
            sOutput = oStreamReader.ReadToEnd()
            Dim server = sOutput.Replace(Chr(34), "")
            For Each line As String In server.Split(vbCrLf)
                Dim command As String = " " & user & " /server:" & line.Replace(vbLf, "")
                Dim pStartInfo As New ProcessStartInfo("c:\windows\sysnative\quser.exe", command)
                pStartInfo.UseShellExecute = False
                pStartInfo.RedirectStandardOutput = True
                pStartInfo.RedirectStandardError = True
                pStartInfo.CreateNoWindow = True
                pProcess.StartInfo = pStartInfo
                pProcess.Start()
                Using pStreamReader As System.IO.StreamReader = pProcess.StandardError
                    sOutput = pStreamReader.ReadToEnd()
                    If sOutput.Contains("SESSIONNAME") = True Then
                        Return "Found on Citrix Server: " & line & vbCrLf & sOutput
                    End If
                End Using
                Using pStreamReader As System.IO.StreamReader = pProcess.StandardOutput
                    sOutput = pStreamReader.ReadToEnd()
                    If sOutput.Contains("SESSIONNAME") = True Then
                        Return "Found on Citrix Server: " & line & vbCrLf & sOutput
                    End If
                End Using
            Next
        End Using