Solved

Running a VBScript from a schdeuled task

Posted on 2009-04-01
13
1,465 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now