In Powershell v3.0 I would like to return the response code from an HTTP GET, such as 200 OK
or 500 Internal Server Error
. (This is for a hack-deploy to do a quick warmup of a deployed site and see if it works, a sort of a mini acceptance test. The status code is truly all I want.)
Against my wishes, HttpWebRequest.GetResponse
throws an error when it receives a 500 Internal Server Error
. This is annoying because it isn't really an error to me in my use case. Anyway, I figured I could catch the exception and still peel out the underlying response code, but I'm having trouble with that.
Here's some almost-working code:
function WebResponseStatusCode (
[Parameter(Mandatory=$true)][string] $url
) {
$req = [system.Net.HttpWebRequest]::Create($url)
try {
$res = $req.GetResponse();
$statuscode = $res.statuscode;
}
catch [System.Net.WebException] {
#the outer error is a System.Management.Automation.ErrorRecord
Write-Host "error!"
return = $_.Response.statuscode; #nope
}
finally {
if (!($res -eq $null)) {
$res.Close();
}
}
return $statuscode;
}
The problem is of course that $_
has no Response
property. Neither does $_.InnerException
, even when cast:
return [System.Net.WebException]($_.InnerException)
I've played around with $_ | Get-Member
and exploring all its properties. I thought $_.TargetObject
had some promise but it doesn't appear to.
(Update) I also think I tried variations on $_.Exception.Response
though may have gotten it wrong.
Getting just a response code seems like such a simple thing to do.
Invoke-WebRequest
handles these errors differently from for exampleTest-Connection
(i.e. ping).Test-Connection
uses-ErrorVariable
and-ErrorAction
as you would expect.Invoke-WebRequest
just callsWrite-Error
when something goes wrong. – Caryophyllaceous