5
votes

I am using powershell to run sqlplus and I would like PowerShell to detect if there are error after the script was run and to perform some action instead of me looking at the result file.

& 'sqlplus' 'system/myOraclePassword' '@Test' | out-file 'result.txt';

Normally in DOS, there is %errorlevel% when the command encounters error and I wonder if there is similar stuff in PowerShell?

Of course, I can read the log file myself but sometimes, thing got too routine and I may forget.

My Test.sql:

select level from dual
connect by level<5;
select 10/0 from dual;
quit;

There is clearly a division by zero error. The result.txt captures it but I would like powershell to detect it as well

SQL*Plus: Release 12.1.0.2.0 Production on Thu Apr 27 16:24:30 2017

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

Last Successful login time: Thu Apr 27 2017 16:17:34 -04:00

Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options


     LEVEL
----------
     1
     2
     3
     4

select 10/0 from dual
         *
ERROR at line 1:
ORA-01476: divisor is equal to zero

Does the powershell statement return an errorlevel after the statement is executed like DOS?

I have tried:

& 'sqlplus' 'system/myOraclePassword' '@Test' | out-file 'result.txt';
if (errorlevel 1)
{ write-host error;
}
else
{ write-host ok;
}

But that has caused syntax error?

errorlevel : The term 'errorlevel' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

What is a proper way to check error in powershell?

UPDATE

I used this:

if ($LASTEXITCODE -ne 0 )
{ 
write-host error;
}
else
{ 
write-host ok;
}
1
Try replacing if (errorlevel 1) with if ($LASTEXITCODE -eq 1) - henrycarteruk
You will also need to add whenever sqlerror exit failure or whenever sqlerror exit 1 to the top of you SQL script, otherwise SQL*Plus will just exit normally when it gets to the quit and the shell won't know anything was wrong. But you can only really get success/failure, not the actual error (1476 here) - you can exit sql.sqlcode but most shells will wrap that at 256 which isn't helpful... - Alex Poole
@AlexPoole: Thank you, Alex. Got it!! No wonder - user1205746
@AlexPoole: My apologies for coming back to your comment adding whenever sqlerror exit failure to the top of sql script. At first I thought it would solve the issue, but after looking more closely, $LASTEXITCODE is still 0 which meant it either has not received the error flag from the sql or sql does not consider a division by zero a sqlerror. Could you shed some more light on that for me? - user1205746

1 Answers

4
votes

Since you are invoking an executable, you probably want to check for the $LASTEXITCODE variable or the return value of sqlplus. In PowerShell each variable has a $ prefix.