Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a vbs script to make backups of MySQL database files using the MySQLDump command. This is working perfectly and creates a new folder for each day's backups (e.g. 20130110).

However, the script is also meant to tidy up by deleting folder older than x days.

When I run the script manually, it works perfectly, but when I setup the script in the Task Scheduler, and set it to "Run whether user is logged on or not" (using the Administrator user account), the task fails. I believe the issue is permissions, but as the folders are created by the task, they should be able to be deleted by the task.

BTW, the vbs script uses the FileSystemObject DeleteFolder command to do the delete...

Any advice would be appreciated!

EDIT It's definatelty the FileSystemObject DeleteFolder command that's the issue. I've tried setting the folder to have Everyone, full permissions, but still no joy...

share|improve this question
    
setting 'Run whether used logged on or not' and 'Run with highest privileges' should be all you require for the task to run. If the issue does lie with FSO Delete folder then why does it run manually but not as a scheduled task? If the command is the issue, and there's no workaround, would there be any benefit to re-writing the script in Powershell? –  kafka Jan 18 '13 at 13:43
    
'Run whether used logged on or not' and 'Run with highest privileges' are already set... as you say, might need to re-think... –  JezB Jan 18 '13 at 13:57
    
where does the folder live, ie is it local, or a UNC share / mapped network drive? –  kafka Jan 18 '13 at 14:10
    
It's local - C:\MySQL_Backups\20130112\ etc. –  JezB Jan 18 '13 at 14:16

2 Answers 2

What is the command-line of your task? What ist the "Last Run Result" produced by the task? Do any errors or warnings show up in the "History" tab? Do you have UAC enabled, but neglected to check the "Run with highest privileges" option?

Try adding some debugging code to your script to isolate the error. Personally I prefer logging to the eventlog to avoid filesystem permission issues.

Set sh = CreateObject("WScript.Shell")
sh.LogEvent 4, "marker 1"

' your code here

sh.LogEvent 4, "marker 2"

' more of your code here

sh.LogEvent 4, "marker 3"

' ...

This should give you a general idea of which section of your code is the problematic one. Once you located that, you can add more specific debugging code, e.g.:

' some code

On Error Resume Next
' first instruction here
If Err Then ReportError "Error in 1st instruction"
' second instruction here
If Err Then Report Error "Error in 2nd instruction"
' ...
On Error Goto 0

' more code

Sub ReportError(txt)
  CreateObject("WScript.Shell").LogEvent 1, txt & ": " & Err.Description _
    & " (0x" & Hex(Err.Number) & ")"
  WScript.Quit 1
End Sub
share|improve this answer
    
It's definitely a permissions issue - if I set the folder to allow Everyone full control, it works... –  JezB Jan 18 '13 at 12:19
    
Correction - I can delete files, but not the folders... –  JezB Jan 18 '13 at 12:28
    
Check the permissions of the parent folder (there's a script on my homepage that might be helpful). Also check the actual error message (see 2nd code sample in my answer). –  Ansgar Wiechers Jan 18 '13 at 14:31
up vote 0 down vote accepted

The answer is as follows:

When setting up the Action, rather than set the 'Program/script' to include the path (e.g. "C:\Scripts\Backup.vbs"), you need to set the 'Program/script' as "Backup.vbs" and the 'Start in' as "C:\Scripts\"

The script now works!

Thanks to http://www.blogfodder.co.uk/2012/4/20/win-2008-task-scheduler-with-return-code-1-0x1 for the suggestion...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.