Script to Find latest Modify date, and email alert if last modify is older than 4hrs or so

So, I want to monitor a directory, and get an alert if any file in the directory was not modified in the last, 4 hours or so.  

I think I'm on the right track, but I'm missing some pieces here.  Any suggestions?

Here's what I got so far:

::Set time and date to use later if needed
set currentTime_Date=%time% %date%
::For loop to pull the last file (I can't seem to pull the last modify time/date with it... this is where I need a hand)
FOR /F "delims=" %a IN ('DIR "*.*" /B /O:D') DO SET NewestFile=%a 

::Email latest file using blat
blat.exe -to my@email.com -server server@domain.com -f from@email.com -subject "File Checker" -body "test" -attach %NewestFile% (Not sure if I can use the variable for my newest file here or not)


Any suggestions?


  • Edited by JoeFri Friday, February 27, 2015 5:05 PM
February 27th, 2015 5:05pm

$folder='c:\myfolder'

while($true){
    if(([datetime]::Now - (Get-Item $folder).LastWriteTime) -gt [timespan]'0:4:0:0'){
        Send-MailMessage -to -from -subject ..
        break
    }
}
Free Windows Admin Tool Kit Click here and download it now
February 27th, 2015 6:05pm

correct, and every log entry is two new files.  So it should work in this case.  I haven't had time to look at this further, but last time I tried, I was still getting the error I listed above, missing statement block.
March 16th, 2015 11:56am

correct, and every log entry is two new files.  So it should work in this case.  I haven't had time to look at this further, but last time I tried, I was still getting the error I listed above, missing statement block.

Sorry - missing a paren.

$folder='c:\myfolder'

while($true){
    if([datetime]::Now - (Get-Item $folder).LastWriteTime) -gt [timespan]'0:4:0:0'){
        Send-MailMessage -to -from -subject ..
        break
    }
}

Free Windows Admin Tool Kit Click here and download it now
March 16th, 2015 12:09pm

What's the easiest way to export the PowerShell window to a log when this runs?   

I have it all setup with a scheduled task, but it doesn't seem to be working. (I had an incident today where it had been 6-7 hours since the folder was updated, and never received an email)

Here's what I was trying:

$folder='C:\v1\temp'
$Logfile = "C:\PicksheetMonitoring\WhileLoop.log"

while($true){
    if([datetime]::Now - (Get-Item $folder).LastWriteTime) -gt [timespan]'0:4:0:0'){
        Send-MailMessage -to "Picksheet Analysis <Picksheet_Analysis@domain.com>" -from "Picksheet Alerting <Picksheet_Alert@domain.com>" -subject "It's been at least 4 hours since last Automation run" -smtpserver "exch01.domain.com" ..
        break
    }
}
Function LogWrite
{
   Param ([string]$logstring)

   Add-content $Logfile -value $logstring
}

March 24th, 2015 2:22pm

Here.  Schedule this to run once an hour.

$folder='C:\v1\temp'
$Logfile = "C:\PicksheetMonitoring\WhileLoop.log"

function logit($msg){
    $msg='' -f [datetime]::Now, $msg
    $msg|Out-File $logfile -append
}
logit 'Running check'
if([datetime]::Now - (Get-Item $folder).LastWriteTime) -gt [timespan]'0:4:0:0'){
    Send-MailMessage -to "Picksheet Analysis <Picksheet_Analysis@domain.com>" -from "Picksheet Alerting <Picksheet_Alert@domain.com>" -subject "It's been at least 4 hours since last Automation run" -smtpserver "exch01.domain.com" ..
    logit 'alert sent'
}else{
    logit 'all files good'
}

You do not want to run a scheduled task in a loop.  It defeats the purpose of a scheduled task.

Free Windows Admin Tool Kit Click here and download it now
March 24th, 2015 4:57pm

Typically when a scheduled task is stuck running, it's because the 'program' the task is  trying to run either froze, errored out, or is still actually running...  All the reasons I can think of are related to the 'program' not to anything in the scheduled task, unless the security options are set wrong...(which I've tried and tested)

Also, if I run the script outside of a scheduled task, just from PowerShell, the log file isn't generated either, which tells me there's still something not quite right with the script.  Unless I'm running it wrong...  I'm saving it as a ps1 and testing by right-clicking and "Run with PowerShell"  

am I missing something?


  • Edited by JoeFri Wednesday, March 25, 2015 1:01 PM
March 25th, 2015 12:47pm

We're closer!  

Thanks for all your help so far JRV

Log file is now generating, but all I get it:

[3/25/2015 11:24:07 AM]Running check

I get a "Cannot convert value "0:4:0:0" to type "System.TimeSpan" . Error: "Input string was not in correct format"  

I'll look up some of the System.TimeSpan parameters to try to find the right one..

Any idea's here?


  • Edited by JoeFri Wednesday, March 25, 2015 4:54 PM
Free Windows Admin Tool Kit Click here and download it now
March 25th, 2015 3:27pm

Upgraded to V4 PS, and same error with this script (Note you had the variables and first function doubled up)

$folder='C:\v1\temp'
$Logfile = "C:\PicksheetMonitoring\WhileLoop.log"

function logit($msg){
    $msg='[{0}]{1}' -f [datetime]::Now, $msg
    $msg|Out-File $logfile -append
}
logit 'Running check'
if([datetime]::Now - (Get-Item $folder).LastWriteTime -gt [timespan]'0:4:0:0'){
    Send-MailMessage -to "Picksheet Analysis <Picksheet_Analysis@domain.com>" -from "Picksheet Alerting <Picksheet_Alert@domain.com>" -subject "It's been at least 4 hours since last Automation run" -smtpserver "exch01.domain.com" ..
    logit 'alert sent'
}else{
    logit 'all files good'
}

  • Marked as answer by JoeFri Tuesday, March 31, 2015 12:44 PM
March 31st, 2015 1:19am

Nevermind!  

I found a slight deviance from the original code,  it's working like a charm now! :)

Thanks for all your help jrv, you rock!

For future reference, Task Manager doesn't like calling PS scripts.  A quick bat file can be used to call the PS script, and then does not need to save credentials either (Change the YOURPOWERSHELLSCRIPT line for your script:

@ECHO OFF

SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%YOURPOWERSHELLSCRIPT.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'";

  • Edited by JoeFri Tuesday, March 31, 2015 4:10 PM
Free Windows Admin Tool Kit Click here and download it now
March 31st, 2015 12:44pm

Hey jrv,

I may be getting a little greedy now, but I'm wondering if the datetime function can be used to bypass the email section of this script and only create the log entry for a specific timeframe. (ie after business hours, 5p-5a)

April 1st, 2015 8:12am

I tired it that way, and it still didn't work for some reason, I think it was related to how Task Scheduler was calling it :s

Here's what I did and it works like a charm:

Here's the Bat script:

@ECHO OFF
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%Monitor.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'";

Free Windows Admin Tool Kit Click here and download it now
April 6th, 2015 1:13pm

Never mind here, I was able to use Task Manager scheduling to have it run at better times, so I don't get too many emails at night, when the automation service isn't running.

Thanks again for all your help!

April 6th, 2015 1:14pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics