Solved

powershell - days since last sent

Posted on 2016-10-26
2
42 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 82

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 13

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

IoT Devices - Fast, Cheap or Secure…Pick Two

The IoT market is growing at a rapid pace and manufacturers are under pressure to quickly provide new products. Can you be sure that your devices do what they're supposed to do, while still being secure?

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

628 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