0

I have created the following PowerShell script.

$root = 'C:\Backups\My Website\Database Dumps\'

$dateString = (Get-Date).ToString("yyyy-MM-dd")

$fileName = $dateString + "-MyWebsiteDbBackup.sql"

$backupFilePath = ($root + $fileName)

$command = ("mysqldump -u root wpdatabase > " + "`"$backupFilePath`"")

Write-Host $command

Invoke-Expression $command

Its function is supposed to be making a daily backup of a MySQL database for my WordPress website.

When I run the script in PowerShell ISE, it runs fine and the MySQL dump file is created with no problems.

However, in Task Scheduler, it was stuck on running with a code 0x00041301.

For the credentials, I am using the my.cnf technique described here. And I've set the task to run whether a user is logged on or not.

CODE UPDATE

Based on vonPryz's answer.

$root = 'C:\Backups\My Website\Database Dumps\'

$dateString = (Get-Date).ToString("yyyy-MM-dd")

$fileName = $dateString + "-MyWebsiteDbBackup.sql"

$backupFilePath = ($root + $fileName + " 2>&1")

$command = ("mysqldump -u root wpdatabase > " + "`"$backupFilePath`"")

Write-Host $command

$output = Invoke-Expression $command 

$output | Out-File C:\mysqlBackupScriptOutput.txt

This now give me an error saying illegal character in path

What am I doing wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
J86
  • 14,345
  • 47
  • 130
  • 228
  • 1
    As a side note: `$dateString = (Get-Date).ToString("yyyy-MM-dd")` would be more readable. – vonPryz Aug 01 '16 at 07:43
  • Thanks, you're right. I'll update it to that. – J86 Aug 01 '16 at 07:44
  • Remove the `2>&1` from the string you assign to $backupFilePath and see if you still get illegal chars (> is an illegal path char). – Keith Hill Aug 02 '16 at 15:24
  • @KeithHill it is what [this suggests](http://stackoverflow.com/questions/12320275/how-to-pipe-output-of-invoke-expression-to-string/12320512#12320512) I do. – J86 Aug 03 '16 at 08:20
  • 1
    You are doing a bit more here - specifically you put `$backupFilePath` in quotes which makes the whole string (including 2>&1) appear as part of the filename. Well, that's my theory anyway. – Keith Hill Aug 04 '16 at 17:39

1 Answers1

0

Task Scheduler's code 0x00041301 means that the task is running. This is likely to mean that mysqldump is prompting for something. Maybe a password or some confirmation dialog. Which user account is the task being run on?

In order to debug, you'd need to capture the process' output to see what's going on. Try using Tee-Object to send a copy to a file.

vonPryz
  • 22,996
  • 7
  • 54
  • 65
  • Thanks Von, if I do this at the end `Invoke-Expression $command | Tee-Object -FilePath "C:\commandoutput.txt" | Out-Null` will it create that text file? Or do I need to have that in place first? – J86 Aug 01 '16 at 08:02
  • Nope, looks like it doesn't. I'll try creating the file first. – J86 Aug 01 '16 at 08:13
  • Ok, that didn't help either. The `commandoutput.txt` was empty after I ran the task! – J86 Aug 01 '16 at 08:19
  • @Ciwan Hm, how about using `2>&1` to [send stderr to stdout](http://stackoverflow.com/a/12320512)? Get rid of the `|out-null` too, as it introduces unnecessary complexity. – vonPryz Aug 01 '16 at 08:32
  • Once I've placed it in an `$output` variable, then what? How would I write that to a file? I think `Tee-Object` doesn't do that right? – J86 Aug 01 '16 at 08:56
  • I think I figured out how to output a variable to a file, but getting a different error with regards to `2>&1` see updated Q. – J86 Aug 01 '16 at 09:13