6 Replies Latest reply on Jul 10, 2018 9:11 AM by yaquaholic

    Powershell script not running as expected

    bhilgenkamp_botw

      I have created a Powershell script to do some basic monitoring on websites. The script works great in the powershell ISE but I can't get it to work properly as a powershell script component.

       

      Script:

      # URL to check
          $url="http://example.com"
      
      
      # Check the URL for status code and measure the time it takes to get the response
          $xactiontime = Measure-Command { try {
              $response=(Invoke-WebRequest -TimeoutSec 1 $url).StatusCode
              }
      
      
          catch {
              $response=$_.Exception.Response.StatusCode.Value__
              $failmessage=$_.Exception.Message
              }
              }
      
      
      # Convert the time to milliseconds
          $xactiontime=$xactiontime.TotalMilliseconds
      
      
      # Send results back to SolarWinds
          If ($response -eq 200) {
              Write-Host "Message: Success"
              Write-Host "Statistic.StatusCode: $response"
              Write-Host "Statistic.ReponseTime: $xactiontime"
              Exit 0
              } 
          Else {
              Write-Host "Message: $failmessage"
              Write-Host "Statistic: $response"
              Write-Host "Statistic.ResponseTime: $xactiontime"
              Exit 1
              }

       

      A normal result is something like:

      Message: Success

      Statistic.StatusCode: 200

      Statistic.ResponseTime: 71.2977

      Now the issue I'm having is when running the powershell script the Invoke-WebRequest times out. I can set it to 1 sec and the script fails after 1 sec, 30 sec it fails after 30 sec, etc. so this tells me the script is trying to run but isn't completely working for some reason. I ran wireshark on the server I'm testing the script on but I don't see it even trying to open a connection. Is there some kind of environment setting that would be prohibiting the script from opening a network connection? Or some other caveat I'm not aware of?