1

I'm trying to run a batch file from a powershell script and check if there has been any error during the runtime.

Right now, I'm doing it by the following code:

$job = Start-Process -FilePath "C:\Test\Testfile_2.bat" -Wait -passthru;
$job.ExitCode

This works... or at least, nearly.

While I get the exitCode, it really only gives me information about the last command ran by the batchfile. Let me explain it.

If I do the following in my batchfile:

MKDIR C:\Test\Testdir

If I run this, I'll get exitCode "0" the first time I run it, for all the following runs I get "1", since the directory already exists. But if I add a simple CD to the end of the batchfile:

MKDIR C:\Test\Testdir
CD C:\

I will ALWAYS get exitCode "0", since the CD command works everytime. So even tho the directory exists and it would give me an error in the cmd, there's no error in the exitCode. Is there a way to check for any errors during the runtime of a batchfile with powershell, or do I have to code my batchfile the way that it cancels as soon as there's an error and immediatly returns an error exitCode?

Thanks for any help or advises!

Twinfriends
  • 1,972
  • 1
  • 14
  • 34
  • 2
    Why not write the whole thing in PowerShell? That way you will get better notification of errors, etc. – boxdog May 24 '18 at 09:47
  • simply because you are asking for the last exitcode which the batch file exited with. – Gerhard May 24 '18 at 09:49
  • The batchfile here is only an example. The batchfile I'm trying to run is a automation script with around 3'000 lines of code, that can't be replaced that easy by powershell. – Twinfriends May 24 '18 at 09:49
  • @GerhardBarnard Yes, I know that I'm asking for this, and I know why I get this "0". Thats why I am asking if there's a way to check for errors during the runtime, and not checking the exitCode. – Twinfriends May 24 '18 at 09:51
  • well, you would need to make changes to the batch script to be able to use exit codes from within the batch. – Gerhard May 24 '18 at 09:51
  • @GerhardBarnard Thank you for your answer. :) Just wanted to make sure that there's no other way. Thought that there's may another solution beside working with ExitCodes. – Twinfriends May 24 '18 at 09:56
  • well, something needs to determine what you want to do with the `errorlevels` :) – Gerhard May 24 '18 at 09:58
  • let me ask this rather.. Do you want to capture all of the errorlevels? If so, what do you plan on doing with them? – Gerhard May 24 '18 at 10:00
  • 2
    It will certainly be easier to rewrite a 3000 line batch script in PowerShell than it would be to adjust the exit codes returned by 3000 lines of batch script. – Ansgar Wiechers May 24 '18 at 10:05
  • @GerhardBarnard Thank you for your answer. Really appreciate it! Was having lunch the past few minutes, thats why I only answer now. I'll show your answer to my boss and ask him what he want me to do. :) – Twinfriends May 24 '18 at 10:37
  • Ahm... why not simply changing the order of commands, so doing `mkdir` after `cd`? that way the exit code (`ErrorLevel`) of `mkdir` is the one present at the end of the batch file... – aschipfl May 24 '18 at 12:07
  • @aschipfl While this would work for this script, its sadly only a sample batch file script... the script I'm actually trying to run with powershell has around 3000 lines of code :( – Twinfriends May 24 '18 at 13:28

1 Answers1

2

In order for an external program, such as your powershell command, to know what is happening while the batch script is being run, you need to actually script this into your batch file in order to determine what should happen during certain errors.

For instance, your powershell example simply launches the batch, waits for it to complete and echo the last errorlevel, which you batch exited with, though you might have numerous different errorlevels during the script being run.

if however your batch handles errors internally, like the below example, you could do something with it:

....
ping google.ca >nul
if %errorlevel% neq 0 (
exit /b %errorlevel%
) else (
....
Gerhard
  • 22,678
  • 7
  • 27
  • 43