powershell smtp queue

display list of all smtp on all servers and their state plust queues and amount of incoming/outgoing messages

smtp.ps1

$servers = @('SRV12', 'SRV14', 'SRV16', 'SRV17')
$items = Get-WmiObject -ComputerName $servers IISSmtpServerSetting -Namespace 'ROOT\MicrosoftIISv2' | where ServerAutoStart | where FullyQualifiedDomainName -NotIn $servers
$queue = @{
    name = 'Queue'
    expression = {
        $server = $_.PSComputerName
        $id = $_.Name.Split('/')[1]
        $counter = Get-Counter "\\$server\SMTP Server(SMTP $id)\Remote Queue Length" | Select-Object -First 1 -ExpandProperty CounterSamples | Select-Object -ExpandProperty CookedValue
        return $counter
    }
}
$received = @{
    name = 'Received'
    expression = {
        $server = $_.PSComputerName
        $id = $_.Name.Split('/')[1]
        $counter = Get-Counter "\\$server\SMTP Server(SMTP $id)\Messages Received/sec" -SampleInterval 1 -MaxSamples 5 | Select-Object -ExpandProperty CounterSamples | Measure-Object -Property CookedValue -Average | Select-Object -ExpandProperty Average
        return $counter
    }
}
$sent = @{
    name = 'Sent'
    expression = {
        $server = $_.PSComputerName
        $id = $_.Name.Split('/')[1]
        $counter = Get-Counter "\\$server\SMTP Server(SMTP $id)\Messages Sent/sec" -SampleInterval 1 -MaxSamples 5 | Select-Object -ExpandProperty CounterSamples | Measure-Object -Property CookedValue -Average | Select-Object -ExpandProperty Average
        return $counter
    }
}
$data = $items | Select-Object @{n='Server';e={ $_.PSComputerName }}, $queue, $received, $sent, FullyQualifiedDomainName -ExpandProperty ServerBindings | Select-Object Server, FullyQualifiedDomainName, IP, Queue, Received, Sent
$data | ft -AutoSize
$data | Out-GridView

output example:

Server FullyQualifiedDomainName IP            Queue          Received              Sent
------ ------------------------ --            -----          --------              ----
SRV12  srv12.rabota.ua          31.28.166.25      0                 0                 0
SRV12  relay90.rabota.ua        62.149.24.190  1518                 0                 0
SRV12  r202.rabota.ua           62.149.24.202  1739                 0                 0
SRV12  r203.rabota.ua           62.149.24.203  1800                 0                 0
SRV12  r204.rabota.ua           62.149.24.204  1775                 0                 0
SRV12  r206.rabota.ua           62.149.24.206  1791                 0                 0
SRV12  r199.rabota.ua           62.149.24.199  1571                 0                 0
SRV12  r198.rabota.ua           62.149.24.198   193 0,777056468412247  1,55557288623282
SRV12  relay78.rabota.ua        62.149.24.178  1543                 0                 0
SRV12  relay84.rabota.ua        62.149.24.184  1547                 0                 0
SRV12  relay5.rabota.ua         62.149.24.185  1518                 0                 0
SRV12  relay86.rabota.ua        62.149.24.186  1788                 0                 0
SRV12  relay87.rabota.ua        62.149.24.187  4507                 0                 0
SRV12  relay88.rabota.ua        62.149.24.188  1841                 0                 0
SRV14  relay79.rabota.ua        62.149.24.179     0 0,105093086207182                 0
SRV14  mailsender.rabota.ua     62.149.24.181  2403  0,78956129991058 0,591535077232385
SRV14  relay4.rabota.ua         62.149.24.182     0 0,197213325674989                 0
SRV14  relay83.rabota.ua        62.149.24.183   260 0,388371007208467 0,789126664278788
SRV14  relay89.rabota.ua        62.149.24.189  2988                 0                 0