VB.net Looped ReadLine 在一行后停止

VB.net Looped ReadLine stops after one line

好的,我 运行 在尝试循环时遇到问题 我有一个包含服务器列表的变量,当我尝试循环时它只循环一次并停止

我在下面提供了部分代码,试图让您了解我在做什么,尽管我已经删除了所有可以指向我工作的公司的数据。

代码的第一部分,从域中获取服务器列表

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()

sOutPut 的输出如下所示(通过 debug.write("servers: " & sOutPut)

在下面的代码中显示
"SERVERxafe01"
"SERVERxafe02"
"SERVERxafe03"
"SERVERxafe04"
"SERVERxafe05"
"SERVERxafe06"

然后我试图让输出为每个服务器循环一个命令

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

在代码中,我试图通过 debug.write 输出当前正在处理的行行被循环,所以基本上只有 debug.write("Line is :" & line) 的输出是

Line is : SERVERxafe01

所以我从来没有让它通过其他服务器循环>

我只是编写代码来尝试提高我的工作效率,但是我不认为自己是一名程序员,因为我很容易感到沮丧并且在接听电话时专注于代码会让人有点急躁

所以欢迎任何想法,谢谢。

尝试替换

    while true
    '
    '
    '
    '
    '
    End While

    Do
    '
    '
    '
    '
    '
    Loop While line <> ""

在尝试查找更多信息后,我发现 post 显示了退出循环的不同技术,这不是我要找的,但在底部使用语句中发现了一些引起我注意的东西

Return sOutput

它退出模块所以它正在停止循环

所以经过修改,现在可以正常工作了,我不得不将 return 政治家放在 if 语句中,以便它仅在获得所需数据时运行 return

  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