简体   繁体   English

我想要powershell中的退出代码和stderr输出

[英]I want the exit code as well as stderr output in powershell

I have code such as this in my linux machine, which I want to port over for windows using powershell:我的linux机器中有这样的代码,我想使用powershell移植到windows上:

mysql -udbuser -pPassword -hlocalhost mydb < my_sql_file.sql >> mylog.log 2>&1
if [! $? ]
then
  echo "Mysql invocation returned a failure" >> mylog.log
  exit 1
fi

In my powershell script, I have something like:在我的 powershell 脚本中,我有类似的东西:

get-content "my_sql_file.sql" | mysql -udbuser -pPassword -hlocalhost mydb 3>&1 2>&1 >> mylog.log 
if (!$($?)) {
  write-output "Mysql invocation returned a failure" >> mylog.log
  exit 1
}

What I expect: In case of SQL runtime error the stderr message should be captured in the logs The mysql return code in case of success should pass by the if condition and execution should continue我的期望:如果出现 SQL 运行时错误,则应在日志中捕获 stderr 消息成功时的 mysql 返回代码应通过 if 条件并继续执行

What is happening: In case of mysql success it is still entering into the if发生了什么:如果 mysql 成功,它仍在进入 if

I have tried the below but encountering same or other problems:我尝试了以下但遇到相同或其他问题:

  1. use |使用 | out-file mylog.log输出文件 mylog.log
  2. use |使用 | add-content mylog.log添加内容 mylog.log
  3. create a logger class with a log method, then use |使用 log 方法创建一个记录器类,然后使用 | % ${$Logger.log($_)} % ${$Logger.log($_)}

Regardless of what I do, I think I am ending up losing the exit code of mysql Please show me how to write the code so that I can capture the mysql error dump as well as its return code so that my code knows when to abort its operation and use the error dump to report to user不管我做什么,我想我最终会丢失 mysql 的退出代码 请告诉我如何编写代码,以便我可以捕获 mysql 错误转储及其返回代码,以便我的代码知道何时中止它操作并使用错误转储报告给用户

  • For simplicity, use *>> to redirect all streams.为简单起见,使用*>>重定向所有流。

  • For robustness, check the automatic $LASTEXITCODE variable for the exit code of the most recently executed external program;为了稳健性,检查自动$LASTEXITCODE变量以获取最近执行的外部程序的退出代码; the abstract, Boolean automatic $?抽象的布尔自动$? variable can yield false negatives in PowerShell versions up to v7.1变量可以在 PowerShell 版本(最高 v7.1)中产生误报

Get-Content "my_sql_file.sql" |
  mysql -udbuser -pPassword -hlocalhost mydb *>> mylog.log

if ($LASTEXITCODE -ne 0) {
  "Mysql invocation returned a failure" >> mylog.log
  exit 1
}

Character-encoding notes :字符编码说明

  • In PowerShell, > is an effective alias of the Out-File cmdlet (and >> effectively calls Out-File -Append ), whose default output character encoding in Windows PowerShell is "Unicode" (UTF-16LE) and in PowerShell (Core) 7+ BOM-less UTF8.在 PowerShell 中, >Out-File cmdlet 的有效别名(并且>>有效地调用Out-File -Append ),其在Windows PowerShell中的默认输出字符编码为“Unicode”(UTF-16LE) ,而在 PowerShell(核心)中7+ 无 BOM 的 UTF8。 To control the character encoding, call Out-File or, for text input, Set-Content / Add-Content with the -Encoding parameter .要控制字符编码,请调用Out-File ,或者对于文本输入,使用-Encoding参数调用Set-Content / Add-Content Note that you may also have to ensure that an external program's output is first properly decoded , which happens based on the encoding stored in [Console]::OutputEncoding .请注意,您可能还必须确保首先正确解码外部程序的输出,这取决于存储在[Console]::OutputEncoding中的编码。 Note that you can't avoid these decoding and re-encoding steps as of PowerShell v7.2.4.请注意,从 PowerShell v7.2.4 开始,您无法避免这些解码和重新编码步骤。

  • In Windows PowerShell, -Encoding utf8 invariably creates a UTF-8 file with a BOM ;在 Windows PowerShell 中, -Encoding utf8总是创建一个带有BOM的 UTF-8 文件; to avoid that, you'll have to call a .NET API directly, eg, $output = some.exe ...; [System.IO.File]::WriteAllLines("$PWD/out.txt", $output)为避免这种情况,您必须直接调用 .NET API,例如$output = some.exe ...; [System.IO.File]::WriteAllLines("$PWD/out.txt", $output) $output = some.exe ...; [System.IO.File]::WriteAllLines("$PWD/out.txt", $output)

For instance, use the following variant of the commands above to create an ASCII file:例如,使用上述命令的以下变体来创建 ASCII 文件:

Get-Content "my_sql_file.sql" |
  mysql -udbuser -pPassword -hlocalhost mydb *>&1 |
    Add-Content -Encoding Ascii mylog.log

if ($LASTEXITCODE -ne 0) {
  "Mysql invocation returned a failure" |
    Add-Content -Encoding Ascii mylog.log
  exit 1
}

Note: Unlike Out-File -Append , Add-Content , when adding to an existing file, tries to match that file's existing encoding.注意:与Out-File -Append不同, Add-Content在添加到现有文件时会尝试匹配该文件的现有编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM