Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with script as Scheduled Task

Status
Not open for further replies.

PGSmick

IS-IT--Management
Feb 20, 2018
2
0
0
US
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")

strLogFile="c:\BackupSystemsTests\BackCheck.log"

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

'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 "
	[highlight #C4A000]strResult= objShell.Run(strCommandLine,1,True)
[/highlight]		'For Diagnostic use:
		Set filetxt = objFSO.OpenTextFile(strLogFile, ForAppending, True)
		filetxt.WriteLine(strOrgName & " Warning should have been sent on: " & Now() & "Result Code: " & strResult)
		filetxt.Close
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.

Thanks,
Peter
 
Sorry. Why does the post not word wrap?
 
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.

Regards,
MrMovie

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

Ronald McDonald
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top