Table of Contents
Exchange Server Queue is one of the most important topics for every Exchange administrator. Queue Viewer is part of the Exchange Toolbox that's installed on Mailbox servers and Edge Transport server in Exchange Server 2016. Queue Viewer is a most important tool to quickly troubleshoot the mail flow related stuff. Most of the administrator doesn't have time to Logon to each Exchange Server to have to view on mail queue.
Here we will see how we can schedule a task to keep an eye on Max queue of Exchange Server in our inbox. We need to create a batch file and PS1 file and keep them somewhere in you Exchange Server.
Steps
We need to create two files and keep them somewhere in your Exchange drives my docs, download or C:\ drive etc where you have full permission to run these file in-case if you have used delegated account for Exchange installation. File names with extension are as below:-
- ExchangeMaxMailQueue.bat
- ExchangeMaxMailQueue.PS1
Creating .bat File
Open the ExchangeMaxMailQueue.bat file and paste the following path which can vary to your environment, or as per your Exchange application installation.
PowerShell.exe -PSConsoleFile "D:\Program Files\Microsoft\Exchange Server\V15\Bin\ExShell.psc1" -Command ". 'C:\Program Files\Update Services\Exchange
Bats\Exchange Bats\ExchangeMaxMailQueue.ps1'"
You can point to this PowerShell.exe where your Exchange is installed. In this case its installed in D:\ so its using that path, the 2nd part of the command is the path of your .PS1 script where your place it.
Creating .PS1 Script
We will open the ExchangeMaxMailQueue.PS1 script in any editor and fill the following information.
01.
function check_queue
02.
{
03.
$a = get-transportservice | get-queue
| measure-object MessageCount -max
04.
05.
if ($a.Maximum -gt 10)
06.
{
07.
send_email $a.Maximum
08.
}
09.
start-sleep -s 300
10.
check_queue
11.
}
12.
13.
function send_email
14.
{param ($queue_size)
15.
16.
$emailFrom = "ExchangeMailQueue@mstechguru.ae"
17.
$emailTo = "shakir.hussain@mstechguru.ae"
18.
$subject = "Exchange Max Mail QUEUE"
19.
$body = "Message Queues are high, max
value $queue_size"
20.
17.
$emailTo = "shakir.hussain@mstechguru.ae"
18.
$subject = "Exchange Max Mail QUEUE"
$smtpServer = "smtp.mstechguru.ae"
21.
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
22.
$smtp.Send($emailFrom, $emailTo, $subject,
$body)
23.
24.
}
25.
26.
check_queue
In Script you can change Subject value, From Value, To Value and SmtpServer value as per your environment. In From value we can put any dummy email ID which will later appear in Email from option. So no need to put any valid email address in From. In To option we have to put some valid email address for Exchange Administrator who will receive email on daily basis and will keep an eye on increasing disk size.
At this point we have created both files and keep them in C:\ drive somewhere. Now we will create a Schedule Task to run this .bat file on daily basis at particular time like when administrator takes coffee or tea at morning he/she can schedule this at that time to have a view to his inbox for Exchange Disk status.
Scheduling a Task in Exchange
Open the Task Scheduler and click Create Basic Task
Click Next, select Daily and click Next
Click Next, select the Date and time as per your time zone and click Next
Select Start a Program and click Next
Click Next
You can select the path of your Bat file where you place it
"C:\Program Files\Update Services\Exchange Bats\Exchange Bats\ExchangeMaxMailQueue.bat"
Click Next
Click Open the Properties dialog checkbox and click Finish
Go to Trigger and click Edit to edit the condition, you can get the queue hourly basis so that you should have a record of your mail queue but you can configure it as per your need and environment.
Click Repeat Task Evey Checkbox, click OK and OK to save the task
We can have a view of the schedule timings and date etc, and Click OK to finish the Task.