Share via


Script for checking if file is being modified.

Question

Friday, February 3, 2017 12:38 PM

Hello everybody,

I am looking for an powershell script that will check if an specific file is being modified. I would like to get an error message/variable when the file isn't modified for 1 day. That script we will use to monitor a couple of customers.

is this possible?

All replies (23)

Friday, February 3, 2017 12:59 PM

Just get the LastWriteTime of the file.

\(ツ)_/


Friday, February 3, 2017 1:01 PM

Possible is almost everything you can think of and even if I think it's not a good idea to monitor customers, have you tried to find something in the Microsoft Technet Script Gallery.

Grüße - Best regards

PS:> (79,108,97,102|%{[char]$_})-join''


Friday, February 3, 2017 1:24 PM

While I really don't think a PowerShell script is the right way to go for this simply because a user could modify a file, leave it open without making more changes, and still have it locked, this will check if your file has been modified in the last day:

If you want it to continually check every few minutes, just remove the "#" from every line that has it.  As is, this will just check once and report back.

$lastDay = (Get-Date).AddDays(-1)
$monitoredFile = "C:\Test.xml"
#while ($true)
#{
    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
    }
    # start-sleep -seconds 300
#}

Friday, February 3, 2017 1:28 PM

Why overcomplicate things.  The LastWriteTime is updated every time the file is written to even if it is still open.

([io.FileInfo]$path).LastWriteTime

\(ツ)_/


Friday, February 3, 2017 1:44 PM

Agreed, but if the user isn't actively modifying the file but just has it sitting open (I'm specifically recalling spreadsheets and PDFs that people just have sitting open on their computer for days at a time), the file would still be "locked" against deletion, moving, etc even if the LastWriteTime property shows as more than 24 hours ago, wouldn't it?


Friday, February 3, 2017 3:04 PM

I agree with JRV. Last write time is probable the way to go.. Although his is more efficient.. I would have went..

Get-ItemProperty C:\Test.xml | select lastwritetime

Once you've got that you can do your IF ELSE if your desperate for error messages but using last write time would tell you date and time you will clearly be able to see whether it was over 1 day ago.. Unless you want to automate the process but then Write-host would be useless cause you wouldn't see them..

If you do want it so you can just run it in the background and it notify you when it hasn't been written/amended for a day id use send-mailmessage which will email you when the conditions are met.


Friday, February 3, 2017 4:17 PM

Agreed, but if the user isn't actively modifying the file but just has it sitting open (I'm specifically recalling spreadsheets and PDFs that people just have sitting open on their computer for days at a time), the file would still be "locked" against deletion, moving, etc even if the LastWriteTime property shows as more than 24 hours ago, wouldn't it?

User want to know if file is NOT being modified.  If the date hasn't been changed the file i NOT been modified for the day in question.

\(ツ)_/


Friday, February 3, 2017 4:45 PM | 1 vote

Still don't understand why you want to write a scrip that Lastwritetime will tell you but have a look at this.. Its similar to SYN_ACK's suggestion if im honest..

While($true){
$date = (Get-Date).AddDays(-1)
$Writetime = Get-ItemProperty C:\Users\0199763\Desktop\Adhoc.txt | select lastwritetime | get-date
If ($Writetime -le $date) {Write-host "Not touched for over a day and the last time the document was amended was $writetime"}
Start-Sleep -Seconds 3600
}

Right, what this does is when it runs it will run and if a file hasn't been modified within 1 day of the current date/time it will spit out message telling you and if it has been modified within the day it wont say anything.. It will then wait 1hr and run again.. This is aright if you can keep looking at PowerShell to see if its spit anything out.. I suggest using the same code with the code as above but make these changes..

While($true){
$date = (Get-Date).AddDays(-1)
$Writetime = Get-ItemProperty C:\Users\0199763\Desktop\Adhoc.txt | select lastwritetime | get-date
If ($Writetime -le $date) {Send-MailMessage -To "User01 <[email protected]>" -From "User02 <[email protected]>" -Subject "Check" -Body "This document hasn't been amended for over 1 day now, the last time it was written to was $writetime"
}
Start-Sleep -Seconds 3600
}

This will do the same thing but instead of you having to keep an eye on Powershell, every hour this script will run and if the document hasn't been written to for over one day it will send you an email telling you..


Friday, February 3, 2017 5:10 PM

Agreed, but if the user isn't actively modifying the file but just has it sitting open (I'm specifically recalling spreadsheets and PDFs that people just have sitting open on their computer for days at a time), the file would still be "locked" against deletion, moving, etc even if the LastWriteTime property shows as more than 24 hours ago, wouldn't it?

User want to know if file is NOT being modified.  If the date hasn't been changed the file i NOT been modified for the day in question.

\(ツ)_/

JRV - Would that then be a case of checking that a process has been running for so long.. the if it runs for so long then to kill it so the users can keep them open for longer than a set length of time.. ??


Friday, February 3, 2017 5:18 PM

Agreed, but if the user isn't actively modifying the file but just has it sitting open (I'm specifically recalling spreadsheets and PDFs that people just have sitting open on their computer for days at a time), the file would still be "locked" against deletion, moving, etc even if the LastWriteTime property shows as more than 24 hours ago, wouldn't it?

User want to know if file is NOT being modified.  If the date hasn't been changed the file i NOT been modified for the day in question.

\(ツ)_/

JRV - Would that then be a case of checking that a process has been running for so long.. the if it runs for so long then to kill it so the users can keep them open for longer than a set length of time.. ??

A process has a starttime that can be monitored.

get-process powershell|select starttime

\(ツ)_/


Friday, February 3, 2017 5:37 PM

Agreed, but if the user isn't actively modifying the file but just has it sitting open (I'm specifically recalling spreadsheets and PDFs that people just have sitting open on their computer for days at a time), the file would still be "locked" against deletion, moving, etc even if the LastWriteTime property shows as more than 24 hours ago, wouldn't it?

User want to know if file is NOT being modified.  If the date hasn't been changed the file i NOT been modified for the day in question.

\(ツ)_/

JRV - Would that then be a case of checking that a process has been running for so long.. the if it runs for so long then to kill it so the users can keep them open for longer than a set length of time.. ??

A process has a starttime that can be monitored.

get-process powershell|select starttime

\(ツ)_/

That was my thinking.. So instead of him trying to check lastwritetime - He could check if the process has been running a couple days then kill it..?? That'd probably be the best route to go wouldn't you think?


Friday, February 3, 2017 5:51 PM

The problem with monitoring the process is that you're going to just see the name of the executable used to open it.  Whether you've opened Robots.txt or SuperImportantStuff.txt, the process should still show as "notepad.exe"

I don't know why I got it in my head that he needed to see if the file was locked or not, but any of the three ways we've given to check against the LastWriteTime property should work.

@Hawdon, I think you can skip piping to Get-Date since you can compare your (get-date).addDays(-1) variable directly to LastWriteTime without any conversion needed.  The object types should match up already.


Friday, February 3, 2017 5:51 PM

What if the process only runs once a day for 1 second and creates the file?

\(ツ)_/


Friday, February 3, 2017 5:55 PM

What if the process only runs once a day for 1 second and creates the file?

\(ツ)_/

I guess that would depend on what these files are and what process is creating them.   OP, can you provide any additional details?


Friday, February 3, 2017 6:24 PM

What if the process only runs once a day for 1 second and creates the file?

\(ツ)_/

I guess that would depend on what these files are and what process is creating them.   OP, can you provide any additional details?

No.  What if the process is a service that never stops and starts?

The question is clear and needs no further information to be solved.  "I would like to get an error message/variable when the file isn't modified for 1 day"

There is no need to know about processes.  You are over-thinking this.

\(ツ)_/


Friday, February 3, 2017 6:41 PM

What if the process only runs once a day for 1 second and creates the file?

\(ツ)_/

I guess that would depend on what these files are and what process is creating them.   OP, can you provide any additional details?

No.  What if the process is a service that never stops and starts?

The question is clear and needs no further information to be solved.  "I would like to get an error message/variable when the file isn't modified for 1 day"

There is no need to know about processes.  You are over-thinking this.

\(ツ)_/

At this point, it's been way over thought. I think it was you and Hawdon started the process discussion. If all we care about is the LastWriteTime of a given file, I think all three of us at this point have chimed in with a while loop that checks the last write time of a file.


Friday, February 3, 2017 6:46 PM | 1 vote

My first post was the concise answer.  It was also the first answer;)

\(ツ)_/


Friday, February 3, 2017 7:02 PM

My first post was the concise answer.  It was also the first answer;)

\(ツ)_/

touche

JRV: 1

ME: 0


Friday, February 3, 2017 7:04 PM

My first post was the concise answer.  It was also the first answer;)

\(ツ)_/

touche

JRV: 1

ME: 0

No.  You did good.  All of it is useful.  I just like simplicity in code as it is the #1 BP.

\(ツ)_/


Monday, February 6, 2017 9:17 AM

The problem with monitoring the process is that you're going to just see the name of the executable used to open it.  Whether you've opened Robots.txt or SuperImportantStuff.txt, the process should still show as "notepad.exe"

I don't know why I got it in my head that he needed to see if the file was locked or not, but any of the three ways we've given to check against the LastWriteTime property should work.

@Hawdon, I think you can skip piping to Get-Date since you can compare your (get-date).addDays(-1) variable directly to LastWriteTime without any conversion needed.  The object types should match up already.

That's a fair point.. Yeah my bad, it was me that brought the process into the equation.. I've jus caught up with what's been going on.. Having a chuckle at my desk  :)


Monday, February 6, 2017 9:23 AM

Thanks everybody for the replies, maybe some extra information is required, since i don't have any knowledge of scripting:

With our monitoring tool Autotask AEM we can deploy scripts on computers to execute.
With an variable that comes out of the script we can generate an support ticket or not.
I would like an script (not nessasary to be powershell, could also be Batch, VBscript, Javascript, Powershell, Python, Ruby, Groovy) that checks if 1 specific file (location and name is Always the same) has been modified the last day. If it is not modified i would like to get an variable/outcome that gives something like 'false'. So we can use that 'false' to tell our monitoring system to generate an ticket.


Monday, February 6, 2017 11:49 AM | 1 vote

Thanks everybody for the replies, maybe some extra information is required, since i don't have any knowledge of scripting:

With our monitoring tool Autotask AEM we can deploy scripts on computers to execute.
With an variable that comes out of the script we can generate an support ticket or not.
I would like an script (not nessasary to be powershell, could also be Batch, VBscript, Javascript, Powershell, Python, Ruby, Groovy) that checks if 1 specific file (location and name is Always the same) has been modified the last day. If it is not modified i would like to get an variable/outcome that gives something like 'false'. So we can use that 'false' to tell our monitoring system to generate an ticket.

You are posting in a PowerShell only forum so you will only get PowerShell answers.  Also we cannot design and build custom scripts for use with third party tools.  The code posted will check a file and return a true/false.  How you would use this in your tool is between you and the tool vendor.

The following line:

([io.fileInfo]'c:\folder\file.ext).LastWriteTIme -gt [datetime]::Today.AddDays(-1)

will always return true or false.

\(ツ)_/


Monday, February 6, 2017 5:03 PM

Unrelated note - JRV - How do you get the standard message under each reply?

I.e. the \("/)_/.. A lot op people have them and im just wondering if I can add auto messages to my replies? Or are they only available to the geniuses :)