Solved

powershell - days since last sent

Posted on 2016-10-26
2
28 Views
Last Modified: 2016-10-27
I want to read a mailbox and locate an email that I've recently sent.
I have a variable that defines how many days should pass before it is resent.

My logic was working okay, but today I found one from last month, and it has been 28 days since it was first sent.
That means that my logic below is calculating (14 - (today - when sent)), which equals -28, giving me a 0 for the result.

My compare logic only 're-notifies' if that value ($DaysSinceNotice) is -le 0.  But now I'm second-guessing myself and would like advice from the collective hive mind. ;^)
$LastSent= $emailFound.DateTimeSent.Date
$DaysSinceNotice = ($RenoticeFrequency - ($(get-date).date.Subtract($LastSent).days)) % $RenoticeFrequency

Open in new window

0
Comment
Question by:sirbounty
2 Comments
 
LVL 79

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 100 total points
ID: 41860804
$frequency = 30
$tsent = $email.found.datetimesent.date
if ($tsent -ge ((get-date).date.adddays(- $frequency))) { write-output 'time to send again' }
1
 
LVL 12

Accepted Solution

by:
Dustin Saunders earned 400 total points
ID: 41860914
Why not use New-TimeSpan to get the difference?

Example:
$frequency = 14
$lastSent = Get-Date
$lastSent = $lastSent.AddDays(-14)

$daysSinceNotice = New-TimeSpan -Start $lastSent -End $(Get-Date)

Write-Host $daysSinceNotice.Days

Open in new window

1

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now