Question : notification

How can i incorporate an email notification when the scripts is kicked off and stopped. At present, it sends an email when the file has not changed status(updated). Would like to keep this feature and add notifications whenever it starts and stops. Its scheduled to start/stop via task scheduler, but can the start and stop times be defined in the script as well? Thanks in advance.

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_Datafile Where Name = 'C:\\programfiles\\dir\\name.txt")

For Each objFile in colFiles
    strOriginalTimestamp = objFile.LastModified
Next

Wscript.Echo "started: " & Now

Do While True
    Wscript.Sleep 3600000
    Set colFiles = objWMIService.ExecQuery _
        ("Select * from CIM_Datafile Where Name = 'C:\\programfiles\\dir\\name.txt")

    For Each objFile in colFiles
        strLatestTimestamp = objFile.LastModified
    Next

Set objEmail = CreateObject("CDO.Message")

On Error Resume Next    
   ProcessScript
    If strLatestTimestamp <> strOriginalTimestamp Then
        strOriginalTimestamp = strLatestTimeStamp
    Else
        objEmail.Subject = "subject."
    objEmail.From = "from.com"
    objEmail.To = "name.com"
    objEmail.TextBody = body")
    objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "name"
    objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
   End If    
Loop

Answer : notification

If you schedule it to start at the beginning of each day, this will exit the loop at 5pm, and send you the email.  I have made a couple of other minor changes.

I don't like the "wait for one hour" that is built into the script......this could mean you don't get the final notification until 6pm, so I have changed the sleep to only one minute, but the script will only check for the file date change when the minute value of the current time is 00, which is on the hour.

To account for the time it might take for the file to be checked, I have made the end time 5:05:00 PM.

So, in short, this will run all day until 5:05:00 PM, checking the file date every hour.  Because it checks the file date on the hour, you should start the scheduled task at 9:01:00 AM each morning.

Regards,

Rob.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
strComputer = "."
strFile = "C:\program files\dir\name.txt"
dteEndTime = "5:05:00 PM"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_Datafile Where Name = '" & Replace(strFile, "\", "\\") & "'")

For Each objFile In colFiles
    strOriginalTimestamp = objFile.LastModified
Next

strSubject = "Monitoring script started"
strFrom = "from.com"
strTo = "name.com"
strBody = "The monitoring script has started at " & Now
strServer = "name"
SendEmail strSubject, strFrom, strTo, strBody, strServer

Do While CDate(Time) < CDate(dteEndTime)
	' Sleep for one minute
	Wscript.Sleep 60000
    If CDate(Time) < CDate(dteEndTime) Then
	    ' Check if the current time is on the hour
	    If Right("0" & Minute(Time), 2) = "00" Then 
		    'Wscript.Sleep 3600000
		    Set colFiles = objWMIService.ExecQuery _
		        ("Select * from CIM_Datafile Where Name = '" & Replace(strFile, "\", "\\") & "'")
		
		    For Each objFile In colFiles
		        strLatestTimestamp = objFile.LastModified
		    Next 
		
			ProcessScript 
		    If strLatestTimestamp = strOriginalTimestamp Then
				strSubject = "File Date Has Not Changed"
				strFrom = "from.com"
				strTo = "name.com"
				strBody = "The file has not changed since " & strLatestTimeStamp
				strServer = "name"
				SendEmail strSubject, strFrom, strTo, strBody, strServer
			Else
		        strOriginalTimestamp = strLatestTimeStamp
			End If
		End If
	End If
Loop

' This will trigger when the current time passes the 
strSubject = "Monitoring script ended"
strFrom = "from.com"
strTo = "name.com"
strBody = "The monitoring script has ended at " & Now
strServer = "name"
SendEmail strSubject, strFrom, strTo, strBody, strServer

Sub SendEmail(strSubject, strFrom, strTo, strBody, strServer)
	Set objEmail = CreateObject("CDO.Message")
	On Error Resume Next     
	objEmail.Subject = strSubject
	objEmail.From = strFrom
	objEmail.To = strTo
	objEmail.TextBody = strBody
	objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
	objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strServer
	objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
	objEmail.Configuration.Fields.Update
	objEmail.Send
	Err.Clear
	On Error GoTo 0
End Sub
Random Solutions  
 
programming4us programming4us