HTTP Status Code check with Powershell

$statusCode = (invoke-webrequest -uri "http://google.com" -UseBasicParsing -TimeoutSec 20).statuscode

 if($StatusCode -ne 200)

 {

 $resultText = 1

 $Context.SetResult($resultText, "Check failed, was not equal to 200");

 }

 else{

 $resultText = 0

 $Context.SetResult($resultText, "Check was successful");

 }


  • I've had a lot of trouble with the built in http content check and some of the other user scripts for checking status codes. (Too many redirects or certificate problems.) From what I've seen the invoke-webrequest seems to handle what other monitors couldn't. 
    Might be able to turn this into a content check using select-string.
  • Mike Rockwell (Progress Software)

    I changed this just a little to add a variable at the top to hold the URL to check instead of in the Invoke-WebRequest ..

     

    1. $url = "http://google.com"
    2.  
    3. $statusCode = (invoke-webrequest -uri $url -UseBasicParsing -TimeoutSec 20).statuscode
    4.  
    5. if($StatusCode -ne 200)
    6.  
    7. {
    8.  
    9. $resultText = 1
    10.  
    11. $Context.SetResult($resultText, "Check failed, was not equal to 200");
    12.  
    13. }
    14.  
    15. else{
    16.  
    17. $resultText = 0
    18.  
    19. $Context.SetResult($resultText, "Check was successful");
    20.  
    21. }