Solved

Running a VBScript from a schdeuled task

Posted on 2009-04-01
13
1,473 Views
Last Modified: 2012-05-06
We have a number of printers that scan to a public folder, and we used to have a scheduled task set up that ran a VBScript that emptied the folder of documents that were over 7 days old every sunday. I'll post the script below, the script runs fine when run by itself, but the problem we're having is that the script used to run with the credentials of the NT AUTHORITY/SYSTEM account, which now doesn't work, the task just sits there as if its running, then nothing. No output message. When we run the task with a domain account it runs fine. Any ideas? We'd prefer to run it under the system account.


NB i've also been told that we have this problem with running ANY VBScripts or batch files.
' Autor: Fernando Inácio
' Date: 02-03-2006
'
' Description : Deletes files of a certain date and time.
'        It also generates an output of the total number of deleted
 
'        files and creates a log file.
 
 
 
strDateTime = "16-02-2006"
strPath = "D:\G\scanner\UAT"
strDatediff= " "
intFileCount = 0
strComputer = "."
 
Dim logFile
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set logFile= objFSO.CreateTextFile(strPath & "\deleteFiles.log", True)
Set Folder = objFSO.GetFolder(strPath)
 
logFile.Write "Directory Name: " & strPath & VbCrLf
logFile.Write "Date: " & Now() & VbCrLf & VbCrLf
 
For Each file In Folder.Files
 
  if DateDiff("d", File.DateLastModified, Now) >= 7 Then
                
            intFileCount = intFileCount + 1
            logFile.Write "File: " & File.name & vbTab & vbTab & File.DateLastModified & VbCrLf
            logFile.Write CRLF
            objFSO.DeleteFile(strPath & "\" & file.name)
    End If
 
Next
 
If intFileCount = 0 Then
    MsgBox "No files to delete!"
Else
    MsgBox intFileCount & " files deleted!"
End If
 
logFile.Write CRLF
logFile.Write intFileCount & " File Deleted."
logFile.Close
Set lofFile = Nothing
Set objFSO = Nothing
Set Folder = Nothing

Open in new window

0
Comment
Question by:tlcsupport
  • 7
  • 6
13 Comments
 
LVL 27

Expert Comment

by:bluntTony
ID: 24038422
Are you using the /interactive switch when shceduling the task, i.e.:
at 12:00 /interactive myvbscript.vbs
Without that, I think the script would be waiting at the msgbox line waiting for user input. As it's not getting any, it's hanging...
0
 
LVL 1

Author Comment

by:tlcsupport
ID: 24039593
As far as I can see the scheduled tasks were setup using the utility not by using the AT command.  Is there a way of setting the interactive switch through the utility or editing the job via command line?
0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24039869
It' strange because I actually copied your script and scheduled using 'at' and it worked fine. Can you not try to create a new scheduled task using at?
Are the files being deleted before it hangs? The msgbox line is after the files are deleted. How about trying to manually run the script under SYSTEM. A way to test this is to create a service to open a CMD prompt in the SYSTEM context. Type:
sc create testsvc binpath= "cmd /K start" type= own type= interact
Then to start the service:
 sc start testsvc
You'll get an error message, but a command prompt will appear. This is running as SYSTEM so you can try to run the script via cscript from here. See if you get any error messages.
Another thing - anything running as SYSTEM will only give feedback to the console session - not a remote desktop session. So you need to run this service locally - you will also need to test your scheduled task on the server locally - not remotely.
Let us know how you get on...
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 1

Author Comment

by:tlcsupport
ID: 24077449
This is the output:

C:\Documents and Settings\ashadmin>sc create testsvc binpath= "cmd K start" type
= own type= interact
[SC] CreateService SUCCESS

C:\Documents and Settings\ashadmin>sc start testsvc
[SC] StartService FAILED 1053:

The service did not respond to the start or control request in a timely fashion.
0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24086783
This error message is normal (well it's not, but this method is a bit of a 'bodge' to get a SYSTEM command prompt up). You should get this error message, but a blank command prompt should appear. You can then run the script via cscript from within this window.
It will run under the SYSTEM context so you are basically emulating what happens when the task is scheduled to run under SYSTEM, and you will see any errors.
Make sure you do this locally, not from a remote desktop session - the command window will only appear on the local session.
0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24086808
And you missed the forward slash from the binpath. it should be 'binpath= "cmd /k start"'
That'll be why the command prompt didn't stay open. Without the /k switch, it'll close again straight away. Create a new service with the correct binpath and try again.
0
 
LVL 1

Author Comment

by:tlcsupport
ID: 24158306
Again, i got this error

C:\Documents and Settings\ashadmin>sc create testsvc2 binpath= "cmd /K start" ty
pe= own type= interact
[SC] CreateService SUCCESS

C:\Documents and Settings\ashadmin>sc start testsvc2
[SC] StartService FAILED 1053:

The service did not respond to the start or control request in a timely fashion.
0
 
LVL 1

Author Comment

by:tlcsupport
ID: 24158358
The only problem is, we can only remote to the servers.


But if this works, then how will that affect us getting our scheduled tasks to run used the NT Authority / System account?
0
 
LVL 1

Author Comment

by:tlcsupport
ID: 24158490
I just tried running the AT command (as below)

And when the job goes to run it just states "Could Not Start"
at \\SERVERNAME 00:10 /interactive /every:F Delete_old_scanned_docs_CEO.vbs

Open in new window

0
 
LVL 1

Author Comment

by:tlcsupport
ID: 24158509
I've checked the permissions and the system account has full permissions, but it just wont run a scheduled task.
0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24159851
Unfortunately if you can only remote into the server then you can't create a system context command window in your session.
In your at command, you need to provide the full path to the vbs file. I usually run via the cscript host as well, e.g.
at \\servername 00:10 /interactive /every:F cscript c:\myscripts\myscript.vbs
Also bear in mind that if you're remotely scheduling the task on another server, then a local path specified will be looked for by the server on it's own file system. In this example the C:\ drive on \\servername. If you are centrally storing the script, you will have to share it out and provide the UNC to the at command, e.g.:
at \\servername 00:10 /interactive /every:F cscript \\sername2\scripts\myscript.vbs
Let us know if this helps...
0
 
LVL 1

Author Comment

by:tlcsupport
ID: 24168381
ok, i've tried that, and run the job, but it just sits there saying that its running, when its obviously not.
0
 
LVL 27

Accepted Solution

by:
bluntTony earned 500 total points
ID: 24186224
As it's running as a scheduled task, have you got rid of the msgbox commands? Otherwise it will hang on them. If you're running as SYSTEM and can only remote in to the server, you won't be able to see this feedback and the script will get stuck.
When it's hung, have the files been deleted or not?
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question