VB Script hangs when called via Scheduled Task

cadcollin
cadcollin used Ask the Experts™
on
I've pieced together a script -- parts are borrowed, parts are original -- that traverses through the sub-directories of a defined directory and deletes temporary files older than 90 days.  It's also supposed to log every file that's deleted.

The script works as intended when run from Windows Server 2008 (x86) SP2 using wscript.exe & cscript.exe.  When run, the process' CPU utilization hovers around 15-20% and logs every file that's deleted.  However, when set to run as a Scheduled Task, the wscript.exe process starts, but nothing ever happens.  The wscript.exe process never jumps over 0% and the script never deletes or logs any files.

The scheduled task is set to run whether a user is logged on or not.  It is also set to run as a user with administrative privileges.

After encountering this, I wrote a batch script to invoke the VB script and had the some results: when run manually, it worked; when run as a scheduled task, it did not.  (Interestingly, when the batch script is run as a scheduled task, the cscript.exe process immediately started and stopped and the task history shows the jobs ran successfully, even though nothing was deleted or logged.)

Here's the code:
Option Explicit
 
Const strSrcPath = "D:\Desktop Backup\"
Const intMaxDate = 90
 
Dim objFSO, deleteLog
Set objFSO = CreateObject("Scripting.FileSystemObject")

set deleteLog = objFSO.OpenTextFile("C:\sync\logs\deletedfiles.txt",8,True)


TraverseFolder strSrcPath, intMaxDate
deleteLog.Close

 
Sub TraverseFolder(strSrcPath, intMaxDate)
	Dim objCurrentFolder
	Set objCurrentFolder = objFSO.GetFolder(strSrcPath)
 
	On Error Resume Next
	if (InStr(objCurrentFolder,"Local Settings\Temporary Internet Files") OR InStr(objCurrentFolder,"Local Settings\Temp\")) then
		Dim objFile, objFolder
		For Each objFile In objCurrentFolder.Files
			If DateDiff("d", CDate(objFile.DateLastModified), Now) > intMaxDate Then
				deleteLog.WriteLine objFile & "|" & objFile.Size & " bytes" & "|" & objFile.DateLastModified & "|" & Now
				objFSO.DeleteFile objFile
			End If
		Next
	end if
 
	For Each objFolder In objCurrentFolder.subFolders
		TraverseFolder objFolder, intMaxDate
	Next
End Sub

Open in new window


Is there a restriction against using VB file system objects or recursion when calling VB scripts from a scheduled task?  (That wouldn't really make a lot of sense to me, but it's the best explanation I've been able to come up with!)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Try allowing the service to interact with the desktop.

Author

Commented:
@exx1976:
Could you please elaborate?  Which service?  Task Scheduler itself, or are you referring to a setting within the specific task?

In either case, I'm not seeing any settings with verbiage similar to "allow to interact with desktop"; Google's not helping me out much either!

Commented:
My apologies.  The service <account>.  Whatever account you have configured the task to run under, it needs to be able to interact.

Alternatively, try uncommenting the "on error resume next" and see what you get when it is run as a task.

Author

Commented:
Resolved!

This was an issue with the Security options on the scheduled task.  The task had to be set to Run with highest privileges, despite the fact that it was running as a user account with local and domain administrative rights.  (Shouldn't the fact that it was being run with administrative  privileges imply that?)
I now see the setting under exx1976 was referring to.  On the Log On tab of the Task Scheduler service, there's an option to "Allow service to interact with desktop".  It's currently unchecked.

While checking this box may resolve the issue, the option to do so is unavailable.  It is grayed out.  I may be able to change this setting using the Command Line or by booting into Safe Mode, but doing so seems excessive when the problem can be corrected by simply checking one box on the task (as described in comment 33116339).

I can confirm that my solution worked.  I can neither confirm nor deny that exx1976's solution works.
Capture.PNG

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial