I need to compare LastWriteTime of a file and compare it to the current time. if the difference is greater then 45 minutes then I need to get a email alert.
Here is what I got so far.
$StartDate=(GET-DATE)
$EndDate=[datetime]”01/01/2014 00:00”
NEW-TIMESPAN –Start $StartDate –End $EndDate
In code above I need to replace $EndDate
with Get-Item C:\Users\myusername\Desktop\file.html | select LastWriteTime
I need to compare the LastWriteTime of file.html with current time.
Please help me store
Get-Item C:\Users\myusername\Desktop\file.html | select LastWriteTime
into $EndDate
so I can do the compare.
Use the datetime Module and the < / > Operator to Compare Two Dates in Python. datetime and simple comparison operators < or > can be used to compare two dates.
Call the getTime() method on each date to get a timestamp. Compare the timestamp of the dates. If a date's timestamp is greater than another's, then that date comes after.
I think this should work:
if (((Get-Date) - (Get-ChildItem file.html).LastWriteTime).TotalMinutes -gt 45) {
Write-Host "Old file"
}
Just to get the date into a variable would be:
$EndDate = (Get-Item C:\Users\myusername\Desktop\file.html).LastWriteTime
or
$EndDate = Get-Item C:\Users\myusername\Desktop\file.html |
select -expandproperty LastWriteTime
The select -expandproperty
syntax is needed on old versions of Powershell (prior to 3.0) when you might be accessing a property on multiple objects. I don't think it is needed even on Powershell 2 if there is only a single object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With