How can I use VB.NET to execute a batch file on another computer?

In vb.net 2008, I want to execute a batch file that is on another computer. There are no errors, but nothing happens. Here is the code:

Dim pStart As New System.Diagnostics.Process
Dim startInfo As New System.Diagnostics.ProcessStartInfo(serverpath & "\file.bat")
startInfo.RedirectStandardOutput = True
startInfo.WindowStyle = ProcessWindowStyle.Hidden
startInfo.UseShellExecute = False
pStart = System.Diagnostics.Process.Start(startInfo)
pStart.WaitForExit()
pStart.Close()

      

0


a source to share


3 answers


To start a process on a remote computer, you can use Sysinternals for free psexec .



You can call it with the appropriate parameters and have the required permissions as you do in your example code.

+1


a source


I've never tried to create a process using a batch file as an executable. I've always had to use cmd.exe as a program. This has worked for me in the past:

Dim startInfo As New System.Diagnostics.ProcessStartInfo("cmd.exe", "/c " & serverpath & "\file.bat")

      



"/ c" as part of the argument list tells cmd.exe to exit after the batch file has finished.

If you are going to use RedirectStandardOutput, you really want to use RedirectStandardError and then also subscribe to the events of the Process class to search for data over those streams (OutputDataReceived and ErrorDataReceived). Otherwise, you won't be able to debug the batch script.

+1


a source


This reads like a permissions issue. I would eliminate it this way if you haven't already eliminated it.
Have you tried to run the same batch file from your local computer?
If it's a permissions issue, you can either copy the file locally before executing it, or map the drive on the remote computer where the file is located and then execute the batch file from the new path.

Also, we don't know which of the batch file might be causing the problem. I would either publish the batch file, or if you cannot publish the batch file, use a batch file that you can publish. The sample batch file writes the current time to a file.

0


a source







All Articles