url's availability

Prabha 236 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 Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,382 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 31,656 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
    

2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 45,091 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
    

  2. Rich Matheisen 45,091 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.

    0 comments No comments