Link to home
Start Free TrialLog in
Avatar of bbimis
bbimis

asked on

psexec output not captured in vb.net

is there not a way to capture the output of psexec as it runs? i can do cmd.exe /k ipconfig and it works fine but when i do the following code using psexec it only returns the command prompt in the richtextbox.

dim proc as new system.diagnostics.process
proc.StartInfo.FileName = "cmd.exe"
        proc.StartInfo.Arguments = "/k c:\shared\psexec.exe \\" & ip & " -u administrator -p somepassword c:\shared\printme.exe " & ticknumber
        proc.StartInfo.CreateNoWindow = True
        proc.StartInfo.UseShellExecute = False
        proc.StartInfo.RedirectStandardOutput = True
        proc.Start()

       Dim output() As String = proc.StandardOutput.ReadToEnd.Split(CChar(vbLf))
        For Each ln As String In output
            RichTextBox1.AppendText(ln & vbNewLine)

        Next
        proc.WaitForExit()

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bbimis
bbimis

ASKER

thanks for the information.
Avatar of bbimis

ASKER

Found this that actually works for psexec also.
Private Sub btnRun_Click(...) Handles btnRun.Click
    ' Set start information.
    Dim start_info As New ProcessStartInfo(txtProgram.Text)
    start_info.UseShellExecute = False
    start_info.CreateNoWindow = True
    start_info.RedirectStandardOutput = True
    start_info.RedirectStandardError = True

    ' Make the process and set its start information.
    Dim proc As New Process()
    proc.StartInfo = start_info

    ' Start the process.
    proc.Start()

    ' Attach to stdout and stderr.
    Dim std_out As StreamReader = proc.StandardOutput()
    Dim std_err As StreamReader = proc.StandardError()

    ' Display the results.
    txtStdout.Text = std_out.ReadToEnd()
    txtStderr.Text = std_err.ReadToEnd()

    ' Clean up.
    std_out.Close()
    std_err.Close()
    proc.Close()
End Sub

Open in new window