0

Experts, my PowerShell Do Until Script below is not Sending any HTTP Status Code to Console, even though I have told it do so. The output is blank. It does run without error. I can't figure out how to get the status code outputted to console. Note: The host notexist.nowhere.com below is intentionally wrong. It's there only for testing.

$url = "http://localhost"
$Header = @{Host = "notexist.nowhere.com"; "Pragma" = "no-cache"; "Cache-Control" = "no-cache, no-transform"}
$i = 1

Do{
    Try {
        $SC = (Invoke-RestMethod -Method POST -Uri $url -Headers $header -ErrorAction Stop ).StatusCode
    }
    catch {
        $SC = ( $_.Exception.Response.StatusCode.value__ )
    }
 
    Write-host($SC) -ForegroundColor Red -BackgroundColor Cyan
    $clipboard += $SC
 
    $i++
} Until( $SC -eq 200 -or $i -gt 2 )
# Until($i -gt 10)
3
  • 1
    I copied an executed the code exactly as you posted it and it works. I get an '405' error each time through the loop. Commented Apr 30, 2021 at 14:49
  • 1
    Just on general principles, I would write the condition as until (($SC -eq 200) -or ($i -gt 2)) Commented Apr 30, 2021 at 14:51
  • @boxdog - I get a 405 too, on IIS after I just tried it on that platform. But weirdly, on Apache web server, I do not get any status code. Quite odd. Commented May 3, 2021 at 12:06

1 Answer 1

2

I did something similar; knowing I likely don't have the REST API on my local system I ran the code anyhow.

The problem is there's no response code from a server that couldn't be communicated with. So, in your catch block you're assigning a null value, then you get to the Write-Host command and there's nothing to output, because $SC is NULL.

At a glance, and obviously not having the benefit of knowledge both of the environment and mission, I might address this using some logic inside the catch block:

$url = "http://localhost"
$Header = @{Host = "notexist.nowhere.com"; "Pragma" = "no-cache"; "Cache-Control" = "no-cache, no-transform"}
$i = 1

Do{
    Try {
        $SC = (Invoke-RestMethod -Method POST -Uri $url -Headers $header ).StatusCode
    }
    catch {
        If( $error[0].Exception.Message -eq "Unable to connect to the remote server" )
        {
            $SC = $error[0].Exception.Message
        }
        
        # $SC = ( $_.Exception.Response.StatusCode.value__ )
        
    }
 
    Write-host($SC) -ForegroundColor Red -BackgroundColor Cyan
    $clipboard += $SC
 
    $i++
} Until( $SC -eq 200 -or $i -gt 2 )

Note: I didn't use the approach of trying to catch specific exceptions. The exception thrown appears to be too generic for that.

Note: I used $Error[0] to get the last error record rather than $_. That's just a syntax preference because $_ is used so many other places...

You can add additional conditions as much as you need. You can put your own language in the $SC variable etc...

Obviously add more conditions

Sign up to request clarification or add additional context in comments.

1 Comment

I'm marking this as accepted, but I've come to the belief that the problem was not with my code, nor was it with Apache or IIS. Web servers may or may not respond at all to strange requests is my conclusion. A blank PowerShell output is indicative or the server not returning any status code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.