powershellmysql-cli

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:

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:

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

What is happening: In case of mysql success it is still entering into the if

I have tried the below but encountering same or other problems:

  1. use | out-file mylog.log
  2. use | add-content mylog.log
  3. create a logger class with a log method, then use | % ${$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


Solution

  • 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:

    For instance, use the following variant of the commands above to create an ASCII file:

    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.