views:

3294

answers:

3

In vb.net 2008 I want to execute a batch file that resides on another computer. There is no error, 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()
+1  A: 

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

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

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

If you are going to use RedirectStandardOutput, you really do want to use RedirectStandardError, and then also subscribe to the events of the Process class for catching data on those streams (OutputDataReceived and ErrorDataReceived). Otherwise you will have no way to debug your batch script.

Neil
i proved it Neil but it still runs .exe file locally and not in remote mode like i want. any other advice?
A: 

This reads like a permissions problem. I would troubleshoot it that way if you haven't ruled it out yet.
Have you tried running the same batch file from the local computer?
If it is a permissions issue you can solve it either copying the file locally before executing it or map a drive to the remote computer where the file is and then execute the batch file from the new path.

Additionally, we don't really know what's in the batch file that could be causing an issue. I would either post the batch file or if you can't post the batch file use a batch file that you can post. Example a batch file would write the current date time to a file.

+1  A: 

To run a process on a remote computer you can use Sysinternals free psexec.

You can call it with the proper parameters and having the required permissions like you are doing in your sample code.

jvanderh
thank you. i used psexec and it runs fine now.