Solved

powershell - days since last sent

Posted on 2016-10-26
2
31 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

Suggested Solutions

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
A procedure for exporting installed hotfix details of remote computers using powershell
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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