Solved

powershell - days since last sent

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

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Synchronize a new Active Directory domain with an existing Office 365 tenant
This article will help you understand what HashTables are and how to use them in PowerShell.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

18 Experts available now in Live!

Get 1:1 Help Now