Monitor a single file's Last Modified date

jg 41 Reputation points
2020-11-18T08:58:58.567+00:00

Hi!

I'd like to create a SCOM monitor that checks a given single file's last modified date.

For example: if "C:\backup\backup.bak" last modified date is over 2 days ago (or 24 hours ago) I'd like an alert. Is this possible to do with vbscript or in any other way? I've googled a bit now but I can't find anything suitable...

Many thanks!

System Center Operations Manager
System Center Operations Manager
A family of System Center products that provide infrastructure monitoring, help ensure the predictable performance and availability of vital applications, and offer comprehensive monitoring for datacenters and cloud, both private and public.
1,603 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Laude 86,026 Reputation points
    2020-11-18T09:02:36.203+00:00

    Hi @jg ,

    You can achieve this by creating a monitor that triggers a script, easiest most likely with PowerShell, you'll find many scripts by using your favorite search engine.

    Here's one example:
    SCOM – Watching a log file for changes w/ PowerShell

    Here's what you're looking for with VBscript:
    Simple Script Which Checks The Modified Time (Hours) Of A Log File

    ----------

    (If the reply was helpful please don't forget to upvote or accept as answer, thank you)

    Best regards,
    Leon


1 additional answer

Sort by: Most helpful
  1. SChalakov 10,576 Reputation points MVP Volunteer Moderator
    2020-11-18T15:28:58.223+00:00

    Hi @jg ,

    here is a simple piece of code, which will return since when a file has not been modified and also in will return something if it has been modified within the last day.

    $lastDay = (Get-Date).AddDays(-1)  
    $monitoredFile = "C:\Temp\123.txt"  
        if ((Get-ItemProperty -Path $monitoredFile -Name LastWriteTime).LastWriteTime -gt $lastDay) {  
            write-host "File has been modified within the last day"  
        } else {  
            write-host "File has not been modified since " (Get-ItemProperty -Path $monitoredFile -Name LastWriteTime).LastWriteTime  
        }  
        New-TimeSpan -Start (Get-ItemProperty -Path $monitoredFile -Name LastWriteTime).LastWriteTime -End (Get-Date)  
    

    It is pretty simple, but it also calculates the difference since the file has been edited the last time.
    You can play with it as you want, you can remove the once-day modification and just calculate the Time span.
    When you have it exactly as you want, just put it in a PowerShell monitor (Example the PowerShell MP from cOOKDOWN) and you are ready to go.

    Here also a link to the PowerShell MP:

    PowerShell Monitoring Management Pack
    https://www.cookdown.com/scom-essentials/powershell-authoring/

    The MP is free and there is a video that shows you how to cretae a PoSh based monitor.

    So you have the script now and a MP that you can use, you just need to put those together.

    I hope I could help!

    ----------

    (If the reply was helpful please don't forget to upvote or accept as answer, thank you)
    Regards,
    Stoyan


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.