PowerShell MySQL Backup Script Error in Task Scheduler 0x00041301

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?

mysql
powershell
windows-server-2012-r2
taskscheduler
asked on Stack Overflow Aug 1, 2016 by J86 • edited Oct 5, 2017 by halfer

1 Answer

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.

answered on Stack Overflow Aug 1, 2016 by vonPryz

User contributions licensed under CC BY-SA 3.0