1

I am trying to verify if my URLs get a response. in other words, i am trying to check if the authentication has succeeded approaching the site.

I used:

$HTTP_Request = [System.Net.WebRequest]::Create('http://example.com')
$HTTP_Response = $HTTP_Request.GetResponse()
$HTTP_Status = [int]$HTTP_Response.StatusCode

If ($HTTP_Status -eq 200) { 
    Write-Host "Site is OK!" 
} Else {
    Write-Host "The Site may be down, please check!"
}

$HTTP_Response.Close()

and I got the response:

The remote server returned an error: (401) Unauthorized.

but after that i got:

site is ok

Does that mean it's ok? If not, what is wrong?

Raf
  • 9,681
  • 1
  • 29
  • 41
user3649137
  • 33
  • 1
  • 1
  • 5

2 Answers2

1

You are getting OK because you rerun your command and $HTTP_Response contains an object from a previous, successful run. Use try/catch combined with a regex to extract correct status code(and clean up your variables):

$HTTP_Response = $null
$HTTP_Request = [System.Net.WebRequest]::Create('http://example.com/')
try{
    $HTTP_Response = $HTTP_Request.GetResponse()
    $HTTP_Status = [int]$HTTP_Response.StatusCode

    If ($HTTP_Status -eq 200) { 
        Write-Host "Site is OK!" 
    }
    else{
        Write-Host ("Site might be OK, status code:" + $HTTP_Status)
    }
    $HTTP_Response.Close()
}
catch{
    $HTTP_Status = [regex]::matches($_.exception.message, "(?<=\()[\d]{3}").Value
    Write-Host ("There was an error, status code:" + $HTTP_Status)
}

.Net HttpWebRequest.GetResponse() throws exceptions on non-OK status codes, you can read more about it here: .Net HttpWebRequest.GetResponse() raises exception when http status code 400 (bad request) is returned

Community
  • 1
  • 1
Raf
  • 9,681
  • 1
  • 29
  • 41
  • thanks, that helped. would you say that and error 401 (unauthorized) means that the site is encrypted? meaning, the kerberos delegation did not allow me to access? or is there an error somewhere else? – user3649137 May 20 '14 at 14:06
  • 401 doesn't mean that the site is encrypted rather you failed to authenticate. Kerberos will only automagically pass your credentials in a browser, for PS constructs try this for auth issues: http://stackoverflow.com/questions/508565/how-to-make-an-authenticated-web-request-in-powershell and http://stackoverflow.com/questions/8919414/powershell-http-post-rest-api-basic-authentication – Raf May 20 '14 at 14:11
  • ok, thanks,i managed to pass my credentials and it said it was connected. – user3649137 May 20 '14 at 14:25
0

I think the following happens:

You are trying to query a web page using GetResponse(). This fails, so the following Statements run with the values you set in a previous run. This leads to the Output you described.

I personally tend to use the invoke-webrequest in my scripts. This makes it easier to handle Errors, because it supports all Common Parameters like Erroraction and so on.

TomG
  • 188
  • 5
  • i tried to use invoke-webrequest but i keep on getting the error: "is not recognized as the name of a cmdlet"- as in an example: $r = Invoke-WebRequest http://www.example.com – user3649137 May 20 '14 at 13:52