[英]Detecting error after a powershell statement is executed
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. 我使用PowerShell来运行
sqlplus
,我希望PowerShell能够检测脚本运行后是否有错误并执行某些操作而不是查看结果文件。
& '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? 通常在DOS中,当命令遇到错误时有%errorlevel%,我想知道PowerShell中是否有类似的东西?
Of course, I can read the log file myself but sometimes, thing got too routine and I may forget. 当然,我自己可以阅读日志文件,但有时候,事情变得过于常规,我可能会忘记。
My Test.sql: 我的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
result.txt捕获它但我也想要PowerShell来检测它
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? 在执行语句之后,powershell语句是否返回错误级别?
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.
errorlevel:术语“errorlevel”未被识别为cmdlet,函数,脚本文件或可操作程序的名称。 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? 在powershell中检查错误的正确方法是什么?
UPDATE UPDATE
I used this: 我用过这个:
if ($LASTEXITCODE -ne 0 )
{
write-host error;
}
else
{
write-host ok;
}
Since you are invoking an executable , you probably want to check for the $LASTEXITCODE
variable or the return value of sqlplus. 由于您正在调用可执行文件 ,因此您可能希望检查
$LASTEXITCODE
变量或sqlplus的返回值 。 In PowerShell each variable has a $
prefix. 在PowerShell中,每个变量都有一个
$
前缀。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.