We have an Exchange 2010 server SP3 which has all roles in it.
We wanted to write a script which checks the queue every 10 minutes and emails us.
I found the following script at https://gallery.technet.microsoft.com/office/e0bb250e-e699-4c6c-a5be-f1af245a2219
Thanks to that author.
However I realized that his script did not run, So I had to modify it to get it to run.
If I launch just the Windows powershell on the server and run the script pasted below BY uncommenting the lines
#Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
#. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
#Connect-ExchangeServer -auto
Then the script works fine.
But for us to run the script using task scheduler, I had to do some research.
The research told me that the actual command I have to run is
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
With the following as an argument
-version 2.0 -NonInteractive -WindowStyle Hidden -command ". ‘C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1’; Connect-ExchangeServer -auto; C:\scripts\Exch2010QueueMonitor.ps1"
And these 3 lines inside the script have to be COMMENTED
#Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
#. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
#Connect-ExchangeServer -auto
To test this script, we purpose created a situation on the server so that the server is unable to send emails out to the Internet.
We manually looked at the Queue and it showed us 30-40 emails.
The problem is that the script runs successfully everytime, in fact every 10 minutes and never emails us anything.
If I comment those 3 lines, and run it manually it does work! (in which case it emails me that the QUEUE has some issues)
What am I missing?
How can i get this script to run successfully?
Any way I can add some debug lines inside to see what its doing?
This is the actual script:
Please do read the comments inside (so you will understand better)
# Script: Exch2010QueueMonitor.ps1
# Purpose: This script can be set as a scheduled task to run every 30minutes and will monitor all exchange 2010 queue's. If a threshold of 10 is met an
# output file with the queue details will be e-mailed to all intended admins listed in the e-mail settings
# Author: Paperclips
# Email: pwd9000@hotmail.co.uk
# Date: May 2011
# Comments: Lines 27, 31-35 should be populated with your own e-mail settings
# Notes:
# - tested with Exchange 2010 SP1
# - The log report output file will be created under "c:\temp\qu.txt"
#
#The following 2 lines: I never used them, since I feel that its not required in my case....
#Because i feel that the author wanted this script to run from other client machines which will connect to exchange server via http to perform remote powershell commands...
#Since i wanted to run this script on the server itself, I think these next 2 lines are not required for my case...
#
#$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://YourCASServerFQDN/PowerShell/ -Authentication Kerberos
#Import-PSSession $s
#
#The following 3 lines need to be UNCOMMENTED if we have to run it manually.
#
#Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
#. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
#Connect-ExchangeServer -auto
$filename = "c:\temp\qu.txt"
Start-Sleep -s 2
if (Get-ExchangeServer | Where { $_.isHubTransportServer -eq $true } | get-queue | Where-Object { $_.MessageCount -gt 10 })
{
Get-ExchangeServer | Where { $_.isHubTransportServer -eq $true } | get-queue | Where-Object { $_.MessageCount -gt 10 } | Format-Table -Wrap -AutoSize | out-file -filepath c:\temp\qu.txt
Start-Sleep -s 2
$smtpServer = "mail.domain.com"
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($filename)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "no-reply@domain.com"
$msg.To.Add("IT@domain.com")
#$msg.To.Add("admin@domain.com")
$msg.Subject = "PLEASE CHECK EXCHANGE QUEUES"
$msg.Body = "Please see attached queue log file for queue information"
$msg.Attachments.Add($att)
$smtp.Send($msg)
exit
}
We wanted to write a script which checks the queue every 10 minutes and emails us.
I found the following script at https://gallery.technet.microsoft.com/office/e0bb250e-e699-4c6c-a5be-f1af245a2219
Thanks to that author.
However I realized that his script did not run, So I had to modify it to get it to run.
If I launch just the Windows powershell on the server and run the script pasted below BY uncommenting the lines
#Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
#. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
#Connect-ExchangeServer -auto
Then the script works fine.
But for us to run the script using task scheduler, I had to do some research.
The research told me that the actual command I have to run is
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
With the following as an argument
-version 2.0 -NonInteractive -WindowStyle Hidden -command ". ‘C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1’; Connect-ExchangeServer -auto; C:\scripts\Exch2010QueueMonitor.ps1"
And these 3 lines inside the script have to be COMMENTED
#Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
#. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
#Connect-ExchangeServer -auto
To test this script, we purpose created a situation on the server so that the server is unable to send emails out to the Internet.
We manually looked at the Queue and it showed us 30-40 emails.
The problem is that the script runs successfully everytime, in fact every 10 minutes and never emails us anything.
If I comment those 3 lines, and run it manually it does work! (in which case it emails me that the QUEUE has some issues)
What am I missing?
How can i get this script to run successfully?
Any way I can add some debug lines inside to see what its doing?
This is the actual script:
Please do read the comments inside (so you will understand better)
# Script: Exch2010QueueMonitor.ps1
# Purpose: This script can be set as a scheduled task to run every 30minutes and will monitor all exchange 2010 queue's. If a threshold of 10 is met an
# output file with the queue details will be e-mailed to all intended admins listed in the e-mail settings
# Author: Paperclips
# Email: pwd9000@hotmail.co.uk
# Date: May 2011
# Comments: Lines 27, 31-35 should be populated with your own e-mail settings
# Notes:
# - tested with Exchange 2010 SP1
# - The log report output file will be created under "c:\temp\qu.txt"
#
#The following 2 lines: I never used them, since I feel that its not required in my case....
#Because i feel that the author wanted this script to run from other client machines which will connect to exchange server via http to perform remote powershell commands...
#Since i wanted to run this script on the server itself, I think these next 2 lines are not required for my case...
#
#$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://YourCASServerFQDN/PowerShell/ -Authentication Kerberos
#Import-PSSession $s
#
#The following 3 lines need to be UNCOMMENTED if we have to run it manually.
#
#Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
#. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
#Connect-ExchangeServer -auto
$filename = "c:\temp\qu.txt"
Start-Sleep -s 2
if (Get-ExchangeServer | Where { $_.isHubTransportServer -eq $true } | get-queue | Where-Object { $_.MessageCount -gt 10 })
{
Get-ExchangeServer | Where { $_.isHubTransportServer -eq $true } | get-queue | Where-Object { $_.MessageCount -gt 10 } | Format-Table -Wrap -AutoSize | out-file -filepath c:\temp\qu.txt
Start-Sleep -s 2
$smtpServer = "mail.domain.com"
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($filename)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "no-reply@domain.com"
$msg.To.Add("IT@domain.com")
#$msg.To.Add("admin@domain.com")
$msg.Subject = "PLEASE CHECK EXCHANGE QUEUES"
$msg.Body = "Please see attached queue log file for queue information"
$msg.Attachments.Add($att)
$smtp.Send($msg)
exit
}
konkani