0

I'm trying to set up an automated deploy script from a windows image. During testing, I'm running into an issue regarding the the part that tries to catch if something went wrong with certain commands.

Instead of stopping the script, the script keeps thinking that it's successful and just keeps going to the next command even though the previous command encountered an error.

I tried adding an else to the sections to see what went wrong and I noticed that the error level was at 0 after executing the command. Unless echo itself sets an error level. Here's the relevant section of the code.

Edit: I isolated the root cause to this code section. I also got rid of the imagex step to reduce complexity and variables.

@echo off
setlocal

set /p i_understand="Begin failure test? (Y/N) "

 if "%i_understand%"=="Y" (
 rem Deliberately causing diskpart to fail by using a nonexistent file.
 diskpart /s doesnotexist.txt
 if %ERRORLEVEL% NEQ 0 (
    echo Diskpart failed and caught.
    endlocal
    exit /b 1 )

 echo Diskpart failed but not caught. Error level is %ERRORLEVEL%
 ) else (
 echo Variable i_understand is %i_understand%
 echo Aborting. )

If I reduce the code to the one below, the error level is properly set:

@echo off
setlocal
rem Deliberately causing diskpart to fail by using a nonexistent file.
diskpart /s doesnotexist.txt
if %ERRORLEVEL% NEQ 0 (
    echo Diskpart failed and caught.
    endlocal
    exit /b 1 )

The idea was to display warnings then get a final answer with a capital "Y" for "Yes, I know this script is dangerous and I know what I'm doing." before proceeding with any other input being interpreted as a denial for safety.

Of course, many things could go wrong during the process. Maybe the network drive connection gets cut off, something causes the target drive to be inaccessible or something yet unforseen causes one of the commands to fail. In this case, the script stops whatever it's doing and leaves a log of the console output up to this point.

Naturally, during my tests, I encountered a bunch of failures due to misconfigured variables but the script kept chugging along as though nothing happened.

JosefZ
  • 12,837
  • 5
  • 37
  • 69
  • 1
    You haven't done any troubleshooting to break down your problem. Make it simpler by e.g. seeing if you can replicate the problem without references to an H drive. – barlop Oct 27 '18 at 07:00
  • Your batch file has two tests.. one with diskpart, one with imagex. We all have diskpart, we don't all have imagex. Why don't you delete the part with imagex, and just show the diskpart one if indeed that part is not working as expected. – barlop Oct 27 '18 at 07:07
  • Also basic tests can show you that echo doesn't have an influence on errorlevel, so why ask if it does when you can so easily test and see for yourself – barlop Oct 27 '18 at 07:10
  • see https://pastebin.com/raw/cswe0LqE re the first part of your batch file.. Results are as expected for me. Does this not happen for you? And I get the same (expected) result with a batch file too – barlop Oct 27 '18 at 07:11
  • Thanks for the tips. I feel stupid. Trying the code bits by itself seems to be working. Something that occurred earlier in the full script may be responsible for this. I'll come back with an update. – Paulo Francisco Santos Oct 27 '18 at 07:15
  • It's normal to run into a problem like whatever it is you ran into, and when you find the problem it might look like it was something stupid/obvious when you know, but when you don't know then it's not stupid/obvious .. And so in reality, since one doesn't know when , it's not stupid/obvious. All problems are like that. And will turn out to be something small and flddly somewhere, something overlooked. But the key is in asking the right question that leads you to the answer. And the key to that is troubleshooting properly by reproducing (or trying to reproduce) the problem in a simpler way. – barlop Oct 27 '18 at 07:40
  • Added an update. It's kinda big so I edited my original question a bit. – Paulo Francisco Santos Oct 27 '18 at 08:09
  • I haven't run diskpart with a script before, but just to be clear, the diskpart line will run and then the next line will run, therefore,If there is an error in the script fed to diskpart, and the error doesn't cause diskpart to exit, then diskpart will continue running the script. If there is no error in the script fed to diskpart then diskpart will continue running the script. What you can do is write some code after the diskpart line that determines whether it worked successfully – barlop Oct 27 '18 at 08:14
  • or diskpart aside, for the case of your image line, e.g. for your imagex line,, code afterwords would perhaps be if the expected file is outputted and is of the expected size. – barlop Oct 27 '18 at 08:15
  • Interesting perspective for diskpart but that's not the issue I'm testing though it is something I should try later. The thing here is, I tried to force diskpart to fail by asking it to call a non-existent script file. By itself, it works fine: diskpart reports that it can't find the file and the script gets to the `exit /b` bit, correctly causing the script to exit prematurely. But if I now put it inside the part that now checks for user input, diskpart still exits because it's trying to find a nonexistent file but `%ERRORLEVEL%` is still 0 for some reason. – Paulo Francisco Santos Oct 27 '18 at 08:36
  • it has been a while since i messed with setlocal , but I notice that if you remove setlocal and endlocal then it seems to get the errorlevel right.. Also this line `echo Diskpart failed but not caught` looks a bit in the middle of nowhere. And whether it runs or not changes when removing the setlocal and endlocal – barlop Oct 27 '18 at 09:47

0 Answers0