Script to monitoring file date and then send email

Home Forums Scripting Windows Script Host Script to monitoring file date and then send email

This topic contains 10 replies, has 7 voices, and was last updated by Avatar jeff.schumacher 9 years, 1 month ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • Avatar
    h0ndzik
    Member
    #149745

    Hello, may I ask you for help with a script? I have just script for sending email but I need integrate it with other function.
    I want monitoring file date (time different mustn’t be over 10 min) nonstop. When will be date older then 10 min then send one email with using SMTP server until file date will be up to date (time different less then 10 min) with send another email, which contains positive message. After this I need checking time different again from begining. I tried use some loop but no succes. I need run this script nonstop on the computer. Thanks a lot for advise.

    Avatar
    Ossian
    Moderator
    #181777

    Re: Script to monitoring file date and then send email

    Could you post what you have already?
    At present it is a bit difficult to understand what you are trying to do

    Avatar
    Silver23
    Member
    #292259

    Re: Script to monitoring file date and then send email

    Perhaps instead of asking for the resolution as you would like it to happen, maybe it is more useful if you told us you’re problem.

    Perhaps we can give you a better solution.

    Rems
    Rems
    Moderator
    #227758

    Re: Script to monitoring file date and then send email

    Here you go

    ‘## Real time file monitor.
    ‘## Any changes to the file will be notified *directly*.
    ‘## If the file was not changed during a defined time period
    ‘## (in this sample after every 10 min) a notification will be generated.
    ‘## The monitor stays active until you terminate its wscript.exe process.

    strComputer = “.”

    FileToMonitor = “[COLOR=”darkslateblue”]c:pathfilename[/COLOR]”

    tmFrame = 6e4*([COLOR=”DarkSlateBlue”]10[/COLOR]) ‘ 6e4*(..) where .. = minutes

    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” & strComputer & “rootcimv2”)

    Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    (“SELECT * FROM __InstanceModificationEvent WITHIN 9 WHERE ” _
    & “TargetInstance ISA ‘CIM_DataFile’ AND ” _
    & “TargetInstance.Name='” & replace(FileToMonitor, “”, “\”) & “‘”)

    FlagRaised = False
    On Error Resume Next

    Do
    Err.clear
    Set objLatestEvent = colMonitoredEvents.NextEvent(tmFrame)

    If Err.number 0 Then
    rem “File did not have changed during the last ‘tmFrame’ ”

    ‘ Send negative notification. *only* send if last was a positive one
    If FlagRaised = False Then
    [COLOR=”Red”]sendmail “Alert. The file not have been changed over the last ” & tmFrame/6e4 & ” minutes”[/COLOR]
    FlagRaised = True
    End If

    Else
    rem “The file just have been modified”
    strFilename = objLatestEvent.TargetInstance.FileName _
    & “.” & objLatestEvent.TargetInstance.Extension

    ‘ Send positive mail *only* if last was a negative one
    If FlagRaised = True Then
    [COLOR=”red”]sendmail “Reset. The file just has been modified”[/COLOR]
    FlagRaised = False
    End If
    End IF
    Loop

    Sub sendmail(strSubject)
    ‘replace with your code
    WScript.CreateObject(“WScript.Shell”).Popup _
    strSubject, 4, “My message”, 0+64
    End Sub
    [/CODE]

    Create one Sub (or Function) for the SMTP sending code!

    Rems[CODE]
    ‘## Real time file monitor.
    ‘## Any changes to the file will be notified *directly*.
    ‘## If the file was not changed during a defined time period
    ‘## (in this sample after every 10 min) a notification will be generated.
    ‘## The monitor stays active until you terminate its wscript.exe process.

    strComputer = “.”

    FileToMonitor = “c:pathfilename

    tmFrame = 6e4*(10) ‘ 6e4*(..) where .. = minutes

    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” & strComputer & “rootcimv2”)

    Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    (“SELECT * FROM __InstanceModificationEvent WITHIN 9 WHERE ” _
    & “TargetInstance ISA ‘CIM_DataFile’ AND ” _
    & “TargetInstance.Name='” & replace(FileToMonitor, “”, “\”) & “‘”)

    FlagRaised = False
    On Error Resume Next

    Do
    Err.clear
    Set objLatestEvent = colMonitoredEvents.NextEvent(tmFrame)

    If Err.number 0 Then
    rem “File did not have changed during the last ‘tmFrame’ “

    ‘ Send negative notification. *only* send if last was a positive one
    If FlagRaised = False Then
    sendmail “Alert. The file not have been changed over the last ” & tmFrame/6e4 & ” minutes”
    FlagRaised = True
    End If

    Else
    rem “The file just have been modified”
    strFilename = objLatestEvent.TargetInstance.FileName _
    & “.” & objLatestEvent.TargetInstance.Extension

    ‘ Send positive mail *only* if last was a negative one
    If FlagRaised = True Then
    sendmail “Reset. The file just has been modified”
    FlagRaised = False
    End If
    End IF
    Loop

    Sub sendmail(strSubject)
    ‘replace with your code
    WScript.CreateObject(“WScript.Shell”).Popup _
    strSubject, 4, “My message”, 0+64
    End Sub
    [/CODE]

    Create one Sub (or Function) for the SMTP sending code!

    Rems

    #375370

    Re: Script to monitoring file date and then send email

    I’ll try it, thanks a lot.

    Avatar
    PatrickMc
    Member
    #353669

    Re: Script to monitoring file date and then send email

    Possibly alternative script.

    # Script FileMonitor.txt
    var string filepath, time10mins
    while (true)
    do
    # Create a time stamp for 10 mins earlier.
    set $time10mins = addtime( diff( “-1000” ) )
    # Get time stamp for the file.
    af $filepath > null
    # Is file’s timestamp less than 10 mins ?
    if ($fmtime < $time10mins)
    do
    # SEND NEGATIVE EMAIL
    done
    else
    do
    # SEND POSITIVE EMAIL
    done
    endif

    # Sleep 60 seconds.
    sleep 60
    done
    [/CODE]

    Script in biterscripting. Save script in file C:/Scripts/FileMonitor.txt. call the script with this command,

    [CODE]
    script "C:/Scripts/FileMonitor.txt" filepath( "C:/Folder/File.ext" )
    [/CODE]

    Use the correct file path in place of [COLOR=red]"C:/Folder/File.ext" [/COLOR][COLOR=black].[/COLOR]

    [COLOR=#000000]Also, use your script to send email in place of [COLOR=red]SEND POSITIVE EMAIL[/COLOR] and [COLOR=red]SEND NEGATIVE EMAIL[/COLOR].[/COLOR]

    [COLOR=#000000]Check out the help pages for addtime() etc. here http: //www .biterscripting .com/helppages/addtime.html.[/COLOR][CODE]
    # Script FileMonitor.txt
    var string filepath, time10mins
    while (true)
    do
    # Create a time stamp for 10 mins earlier.
    set $time10mins = addtime( diff( “-1000” ) )
    # Get time stamp for the file.
    af $filepath > null
    # Is file’s timestamp less than 10 mins ?
    if ($fmtime < $time10mins)
    do
    # SEND NEGATIVE EMAIL
    done
    else
    do
    # SEND POSITIVE EMAIL
    done
    endif

    # Sleep 60 seconds.
    sleep 60
    done
    [/CODE]

    Script in biterscripting. Save script in file C:/Scripts/FileMonitor.txt. call the script with this command,

    script “C:/Scripts/FileMonitor.txt” filepath( “C:/Folder/File.ext” )
    [/CODE]

    Use the correct file path in place of [COLOR=red]”C:/Folder/File.ext” [/COLOR][COLOR=black].[/COLOR]

    [COLOR=#000000]Also, use your script to send email in place of [COLOR=red]SEND POSITIVE EMAIL[/COLOR] and [COLOR=red]SEND NEGATIVE EMAIL[/COLOR].[/COLOR]

    [COLOR=#000000]Check out the help pages for addtime() etc. here http: //www .biterscripting .com/helppages/addtime.html.[/COLOR][CODE]
    script “C:/Scripts/FileMonitor.txt” filepath( “C:/Folder/File.ext” )
    [/CODE]

    Use the correct file path in place of “C:/Folder/File.ext” .

    Also, use your script to send email in place of SEND POSITIVE EMAIL and SEND NEGATIVE EMAIL.

    Check out the help pages for addtime() etc. here http: //www .biterscripting .com/helppages/addtime.html.

    #375371

    Re: Script to monitoring file date and then send email

    Here is final working script.
    I try describe my problem: I have a application, which generate txt file (like a report of service) and I need to know, if the file is up to date. If not, I know that application is down.
    Script checking this file all the time and if he recognize that file date is older then 10 (etc) minutes, he will send email. When file date is up to date again, he send anather email with positive information.
    All this is because this service can’t check with WMI or NT service and this is only one way, how monitor health of this app.
    Thanks a lot for help!
    ‘## Real time file monitor.
    ‘## Any changes to the file will be notified *directly*.
    ‘## If the file was not changed during a defined time period
    ‘## (in this sample after every 10 min) a notification will be generated.
    ‘## The monitor stays active until you terminate its wscript.exe process.

    strComputer = “.”

    FileToMonitor = “c:tempoutput.txt”

    tmFrame = 6e4*(10) ‘ 6e4*(..) where .. = minutes

    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” & strComputer & “rootcimv2”)

    Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    (“SELECT * FROM __InstanceModificationEvent WITHIN 9 WHERE ” _
    & “TargetInstance ISA ‘CIM_DataFile’ AND ” _
    & “TargetInstance.Name='” & replace(FileToMonitor, “”, “\”) & “‘”)

    FlagRaised = False
    On Error Resume Next

    Do
    Err.clear
    Set objLatestEvent = colMonitoredEvents.NextEvent(tmFrame)

    If Err.number 0 Then
    rem “File did not have changed during the last ‘tmFrame’ ”

    ‘ Send negative notification. *only* send if last was a positive one
    If FlagRaised = False Then
    sendmail “Alert. The file not have been changed over the last ” & tmFrame/6e4 & ” minutes”
    FlagRaised = True
    End If

    Else
    rem “The file just have been modified”
    strFilename = objLatestEvent.TargetInstance.FileName _
    & “.” & objLatestEvent.TargetInstance.Extension

    ‘ Send positive mail *only* if last was a negative one
    If FlagRaised = True Then
    sendmail2 “Reset. The file just has been modified”
    FlagRaised = False
    End If
    End IF
    Loop

    Sub sendmail(strSubject)
    ‘replace with your code
    Set objMessage = CreateObject(“CDO.Message”)
    objMessage.Subject = “subject”
    objMessage.From = “from”
    objMessage.To = “to”
    objMessage.TextBody = “text”

    ‘==This section provides the configuration information for the remote SMTP server.
    ‘==Normally you will only change the server name or IP.

    objMessage.Configuration.Fields.Item _
    (“h t t p :// schemas.microsoft.com/cdo/configuration/sendusing”) = 2

    ‘Name or IP of Remote SMTP Server
    objMessage.Configuration.Fields.Item _
    (“h t t p :// schemas.microsoft.com/cdo/configuration/smtpserver”) = “192.168.6.100”

    ‘Server port (typically 25)
    objMessage.Configuration.Fields.Item _
    (“h t t p :// schemas.microsoft.com/cdo/configuration/smtpserverport”) = 25

    objMessage.Configuration.Fields.Update

    ‘==End remote SMTP server configuration section==

    objMessage.Send
    End Sub

    Sub sendmail2(strSubject)
    ‘replace with your code
    Set objMessage = CreateObject(“CDO.Message”)
    objMessage.Subject = “subject”
    objMessage.From = “from”
    objMessage.To = “to”
    objMessage.TextBody = “text”

    ‘==This section provides the configuration information for the remote SMTP server.
    ‘==Normally you will only change the server name or IP.

    objMessage.Configuration.Fields.Item _
    (“h t t p :// schemas.microsoft.com/cdo/configuration/sendusing”) = 2

    ‘Name or IP of Remote SMTP Server
    objMessage.Configuration.Fields.Item _
    (“h t t p :// schemas.microsoft.com/cdo/configuration/smtpserver”) = “192.168.6.100”

    ‘Server port (typically 25)
    objMessage.Configuration.Fields.Item _
    (“h t t p :// schemas.microsoft.com/cdo/configuration/smtpserverport”) = 25

    objMessage.Configuration.Fields.Update

    ‘==End remote SMTP server configuration section==

    objMessage.Send
    End Sub[/CODE][CODE] ‘## Real time file monitor.
    ‘## Any changes to the file will be notified *directly*.
    ‘## If the file was not changed during a defined time period
    ‘## (in this sample after every 10 min) a notification will be generated.
    ‘## The monitor stays active until you terminate its wscript.exe process.

    strComputer = “.”

    FileToMonitor = “c:tempoutput.txt”

    tmFrame = 6e4*(10) ‘ 6e4*(..) where .. = minutes

    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” & strComputer & “rootcimv2”)

    Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    (“SELECT * FROM __InstanceModificationEvent WITHIN 9 WHERE ” _
    & “TargetInstance ISA ‘CIM_DataFile’ AND ” _
    & “TargetInstance.Name='” & replace(FileToMonitor, “”, “\”) & “‘”)

    FlagRaised = False
    On Error Resume Next

    Do
    Err.clear
    Set objLatestEvent = colMonitoredEvents.NextEvent(tmFrame)

    If Err.number 0 Then
    rem “File did not have changed during the last ‘tmFrame’ “

    ‘ Send negative notification. *only* send if last was a positive one
    If FlagRaised = False Then
    sendmail “Alert. The file not have been changed over the last ” & tmFrame/6e4 & ” minutes”
    FlagRaised = True
    End If

    Else
    rem “The file just have been modified”
    strFilename = objLatestEvent.TargetInstance.FileName _
    & “.” & objLatestEvent.TargetInstance.Extension

    ‘ Send positive mail *only* if last was a negative one
    If FlagRaised = True Then
    sendmail2 “Reset. The file just has been modified”
    FlagRaised = False
    End If
    End IF
    Loop

    Sub sendmail(strSubject)
    ‘replace with your code
    Set objMessage = CreateObject(“CDO.Message”)
    objMessage.Subject = “subject”
    objMessage.From = “from”
    objMessage.To = “to”
    objMessage.TextBody = “text”

    ‘==This section provides the configuration information for the remote SMTP server.
    ‘==Normally you will only change the server name or IP.

    objMessage.Configuration.Fields.Item _
    (“h t t p :// schemas.microsoft.com/cdo/configuration/sendusing”) = 2

    ‘Name or IP of Remote SMTP Server
    objMessage.Configuration.Fields.Item _
    (“h t t p :// schemas.microsoft.com/cdo/configuration/smtpserver”) = “192.168.6.100”

    ‘Server port (typically 25)
    objMessage.Configuration.Fields.Item _
    (“h t t p :// schemas.microsoft.com/cdo/configuration/smtpserverport”) = 25

    objMessage.Configuration.Fields.Update

    ‘==End remote SMTP server configuration section==

    objMessage.Send
    End Sub

    Sub sendmail2(strSubject)
    ‘replace with your code
    Set objMessage = CreateObject(“CDO.Message”)
    objMessage.Subject = “subject”
    objMessage.From = “from”
    objMessage.To = “to”
    objMessage.TextBody = “text”

    ‘==This section provides the configuration information for the remote SMTP server.
    ‘==Normally you will only change the server name or IP.

    objMessage.Configuration.Fields.Item _
    (“h t t p :// schemas.microsoft.com/cdo/configuration/sendusing”) = 2

    ‘Name or IP of Remote SMTP Server
    objMessage.Configuration.Fields.Item _
    (“h t t p :// schemas.microsoft.com/cdo/configuration/smtpserver”) = “192.168.6.100”

    ‘Server port (typically 25)
    objMessage.Configuration.Fields.Item _
    (“h t t p :// schemas.microsoft.com/cdo/configuration/smtpserverport”) = 25

    objMessage.Configuration.Fields.Update

    ‘==End remote SMTP server configuration section==

    objMessage.Send
    End Sub[/CODE]

    Avatar
    Ossian
    Moderator
    #181932

    Re: Script to monitoring file date and then send email

    There may be hyperlinks in the script which you do not have permission to add until you have 10 posts

    Avatar
    Ossian
    Moderator
    #181935

    Re: Script to monitoring file date and then send email

    So edit them out as you did above — please don’t post garbage to reach the magic number

    Avatar
    vonPryz
    Member
    #347475

    Re: Script to monitoring file date and then send email

    h0ndzik;210631 wrote:
    All this is because this service can’t check with WMI or NT service and this is only one way, how monitor health of this app.

    What happens if you query service state via sc and the service has crashed? Can you send stop and start commands to the service every so often?

    -vP

    #375372

    Re: Script to monitoring file date and then send email

    Quote:
    I restart service manually after script send mail but i think about automatize this by another script.

    I tested this script and it works.
    intSleep – time to wait before start service
    strComputer=”.” – local computer
    strService – name of service to restart

    Maybe good way is combinate this script with script which I post above.

    Option Explicit
    Dim objWMIService, objItem, objService
    Dim colListOfServices, strComputer, strService, intSleep
    strComputer = “.”
    intSleep = 15000
    WScript.Echo ” Click OK, then wait ” & intSleep & ” milliseconds”

    On Error Resume Next
    ‘ NB strService is case sensitive.
    strService = ” ‘WSearch’ ”
    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” _
    & strComputer & “rootcimv2”)
    Set colListOfServices = objWMIService.ExecQuery _
    (“Select * from Win32_Service Where Name =”_
    & strService & ” “)
    For Each objService in colListOfServices
    objService.StopService()
    WSCript.Sleep intSleep
    objService.StartService()
    Next
    WScript.Echo “Your “& strService & ” service has Started”
    WScript.Quit
    [/CODE][CODE]
    Option Explicit
    Dim objWMIService, objItem, objService
    Dim colListOfServices, strComputer, strService, intSleep
    strComputer = “.”
    intSleep = 15000
    WScript.Echo ” Click OK, then wait ” & intSleep & ” milliseconds”

    On Error Resume Next
    ‘ NB strService is case sensitive.
    strService = ” ‘WSearch’ “
    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” _
    & strComputer & “rootcimv2”)
    Set colListOfServices = objWMIService.ExecQuery _
    (“Select * from Win32_Service Where Name =”_
    & strService & ” “)
    For Each objService in colListOfServices
    objService.StopService()
    WSCript.Sleep intSleep
    objService.StartService()
    Next
    WScript.Echo “Your “& strService & ” service has Started”
    WScript.Quit
    [/CODE]

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.

Register for this Petri Webinar!

Software-Defined Unlimited Backup Storage

Tuesday, August 27, 2019 @ 1:00 pm EDT

A Scale-Out Backup storage infrastructure is a must-have technology for your backups. In this webinar, join expert Rick Vanover for a look on what real-world problems are solved by the Scale-Out Backup Repository.

Register Now

Sponsored By