title: VB piping STDOUT from cmd.exe output to to write to textbox
body: <p>I'm trying to adapt a VBscript that runs the QWINSTA command against a text file I've defined as an array and displays the results in a text file.</p>
  
  <p>after looking at numerous examples on Stack and other sites this is my latest iteration, that displays everything except the STDOUT, which is really what I'm after, not sure if it's an issue with how I'm calling the command piping the output or what. I'm also passing two variables as arguments to complicate matters :)</p>
  
  <p>The Text file that stdout is piped to is empty after the FOR Loop completes
  I may be going the long way about this, most of my past experience with this is in BATCH</p>
  
  <pre><code>For Each server In RemotePC
  
          'The Query is launched
          Dim Query As New ProcessStartInfo
  
          Query.WorkingDirectory = "c:\"
          Query.Arguments = server &amp; " " &amp; profile &amp; " &gt;C:\Windows\Temp\SFOUT.txt"
          Query.FileName = "QWINSTA"
          Query.WindowStyle = ProcessWindowStyle.Hidden
          Process.Start(Query)
  
          'Call Shell("QWINSTA /SERVER:" &amp; server &amp; " " &amp; profile &amp; " &gt;C:\Windows\Temp\SFOUT.txt", vbHidden, Timeout:=5000)
  
  
          Dim SFOUT As New IO.StreamReader("C:\windows\temp\SFOUT.txt")
  
          'Results are Echoed to TextboxResults
          TextBoxResults.Text &amp;= "__________________________________________________________" &amp; ControlChars.CrLf
          TextBoxResults.Text &amp;= server &amp; ControlChars.CrLf
          TextBoxResults.Text &amp;= SFOUT.ReadToEnd &amp; ControlChars.CrLf
          SFOUT.Close()
  
      Next
  </code></pre>
  
  <p>Here is what the working code in VBscript looks like</p>
  
  <pre><code>For Each server In RemotePC
  
  
  Dim WshShell, oExec
  Set WshShell = CreateObject("WScript.Shell")
  Set StrDir = "QWINSTA /SERVER:" &amp; server &amp; " " &amp; profile
  
  
  'The Query is launched
  Set oExec = WshShell.Exec(StrDir)
  
  
  'We wait for the end of process
  Do While oExec.Status = 0
  WScript.Sleep 500
  Loop
  
  'We scan and only display the command output of commands without NULL output
  Do While oExec.StdOut.AtEndOfStream &lt;&gt; True
        Results.WriteLine("__________________________________________________________")
          Results.WriteLine(server)
          Results.WriteLine oExec.StdOut.ReadLine
  Loop
  
  
  
  NEXT
  </code></pre>
  
  <p>Any help is appreciated</p>
  
  <p>@Nilpo</p>
  
  <p>this is what I get back</p>
  
  <pre><code>SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE 
  </code></pre>
  
  <p>console           Matt                      1  Active                      </p>
  
  <p>I've built something similar to this using QWINSTA and piping the output in batch and it works flawlessly, just having issues adapting this.</p>
  
  <hr>
  
  <p>here's the last thing I tried I tried to simplify things by calling something as basic as notepad.exe, and even trying to define the environment variable thinking it's not reading %PATH%, in reply to @Nilpo</p>
  
  <pre><code>Dim Query As New Process
  
          Query.StartInfo.FileName = "notepad.exe"
          'Query.StartInfo.Arguments = server &amp; " " &amp; profile &amp; " &gt;C:\Windows\Temp\SFOUT.txt"
          Query.StartInfo.UseShellExecute = False
          Query.StartInfo.RedirectStandardOutput = True
          Query.StartInfo.WindowStyle = ProcessWindowStyle.Normal
  
          Environment.SetEnvironmentVariable("PATH", "%PATH%;" &amp; "C:\Windows\System32\notepad.exe")
  
          Query.Start()
  </code></pre>