Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Probably a problem with if statements

$
0
0

I'm trying to use system.net.webclient to invoke a website. This isn't a problem but the way I've got it is I'm monitoring multiple mailboxes and invoking the website on new mail. If a new mail comes in to ONE mailbox then it does it no problem but if TWO mailboxes receive mail at the same time then it doesn't pick the second one up.
Here's the code:

$timeflag = (Get-Date)
$timeflag2 = (Get-Date)
$link = "www.link.com"
mailbox = "user@domain.com"
mailbox2 = "user@domain.com"

start loop{
$start = (Get-Date).AddSeconds(-14)
$end = (Get-Date)
$checkmail = Get-MessageTrackingLog -Recipients $mailbox -EventID RECEIVE -Start $start -End $End

write-host "checking first mailbox"
if ($checkmail)
  {
   write-host "mail detected" -ForegroundColor Yellow
   $CurrentDate = Get-Date
   if ($CurrentDate -gt $timeflag)
    {
     write-host "invoke site" -ForegroundColor Yellow
     $wc = New-Object system.net.webclient
     $InvokeSite = $wc.downloadString($link)
     $CurrentDate = (Get-Date)
     $timeflag = $CurrentDate.AddMinutes(5)
     $checkmail = ""
    }
  }

write-host "checking second mailbox"
$checkmail = Get-MessageTrackingLog -Recipients $mailbox2 -EventID RECEIVE -Start $start -End $End
if ($checkmail)
  {
   write-host "mail detected" -ForegroundColor Yellow	# Script doesn't get here if two mails come in
   $CurrentDate = Get-Date
   if ($CurrentDate -gt $timeflag2)
    {
     write-host "invoke site" -ForegroundColor Yellow
     $wc = New-Object system.net.webclient
     $InvokeSite = $wc.downloadString($link)
     $CurrentDate = (Get-Date)
     $timeflag2 = $CurrentDate.AddMinutes(15)
     $checkmail = ""
    }
  }
} # Repeat loop
$timeflag is a variable that it checks to wait for the specified amount of time. This stops me being able to enumerate an array of mailboxes because the delay would be different for some of them. As I say, if the first mailbox picks one up then the second mailbox is skipped but individually they're fine. I decided to give EWS a miss as well as I couldn't get it working in my environment but I might look into it in the future.

Reading over this it's more than likely a problem with my if statements but any advice?

Thanks in advance.



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>