Share via

url's availability

Prabha 241 Reputation points
2021-10-23T20:11:34.267+00:00

How to detemine the url's availability until it gets status code 200 after number of retries

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

MotoX80 37,686 Reputation points
2021-10-23T21:58:02.983+00:00

You're resetting $counter to 1 as part of the do/while loop. It will always be less than 6.

You can't reference $Response.Statuscode if you set $Response to an exception value in your catch statements.

I don't understand what you are trying to do on the first 5 loops.

I simplified your code. See if this works.

 Set-StrictMode -Version Latest
 $user = "test"
 $pass = "test"
 $baseUrl = "https://test.cloudapp.azure.com"

 function Get-RootWebStatus($baseUrl) {
    $KeepLooking = $true                       # Should we continue to retry?
    $counter = 1                               # How many times we've tried
    $RetryLimit = 5                            # Max number of retries 
    $SleepInterval = 5                         # How long to sleep between retries 
    $StartTime = Get-Date                      # When we started 
    while ($KeepLooking) {
        write-output "$(Get-Date -format 'g') - Querying $baseUrl to see if available."
        try { 
            $response = Invoke-WebRequest $baseUrl 
            write-output "$(Get-Date -format 'g') - Web request status: $($Response.Statuscode)"
            if ($Response.Statuscode -eq 200) {
                write-output "$(Get-Date -format 'g') - Web site is alive!"
                $KeepLooking = $false
                continue 
            }
        } 
        catch { 
            write-output "$(Get-Date -format 'g') - Error! $($_.Exception.Message)"      
        }
     -  $counter++  | Out-Null
        if ($counter -gt $RetryLimit) {
            write-output "$(Get-Date -format 'g') - RetryLimit exceeded. Giving up.."
            $KeepLooking = $false
            continue
        }
        start-sleep $SleepInterval 
    }
    write-output "$(Get-Date -format 'g') - Execution time was $((New-TimeSpan -start $StartTime -end (Get-Date)).seconds) seconds"  
}  

   Get-RootWebStatus $baseUrl

Was this answer helpful?


2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 48,116 Reputation points
    2021-11-11T22:27:57.933+00:00

    I see the "Write-Host" results. But the "Write-Output" results are emitted to the "Success" stream and returned to the caller of your Get-SubUrlResponse function.

    Also, not all the code for the function is presented. The "do" is missing the closing "}", as is the function. And, there's nothing to show what the function's caller does with the output returned to it (which is where the "execution time" strings should be handled)!

    If you're looking for help with PowerShell you should at least take the advice that's been offered in the past and avail yourself of all the online educational data available, free of charge except the time you spend to acquire the knowledge.

    Was this answer helpful?

    0 comments No comments

  2. Rich Matheisen 48,116 Reputation points
    2021-10-24T02:47:05.53+00:00

    How about this?

    function Get-RootWebStatus($baseUrl) {
        $sleepfor = 1
        $counter = 1
        $maxretries = 5
        do { 
            try { 
                $response = Invoke-WebRequest $baseUrl -ErrorAction STOP
                if ($Response.StatusCode -eq 200){
                    $Response.StatusCode
                    Break                               # avoid the Start-Sleep
                }
            }
            catch {
                if ($counter -gt ($maxretries - 1)){    # if it fails this many times just return the status and quit
                    if ($_.Exception.Response.SttusCode.Value__){
                        $_.Exception.Response.StatusCode.Value__
                    }
                    else {
                        $_.Exception.Message
                    }
                    Break
                    # as an alternate just return, say, a -1 and Break instead of showing a message
                    # -1
                    # Break
                }
            }
            Start-Sleep -s $sleepfor
            $sleepfor *= 2                  # adjust as you feel necessary
            $counter++
        } While ($Response.Statuscode -ne 200 -and $counter -le $maxretries)
    }
    
    Get-RootWebStatus 'ibm.com/gibberish'       # 404
    Get-RootWebStatus 'ibmxyz.com'              # The remote name could not be resolved: 'ibmxyz.com'
    Get-RootWebStatus 'ibm.com'                 # returns 200
    

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.