'How to suppress output and check whether or not a command is successful?

I am trying to write a powershell script that tests if a MySQL login is successful by using $? to check if an error occurs.

I also want to suppress all output - successful or not successful - from the command.

These are the things I've tried:

mysql -u root --password=mypass -e "show databases"
If ( $? ) {
  echo "Hooray!"
} Else {
  echo "Boo!"
}

This works correctly but doesn't suppress any output.

mysql -u root --password=mypass -e "show databases" > $null

Works correctly still but does not suppress the errors if the password is wrong.

mysql -u root --password=mypass -e "show databases" 2> $null

This does not work correctly. In this example, it always prints "Boo!"

mysql -u root --password=mypass -e "show databases" > $null 2>&1

This suppresses all output correctly but only ever prints "Boo!" like before.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source