PowerShell MySQL Backup Script Error in Task Sched

2019-09-09 06:21发布

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?

1条回答
啃猪蹄的小仙女
2楼-- · 2019-09-09 07:19

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.

查看更多
登录 后发表回答