How do you get the response from a 404 page requested from powershell
Change your catch clause to catch the more specific WebException
, then you can use the Response
property on it to get the status code:
{ #...} catch [System.Net.WebException] { $statusCode = [int]$_.Exception.Response.StatusCode $html = $_.Exception.Response.StatusDescription}
BrokenGlass gave the answer, but this might help:
try{ $URI='http://8bit-museum.de/notfound.htm' $HTTP_Request = [System.Net.WebRequest]::Create($URI) "check: $URI" $HTTP_Response = $HTTP_Request.GetResponse() # We then get the HTTP code as an integer. $HTTP_Status = [int]$HTTP_Response.StatusCode} catch [System.Net.WebException] { $statusCode = [int]$_.Exception.Response.StatusCode $statusCode $html = $_.Exception.Response.StatusDescription $html}$HTTP_Response.Close()
Response:check: http://8bit-museum.de/notfound.htm404Not Found
another approach:
$URI='http://8bit-museum.de/notfound.htm'try { $HttpWebResponse = $null; $HttpWebRequest = [System.Net.HttpWebRequest]::Create("$URI"); $HttpWebResponse = $HttpWebRequest.GetResponse(); if ($HttpWebResponse) { Write-Host -Object $HttpWebResponse.StatusCode.value__; Write-Host -Object $HttpWebResponse.GetResponseHeader("X-Detailed-Error"); }}catch { $ErrorMessage = $Error[0].Exception.ErrorRecord.Exception.Message; $Matched = ($ErrorMessage -match '[0-9]{3}') if ($Matched) { Write-Host -Object ('HTTP status code was {0} ({1})' -f $HttpStatusCode, $matches.0); } else { Write-Host -Object $ErrorMessage; } $HttpWebResponse = $Error[0].Exception.InnerException.Response; $HttpWebResponse.GetResponseHeader("X-Detailed-Error");}
if i understand the question then $ErrorMessage = $Error[0].Exception.ErrorRecord.Exception.Message contains the errormessage you are looking for. (source: Error Handling in System.Net.HttpWebRequest::GetResponse() )
Another simple example, hope this helps:
BEGIN{ # set an object to store results $queries = New-Object System.Collections.ArrayList Function Test-Website($Site) { try { # check the Site param passed in $request = Invoke-WebRequest -Uri $Site } catch [System.Net.WebException] # web exception { # if a 404 if([int]$_.Exception.Response.StatusCode -eq 404) { $request = [PSCustomObject]@{Site=$site;ReturnCode=[int]$_.Exception.Response.StatusCode} } else { # set a variable to set a value available to automate with later $request = [PSCustomObject]@{Site=$site;ReturnCode='another_thing'} } } catch { # available to automate with later $request = [PSCustomObject]@{Site=$site;ReturnCode='request_failure'} } # if successful as an invocation and has # a StatusCode property if($request.StatusCode) { $siteURI = $Site $response = $request.StatusCode } else { $response = $request.ReturnCode } # return the data return [PSCustomObject]@{Site=$Site;Response=$response} }}PROCESS{ # test all the things $nullTest = Test-Website -Site 'http://www.Idontexist.meh' $nonNullTest = Test-Website -Site 'https://www.stackoverflow.com' $404Test = Test-Website -Site 'https://www.stackoverflow.com/thispagedoesnotexist' # add all the things to results $queries.Add($nullTest) | Out-Null $queries.Add($nonNullTest) | Out-Null $queries.Add($404Test) | Out-Null # show the info $queries | Format-Table}END{}
Output:
Site Response ---- -------- http://www.Idontexist.meh another_thinghttps://www.stackoverflow.com 200 https://www.stackoverflow.com/thispagedoesnotexist 404