0

わかりました、ループを実行しようとして問題が発生しました。サーバーのリストを含む変数があり、ループしようとすると、ループが 1 回だけ停止します。

以下にコードの一部を示します。これは、私が何をしているのかを理解してもらうためのものですが、私が働いている会社を示すデータはすべて削除しています。

コードの最初の部分で、ドメインからサーバーのリストを取得します

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 を実行して現在処理している行を出力しようとしていますが、これを実行するたびに、使用されている sOutPut の最初の行のみが表示され、他の行がループされていないため、基本的にデバッグの出力のみが表示されます。 write("Line is :" & line) は

Line is : SERVERxafe01

そのため、他のサーバーを介してループすることはありません>

私は自分の仕事をもう少し生産的にしようとコードを書いているだけですが、イライラしやすく、コードに集中しているときに電話をかけると少しエッジが効くので、自分自身をプログラマーだとは考えていません

どんなアイデアでも大歓迎です、ありがとう。

4

2 に答える 2

0

より多くの情報を見つけようとした後、ループを終了するためのさまざまな手法を示す投稿を見つけました。

Return sOutput

モジュールを終了するため、ループが停止していました

変更により、これは現在機能しています。必要なデータを取得した場合にのみ return を実行するように、return ステートメントを if ステートメントに入れる必要がありました。

  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
于 2016-02-07T00:53:59.397 に答える
0

交換してみる

    while true
    '
    '
    '
    '
    '
    End While

    Do
    '
    '
    '
    '
    '
    Loop While line <> ""
于 2016-02-06T02:54:47.143 に答える