Help with Exchange 2010 scheduled PowerShell email report

Hi,

I'd like to write a report in PowerShell that will perform either of the following tasks:

If number of emails received from xxx@xxx.com over the past x hours = 0, send email to xxx@company.com stating “No emails received from xxx@xxx.com for past x hours”

If number of emails received with subject "xxx" over the past x hours = 0, send email to xxx@company.com stating “No emails received with subject xxx for past x hours”

Ideally we would use one script for multiple email addresses or subjects but if it's easier we can amend a single script for each requirement.  Has anyone got anything similar to this that we can implement as I honestly don't know where to start.

Thanks in advance.

Giles
LVL 3
GilesmpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

e_aravindCommented:
Can you please check if the "Test email autoconfiguration"  results and log tabs.

> We need to get the autodiscovery working before troubleshooting the OOF and free\busy
e_aravindCommented:
<<sorry wrong window>>
prashanthdCommented:
The following needs to be modified..

      $hours=-5 # Value should be negative
      
      -sender "xxx@xxx.com"

      $from    = "abc@xyz.com"
      $to      = "xxx@company.com"
      $server  = "smtp.xyz.com"
      
      -messagesubject "xxx"

        $from    = "abc@xyz.com"
      $to      = "xxx@company.com"
      $server  = "smtp.xyz.com"
      

Add-PSSnapin Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin.E2010
	
	$hours=-5 # Vaule should be negative
	$stamp   = Get-Date 
	$stamp1  = $stamp.addhours($hours) 
	write-host $stamp1
	
	$dtamps=Get-TransportServer | Get-MessageTrackingLog -start $stamp1 -sender "xxx@xxx.com" | select timestamp
	if (!($dtamps)){
			Write-Host "no mails from xxx@xxx.com"
			$from    = "abc@xyz.com"
			$to      = "xxx@company.com"
			$subject = "No emails received from xxx@xxx.com for past $hours hours"
			$body    = "No emails received from xxx@xxx.com for past $hours hours"
			$server  = "smtp.xyz.com"
			$smtp.Send($from, $to, $subject, $body)
			write-host "mail Sent - $subject"
	}
	
	$dtamps=Get-TransportServer | Get-MessageTrackingLog -start $stamp1 -messagesubject "xxx" | select timestamp
	if (!($dtamps)){
			Write-Host "no mails with xxx"
			$from    = "abc@xyz.com"
			$to      = "xxx@company.com"
			$subject = "No emails received with subject xxx for past $hours hours"
			$body    = "No emails received with subject xxx for past $hours hours"
			$server  = "smtp.xyz.com"
			$smtp.Send($from, $to, $subject, $body)
			write-host "mail Sent - $subject"
	}

Open in new window

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

GilesmpAuthor Commented:
Hi Prashanthd,

Thanks for your reply.  I've implemented this script and changed the values you suggest, however, when running in the EMS I get the error:

You cannot call a method on a null-valued expression.
At D:\Reports\Test report.ps1:16 char:14
+             $smtp.Send <<<< ($from, $to, $subject, $body)
    + CategoryInfo          : InvalidOperation: (Send:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull


and the email is not sent to the specified recipient.
prashanthdCommented:
Missed something...please try the following
Add-PSSnapin Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin.E2010
	
	$hours=-5 # Vaule should be negative
	$stamp   = Get-Date 
	$stamp1  = $stamp.addhours($hours) 
	write-host $stamp1
	
	$dtamps=Get-TransportServer | Get-MessageTrackingLog -start $stamp1 -sender "xxx@xxx.com" | select timestamp
	if (!($dtamps)){
			Write-Host "no mails from xxx@xxx.com"
			$from    = "abc@xyz.com"
			$to      = "xxx@company.com"
			$subject = "No emails received from xxx@xxx.com for past $hours hours"
			$body    = "No emails received from xxx@xxx.com for past $hours hours"
			$server  = "smtp.xyz.com"
$smtp    = new-object Net.Mail.SmtpClient($server)
			$smtp.Send($from, $to, $subject, $body)
			write-host "mail Sent - $subject"
	}
	
	$dtamps=Get-TransportServer | Get-MessageTrackingLog -start $stamp1 -messagesubject "xxx" | select timestamp
	if (!($dtamps)){
			Write-Host "no mails with xxx"
			$from    = "abc@xyz.com"
			$to      = "xxx@company.com"
			$subject = "No emails received with subject xxx for past $hours hours"
			$body    = "No emails received with subject xxx for past $hours hours"
			$server  = "smtp.xyz.com"
$smtp    = new-object Net.Mail.SmtpClient($server)
			$smtp.Send($from, $to, $subject, $body)
			write-host "mail Sent - $subject"
	}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GilesmpAuthor Commented:
Perfect, thank you.  Works a treat!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Exchange

From novice to tech pro — start learning today.