I made a sensor that runs a short powershell script. If I run the script straight from powershell, it works fine...produces either a 2:FAILED or a 0:OK response/output.

But from PRTG, it AWAYS produces a 0:OK no matter what. Can't figure out why. Here's the script below. It queries my server's web page and looks for the phrase *not available* on the page...that determines success or failure. You can skip this top block of code and jump to the bottom. Top portion is to ok the ssl cert. Again, works fine from powershell command line or from powershell ISE, just not PRTG.

add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

$link = "https://server01.mycompany.com/web/MG_HLP_001_INQ.jsp"
$result = Invoke-WebRequest -Uri $link
$fullcontent = $result.Content

if ($fullcontent -like '*Not Available*') {
  $exitstatus = "2:FAILED"
  write-host $exitstatus

  exit 2
  }
ELSE {
  $exitstatus = "0:OK"
  write-host $exitstatus

  exit 0
  }


Article Comments

I figured this out myself. I added the parameter -UseBasicParsing I changed the Invoke-WebRequest line to: $result = Invoke-WebRequest -Uri $link -UseBasicParsing

Not 100% sure why that works but it does.

-thanks ME


Jan, 2016 - Permalink