How to detemine the url's availability until it gets status code 200 after number of retries
How to detemine the url's availability until it gets status code 200 after number of retries
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
I think he's still working on his original question about waiting for a web site to become available after a startup. He's gone through several iterations of this.
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
Hello All, I have tried the below one but I am not getting the execution time for the success status codes urls. Expected to provide the status codes aswell as time execution in milliseconds for the urls.
function Get-SubUrlResponse($hostUri, $relativeUrl)
{
$requestUri = $hostUri+$relativeUrl
$requestUri
$sleepFor = 1
$Counter = 1
$maxRetries = 5
do {
try {
$responseTime = (Measure-Command -Expression { $suburlResponse = Invoke-WebRequest -Uri $requestUri -Headers @{Authorization = $creds}}).Milliseconds
if ($suburlResponse.StatusCode -eq 200)
{
Write-host "Status code of the $requestUri is " $suburlResponse.StatusCode
write-output "$(Get-Date -format 'g') - Url is alive!"
Write-Host "$Counter attempt = Request StatusCode: $($suburlResponse.StatusCode), success!" -ForegroundColor DarkGreen
write-output "$(Get-Date -format 'g') - Execution time is $responseTime milliseconds"
Break
}
}
Output:
Status code of the https://whatever/ is 200
1 attempt = Request StatusCode: 200, success!
Status code of the https://whatever is 200
1 attempt = Request StatusCode: 200, success!
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.
17 people are following this question.