We help IT Professionals succeed at work.

Help with Exchange 2010 scheduled PowerShell email report

Gilesmp
Gilesmp asked
on
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
Comment
Watch Question

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
<<sorry wrong window>>
Top Expert 2011

Commented:
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

Author

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.
Top Expert 2011
Commented:
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

Author

Commented:
Perfect, thank you.  Works a treat!