The PowerShell command should return with an exit code >0 in case of an error. You can handle that like this:
set "DBSCRIPT=C:\Scripts\UpdateAppDB.ps1"
if exists %DBSCRIPT% (
powershell -Command %DBSCRIPT% || (
rem Error handling routines here
)
) else (
echo "Script not found." >> C:\TestResults\TestLog.txt
exit
)
or like this (requires delayed expansion enabled):
setlocal EnableDelayedExpansion
set "DBSCRIPT=C:\Scripts\UpdateAppDB.ps1"
if exists %DBSCRIPT% (
powershell -Command %DBSCRIPT%
if !errorlevel! neq 0 (
rem Error handling routines here
)
) else (
echo "Script not found." >> C:\TestResults\TestLog.txt
exit
)
As a side note: since you want to run a PowerShell script I'd use powershell -File "%DBSCRIPT%" instead of powershell -Command "%DBSCRIPT%". The double quotes around the variable take care of potential spaces in the path.
Edit: To be clear, the above code only handles non-zero return codes from either the PowerShell executable or the PowerShell script. It does not (and cannot) replace error handling inside the PowerShell script. If you want the PowerShell script to terminate on all errors (and indicate the error status with a non-zero exit code) you'll need at least something like this in the PowerShell script:
$ErrorActionPreference = "Stop"
try {
# ...
# rest of your code here
# ...
} catch {
Write-Error $_
exit 1
}
if errorlevel 1 goto BAD. That might require you toCALLPOWERSHELL if it doesn't otherwise return.