Trying to montior files in an SMTP queue for age of 15 mins or longer

I am trying to Monitor my SMTP queue on my windows 2012 r2 server

when I use the following script i get an output of 1 in powershell.

$file= Get-ChildItem -Path 'c:\inetpub\mailroot\queue' | ?{$_.LastWriteTime -lt (Get-Date).Addminutes(-15)}
$stats=$file.count
Write-Host "Statistic: $stats"

When i move it over to SAM Powershell Script Monitor I get the following message

Output: ==============================================
Statistic: 0

Errors: ==============================================
Get-ChildItem : Cannot find path 'C:\inetpub\mailroot\queue' because it does not exist.
At line:2 char:8
+ $file= Get-ChildItem -Path 'c:\inetpub\mailroot\queue' | Where-Obj ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\inetpub\mailroot\queue:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
 
 
I found out that it is running this powershell script on the Solarwinds server instead of the target server any ideas on how to resolve that?
  • In your PowerShell script component monitor settings, check the box to run the script remotely. You're getting this error because the script is running locally on the Orion server.

  • I used your code and created a scheduled task to send an email alert if it finds files that are in the queue that are older than 5 minutes, otherwise it does nothing

    $stats = 0

    $file= Get-ChildItem -Path 'c:\inetpub\mailroot\queue' | ?{$_.LastWriteTime -lt (Get-Date).Addminutes(-5)}

    $stats=$file.count

    if ( $stats -gt 0)

    {

    Write "The smtp server has $stats items stuck in queue older than 5 minutes" > C:\Notify\smtp.txt

    $body=Get-Content "C:\Notify\smtp.txt" | out-string

    Send-MailMessage -From 'sender@somedomain.com        -To 'recipient@somedomain.com -Subject 'items stuck in SMTP Server Queue' -Body $body -Priority High -DeliveryNotificationOption OnSuccess, OnFailure -SmtpServer 'smtpserver.somedomain.com'

    remove-item "C:\Notify\smtp.txt"

    }

  • For this, you can use Windows Performance Monitor counters,

    Counter: Messages in the queue directory
    Instance: _Total
    Category: SMTP NTFS Store Driver

    a quick check via WMI query:

    Get-WmiObject -class Win32_PerfFormattedData_NTFSDRV_SMTPNTFSStoreDriver  | ? { $_.Name -eq '_Total' }