piątek, 7 września 2012

Powershell / ZABBIX - how to determine if there are any emails in a particular mailbox folder for longer than some certain time.

Recently I had to make a script for ZABBIX user parameter, that will look if mailbox folder contains a email message that has been received over some time ago.

I haven't been doing this before so I was really astonished that I can receive that kind of information with Powershell. Unfortunatelly as far as I know, I can determine only the time of the oldest message - but this works for me, the next step will be to get number of all messages that are actually in folder for above than some certain time - I think I would need outlook object to do that. But at this time I have something like this:

$SystemDate = (Get-Date)
$MailboxStatistics = (Get-MailboxFolderStatistics -IncludeOldestAndNewestItems -identity {MAILBOX} | where {$_.Name -match "{MAILBOX_FOLDER}"})
$OldestItemReceiveDate = ($MailboxStatistics.OldestItemReceivedDate)
if ($OldestItemReceiveDate){
$OldestItemReceiveDateLocalTime = get-date ($OldestItemReceiveDate).tolocaltime()
    $SystemAndLastMessageDateDifference = ($SystemDate - $OldestItemReceiveDateLocalTime)
    if ($SystemAndLastMessageDateDifference.TotalMinutes -gt "10") {
    $MailboxStatistics.ItemsInFolder-1
    } else {Write-Host "0"}
} else {
Write-Host "0"
}

 Thge script will check if there is en email in certain folder in mailbox, that is being there for more than 10 minutes, if it is true, it will get amount of email messages that are in the folder at the time. Unfortunatelly as I said it will return also the emails that are not there for 10 minutes, but we are sure that there is at least one :)

Brak komentarzy:

Prześlij komentarz