PowerShell MySQL Backup Script Error in Task Scheduler 0x00041301
Asked Answered
P

1

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?

Pertussis answered 1/8, 2016 at 7:35 Comment(5)
As a side note: $dateString = (Get-Date).ToString("yyyy-MM-dd") would be more readable.Affable
Thanks, you're right. I'll update it to that.Pertussis
Remove the 2>&1 from the string you assign to $backupFilePath and see if you still get illegal chars (> is an illegal path char).Oatcake
@KeithHill it is what this suggests I do.Pertussis
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.Oatcake
A
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.

Affable answered 1/8, 2016 at 7:50 Comment(6)
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?Pertussis
Nope, looks like it doesn't. I'll try creating the file first.Pertussis
Ok, that didn't help either. The commandoutput.txt was empty after I ran the task!Pertussis
@Ciwan Hm, how about using 2>&1 to send stderr to stdout? Get rid of the |out-null too, as it introduces unnecessary complexity.Affable
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?Pertussis
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.Pertussis

© 2022 - 2024 — McMap. All rights reserved.