Announcement

Collapse
No announcement yet.

Script to monitoring file date and then send email

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script to monitoring file date and then send email

    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.

  • #2
    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
    Tom Jones
    MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
    PhD, MSc, FIAP, MIITT
    IT Trainer / Consultant
    Ossian Ltd
    Scotland

    ** Remember to give credit where credit is due and leave reputation points where appropriate **

    Comment


    • #3
      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.
      Please give points where appropriate

      <I dont create ready scripts for you, but I'm willing to point you in the right direction>

      Comment


      • #4
        Re: Script to monitoring file date and then send email

        Here you go

        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:\path\filename"
        
        tmFrame = 6e4*(10)   ' 6e4*(..) where .. = minutes
        
        Set objWMIService = GetObject("winmgmts:" _
         & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        
        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
        Create one Sub (or Function) for the SMTP sending code!


        \Rems
        Last edited by Rems; 28th June 2010, 21:52.

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Script to monitoring file date and then send email

          I'll try it, thanks a lot.

          Comment


          • #6
            Re: Script to monitoring file date and then send email

            Possibly alternative script.

            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

            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" )
            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.
            Last edited by Rems; 29th June 2010, 18:28. Reason: live url removed by mod

            Comment


            • #7
              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!
              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:\temp\output.txt"
              
              tmFrame = 6e4*(10)   ' 6e4*(..) where .. = minutes
              
              Set objWMIService = GetObject("winmgmts:" _
               & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
              
              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
              Last edited by h0ndzik; 8th July 2010, 14:08. Reason: edit urls

              Comment


              • #8
                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
                Tom Jones
                MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
                PhD, MSc, FIAP, MIITT
                IT Trainer / Consultant
                Ossian Ltd
                Scotland

                ** Remember to give credit where credit is due and leave reputation points where appropriate **

                Comment


                • #9
                  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
                  Tom Jones
                  MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
                  PhD, MSc, FIAP, MIITT
                  IT Trainer / Consultant
                  Ossian Ltd
                  Scotland

                  ** Remember to give credit where credit is due and leave reputation points where appropriate **

                  Comment


                  • #10
                    Re: Script to monitoring file date and then send email

                    Originally posted by h0ndzik View Post
                    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

                    Comment


                    • #11
                      Re: Script to monitoring file date and then send email

                      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.
                      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 & "\root\cimv2")
                      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

                      Comment

                      Working...
                      X