1

我正在运行一个 bat 文件并将输出显示在控制台窗口中。现在,我故意让 bat 文件失败(bat 文件包含“STARET www.webaddress.com”)。错误捕获并且一切正常,但控制台窗口立即关闭。我想保持开放,但到目前为止,我搜索如何做到这一点还没有找到任何解决方案。

我目前拥有的代码:

Me.processStartInfo = New ProcessStartInfo("C:\Admin\PsTools\psexec.exe", "\\PCName -u domain\" & user & " -p " & pass & " pathtobatfile")
Me.processStartInfo.RedirectStandardError = True
Me.processStartInfo.RedirectStandardOutput = True
Me.processStartInfo.WindowStyle = ProcessWindowStyle.Hidden
Me.processStartInfo.UseShellExecute = False
Dim process As System.Diagnostics.Process = Nothing

'Start the process, which runs the bat file on the remote server
process = System.Diagnostics.Process.Start(Me.processStartInfo)
AddHandler process.OutputDataReceived, AddressOf OutputHandler
process.BeginOutputReadLine()
process.WaitForExit()

Dim errorresponse As DialogResult
   If process.HasExited And process.ExitCode <> 0 Then
      errorresponse = MessageBox.Show("There was an error. Exit code: " & process.ExitCode & _
                        ". Do you wish to view the log?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error)
        ElseIf process.HasExited And process.ExitCode = 0 Then
            MessageBox.Show("The update completed successfully.", "Success", MessageBoxButtons.OK)
        End If
        If errorresponse = DialogResult.Yes Then
          'To fill in later
        End If

和事件处理程序子:

Private Shared Sub OutputHandler(ByVal sendingProcess As Object, ByVal output As DataReceivedEventArgs)
    Console.WriteLine(output.Data)
End Sub

我也尝试过只进行同步输出(使用 StreamReader 和 Console.Writeline 和 Readline()),但这也不起作用。

4

2 回答 2

1

尝试在最后添加

set err=%ERRORLEVEL%
pause
exit %err%
于 2012-10-17T06:14:12.027 回答
0

尝试在文件pause末尾添加BAT。或者,如果您通过进程启动 bat 文件cmd,则可以添加/k参数。

cmd /k pathToBatFile
于 2012-10-17T00:41:21.013 回答