1 2 3 4 5 | @echo off c: cd "C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML" set myscript=.\%~n0.ps1 Powershell -ExecutionPolicy unrestricted -command "& {%myscript% %1}" |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | $servername = $args[0] $pssession = new-pssession -configurationname "Microsoft.Exchange" -connectionuri "http://$servername/Powershell" -Authentication Kerberos $startsession = import-pssession -session $pssession -allowclobber -WarningAction SilentlyContinue -DisableNameChecking | out-null $qMsgs = get-queue -Server $servername | where {$_.MessageCount -gt 0} $qMsgCount = ($qMsgs | measure-object MessageCount -max).count $prtg = "<!--?xml version=""1.0"" encoding=""Windows-1252"" ?--> QueueSize $qMsgCount Count -1 50 21 20 10 1 1 Absolute `n" foreach ($msg in $qMsgs) { $channel = $msg.NextHopDomain $c_count = $msg.MessageCount $prtg +=" `n" $prtg +=" $channel`n" $prtg +=" $c_count`n" $prtg +=" Count`n" $prtg +=" -1`n" if ($msg.MessageCount -gt 10) { $prtg +=" 1`n" } $prtg +=" 50`n" $prtg +=" 21`n" $prtg +=" 20`n" $prtg +=" 10`n" $prtg +=" 1`n" $prtg +=" 1`n" $prtg +=" Absolute`n" $prtg +=" `n" } $prtg +=" Total Queued Messages: $qMsgCount`n" function kill_session($servername) { $sessioninfo = get-pssession | ? {$_.ComputerName -like $servername} | measure-object Id -max $sessioncount = $sessioninfo.count if ($sessioncount -gt 0) { Remove-PSSession $pssession #get-pssession $pssession | Remove-PSSession get-pssession | ? {$_.ComputerName -like $servername} | Remove-PSSession kill_session($servername) } else { $prtg } } kill_session($servername) |
- The powershell and batch file must be named the same thing to run, otherwise you’ll need to change the path to the PowerShell script in the Batch file.
- The batch and powershell require one argument, the server you wish to monitor. Since the custom sensor is linked to a device it will/should only monitor that specific device/server.
- I found some issues where the PowerShell didn’t terminate on the remote servers so I added the function to kill the session over again.