PowerShell MySQL Backup Script Error in Task Scheduler 0x00041301

浪尽此生 提交于 2020-01-17 06:18:50

问题


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:


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.



来源:https://stackoverflow.com/questions/38693278/powershell-mysql-backup-script-error-in-task-scheduler-0x00041301

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!