3

How do I capture error messages from a PowerShell-launched command in a text file?

I searched the Internet for a while and found that supposedly, I should be able to do something like ""C:\UniServer\usr\local\mysql\bin\mysqldump.exe" -h 192.0.2.0 -u"backup" -p"longComplicatedPassword" --all-databases >"C:\Users\Zian\MySQL Database Backups\20130118221525.sql" 2>"C:\users\zian\desktop\MySQL Backup Error.txt"" to direct the output to output.txt and the errors to errors.txt but when I try to run the command, I get the following error:

cmd.exe : The filename, directory name, or volume label syntax is incorrect.
At C:\Users\Zian\Desktop\Untitled1.ps1:27 char:4
+ cmd <<<<  /c $command 
    + CategoryInfo          : NotSpecified: (The filename, d...x is incorrect.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Furthermore, if I try to run the command without everything starting at "2", then the command executes correctly and output.txt catches the right output.

I looked at Redirect stderr to variable in powershell but it wasn't helpful because the answer to that question suggests capturing the entire output and filtering it in memory. In my case, I am backing up every database on a computer and since the databases won't fit in my laptop's RAM, I cannot use the question's solution.

I also found tantalizing suggestions about using $err = @(command goes here) but with no information on what to do other than simply inserting that line of text. I tried to utilize the search function on Serverfault with the string "@()", but it did not return any results.

What can I do to get the error messages into errors.txt?

Zian Choy
  • 281
  • 5
  • 18
  • It may help if you interpolate "big blob of text" for us so that we can test. Powershell doesn't need to know about stdout/stderr from the external command as they should be handled, well, *externally* and redirect their streams accordingly. For instance, in PS a simple `cmd /c "dir . >output.txt 2>errors.txt"` works as expected, producing an `output.txt` which contains the `dir` list, and a zero-byte `errors.txt`. – jscott Dec 01 '12 at 01:41
  • Similar to: http://stackoverflow.com/q/1215260/80161 – Nathan Hartley Oct 27 '16 at 19:16

1 Answers1

2

As jscott pointed out, the command works. I simply forgot to add in enough back-ticked quotation marks.

The solution then, is to make sure there are the quotation marks around the entire line of text after cmd /c.

Zian Choy
  • 281
  • 5
  • 18