Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Date comparison failing at the same times each day

$
0
0

I have a script that checks the last run time of a scheduled task on a windows 2012 server. If the last run time was before 35 minutes ago..... it sends me an email. Works great until 1:17AM and 1:17pm. Then I get an email. The logs show the task ran at the right time.....the check that powershell does is failing (I guess) even though the last run time is within the success parameters??? I'm at a loss. Any help would be appreciated.

Here is the almost awesome script:

$tasklists = Get-Content 'C:\Scripts\Monitoring\ScheduledTasksList.txt'
ForEach($tasklists in $tasklists)
{
$servername = "Server1"
$schedule = new-object -com("Schedule.Service")
$schedule.connect($servername)
$tasks = $schedule.getfolder("\").gettasks(0) | Where-Object {$_.Enabled -like 'True' -and $_.Name -like $tasklists}
$tasks | select name, state, lastruntime, lasttaskresult, NextRunTime, Enabled, NumberOfMissedRuns
$LRT = $tasks.LastRunTime.ToString("MM-dd-yyyy hh:mm tt")
$DateTime = (get-date).AddMinutes(-35).ToString("MM-dd-yyyy hh:mm tt")
If($LRT -le $DateTime)
{
$TaskInfo = $tasks | select name, state, lastruntime, lasttaskresult, NextRunTime, Enabled, NumberOfMissedRuns| Out-String
Send-MailMessage -From "myemail@email.org" -To "myemail@email.org" -Subject "Bla bla" -Body "A scheduled task on Server1 called $tasklists last ran at $LRT `n $TaskInfo" -smtpServer mysmtp@email.org
}
}


Please mark my post as helpful or the answer or better yet.... both! :) Thanks!


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>