Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Problem with script as Scheduled Task

Problem with script as Scheduled Task

Problem with script as Scheduled Task

My apologies in advance for such a long first post, but I want to provide as much detail as I think this problem might need.

I use a set of nested VBS scripts to monitor the backup operations of several servers. The Master script is set to run as a scheduled task every day on one computer. The Master runs individual scripts for each independent server being monitored. Each individual script checks a specific dropbox folder for a flag file that indicates whether the nightly backup ran for that server. If the flag file is not found, a command line SMTP e-mail program is used to send a warning e-mail to an administrator.

Here is an excerpt of the Master Script:

CODE -->

Dim objShell, objFSO, strLogFile, filetxt
Const ForReading=1, ForWriting=2, ForAppending=8

Set objShell = CreateObject("WScript.Shell")
set objFSO = CreateObject("Scripting.FileSystemObject")


'Write entry to log file
Set filetxt = objFSO.OpenTextFile(strLogFile, ForAppending, True)
filetxt.WriteLine("BackCheck.vbs now runs on: " & Now())

'Run the individual VBS scripts
objShell.Run "c:\BackupSystemsTests\PGS-CHECK.VBS"

Set objShell = Nothing
Set objFSO = Nothing 

And here is the relevant part of one of the individual scripts launched by the Master script:

CODE -->

'If strMissing exists, then an expected backup job did not run
'so, format the string and send warning e-mail
If strMissing <> "" Then
	If Left(strMissing,1)="," Then 
		strMissing= Right(strMissing,Len(strMissing)-2)
	End if
	'Send warning e-mail
	strResult="No code received."
	strCommandLine="fmail -cfg PSConAUTH.TXT -to pgsmick@XXXX.org -from pgsmick@XXXX.com -subj " & strOrgName & " did not run one or more backup jobs last night! -msg The following job(s) did not run: " & strMissing & vbCRLF & "Please investigate. (From PS-FS1) -log email.log "
	strResult= objShell.Run(strCommandLine,1,True)
		'For Diagnostic use:
		Set filetxt = objFSO.OpenTextFile(strLogFile, ForAppending, True)
		filetxt.WriteLine(strOrgName & " Warning should have been sent on: " & Now() & "Result Code: " & strResult)
End If 

The log file maintained by the Master and Individual scripts show that when the Master script is run manually, everything works as designed. When the Master script is run as a scheduled task, however, it does run and it properly launches all the individual script files, and they all run almost as expected, but the Shell.Run command that generates the e-mail appears not to execute. The diagnostic line seen in the code is written to the log file, so I know that the script is reaching and getting beyond the Shell.Run command.

I get the same behavior when I set up the individual script as a scheduled task, so it appears not to be a nesting issue.

I am capturing error codes from the Shell.Run command in the log file and when the script is run directly, the reported error code is 0, but when run as a scheduled task the error code is 1.

Finally, the command-line e-mail program generates its own error codes when there are problems and writes them to a secondary email log file. If I intentionally introduce an error (like not specifying a recipient) in the e-mail command line and run the script manually, then both the e-mail log file and my main log file show the e-mail program's error code (6), but when the script is run as a scheduled task then the e-mail log file shows no entry and the main log file shows an error code of 1 (which is NOT coming from the e-mail program).

What am I missing that might cause this problem? If I've left out relevant info, please let me know.

Any help would be appreciated.


RE: Problem with script as Scheduled Task

Sorry. Why does the post not word wrap?

RE: Problem with script as Scheduled Task

you should use Chr(34) to wrap your params in your strCommandLine string which contain white space.,..i.e.
strCommandLine = ".... -subj " & Chr(34) & strOrgName & " did not run" & Chr(34) & " -msg " & Chr(34) & " hello world" & Chr(34)
but, i doubt that is your problem if when you run it manually it is fine.
When you run it manually you are logged on a user, it is running under a users context. Your scheduled tasks are running under what context? LocalSystem? perhaps interact with desktop or something other scheduled task option will help.
You could try "psexec.exe -s -i cmd.exe" then run your script in that window and see if you get anything in stdout to help debug.
But, it would prob. be easier to just run your 'fmail' command from the psexec system command prompt, if you are getting a 1 in strResult then this fmail.exe having an issue.
One approach would be to append " >> %temp%\fmailoutput.txt" to the end of your strCommandLine, that way you will always get stdOut from fmail which will be useful for debugging.
But, white space should be your issue, or the context...but i dont know why localsystem (assumed) cant run fmail...but your psexec.exe -s -i cmd.exe should help you debug.

btw, strResult should really be intResult as .Run will return a numeric value...just for readability.
I would also recommend writing strCommandLine to your log file before you run it, then you really know what was attempted to be run, you can copy and paste this to your localsystem context cmd.exe and see what is causing the issue.
You should also wrap your .Run command with On Error Resume Next, then check Err.Number as well as strResult (int), or that is at least what i tend to do.


I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald

RE: Problem with script as Scheduled Task

oh, i would recommend just scheduling tasks on your other servers (each server having a scheduledtask), rather then your 'star' pattern. seems less complicated and would seem more supportable and transparent to me....less complicated as well

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close