Cataster
Cataster

Reputation: 3521

Ignore a line in batch file from powershell?

Suppose i have the following batch file, named openPS.bat:

@echo off

SET psFile="%1"

Powershell.exe -ExecutionPolicy ByPass -File %psFile% %2 %3 %4 %5

if %errorlevel% NEQ 0 GOTO :error
GOTO :end
:error
echo Exit Code: %ERRORLEVEL%
echo Failed!

EXIT /B %ErrorLevel%
:end
echo Exit Code: %ERRORLEVEL%
echo Success!

This batch file is used to run a powershell script in a CMD console:

C:> openPS.bat script.ps1

the script.ps1 runs multiple times by a wrapper, so everytime it runs successfully, it outputs exit code 0

exit code 0

Can i somehow ignore this line in openPS.bat via powershell?

EXIT /B %ErrorLevel%
:end
echo Exit Code: %ERRORLEVEL%
echo Success!

i know i can REM it but i need to output one success message after all loops of the script.ps1 are completed. so it has to remain and I found no way to suppress all those exit code 0's. if i use out-null in powershell script, i lose all other output, so out-null is not an option.

Upvotes: 0

Views: 391

Answers (1)

user10316640
user10316640

Reputation:

Instead of checking and showing "Success!" for each iteration, use the wrapper script to indicate completion. The screen will show only the failures and completion message.

You can also use a variable in the wrapper script to keep track of the return values, a simple sum will work, and test it for 0 when execution of all of the iterations is complete. Then display success or failure.

@echo off

SET psFile="%1"

Powershell.exe -ExecutionPolicy ByPass -File %psFile% %2 %3 %4 %5

if %errorlevel% NEQ 0 GOTO :error
GOTO :end
:error
echo Exit Code: %errorlevel%
echo Failed!

:end
EXIT /B %errorlevel%

Upvotes: 1

Related Questions