Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

powershell - days since last sent

Posted on 2016-10-26
2
Medium Priority
?
48 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 84

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 400 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 14

Accepted Solution

by:
Dustin Saunders earned 1600 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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

927 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