status code

Prabha 236 Reputation points
2021-10-12T17:52:32.123+00:00

Need to get status codes of the Relative urls from the Absolute url using basic authentication in powershell script

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,381 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,091 Reputation points
    2021-10-12T19:29:36.803+00:00

    Change line 29 in your code to:

    $resutVar = Get-UrlResponse $baseUrl $url.url $base64AuthInfo
    

    Each of the "urls" is itself a PSCustomObject, not a simple string. You want to pass the value of the property "url" from the variable $url in the foreach expression to your function -- not the PSCustomObject.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 45,091 Reputation points
    2021-10-16T19:11:37.603+00:00

    How about something like this instead?

    $user = "testuser"
    $pass = "testpwd"
    $baseUrl = "https://test.westeurope.cloudapp.azure.com"
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
    $reply = Invoke-WebRequest -Uri $baseUrl -Method GET -Headers @{Authorization = $base64AuthInfo }
    Write-Host "Login done"
    $urlArray = Get-Content -Raw -Path "C:\pipelinetask\urltest1.json" | ConvertFrom-Json
    function Get-UrlResponse($hostUri, $Relativeurl, $authHeader)
    {
        $requestUri = $hostUri+$Relativeurl
        $attempts = 5
    
        for ($i = 1; $i -le $attempts; $i++) {
            try {
                $reply = Invoke-WebRequest -Uri $requestUri -Method GET -Headers @{Authorization = "Basic $($authHeader)"} -ErrorAction STOP
                Write-Host "$($i) attempt = Request $($requestUri) with $($timeoutSec) timeout"
                if ($reply.StatusCode -eq 200) 
                {
                    Write-Host "$($i) attempt = Request StatusCode: $($reply.StatusCode), done!" -ForegroundColor DarkGreen
                    break;
                }
                else  {
                    Write-Host "$($i) attempt = Request StatusCode: $($reply.StatusCode), :-( " -ForegroundColor DarkYellow
                }
            }
            catch {
                $attempts = $i  # preserve the number of tries
                Write-Host "Error: $($_.Exception)" -ForegroundColor Red
            }
            Write-Host "waiting $($intervalSec) sec till next attempt."
            Start-Sleep -Seconds $intervalSec
        }
        if ($resultVar.Status -ne 200){
            Write-Error "$requestUri warm-up Failed after $($attempts) attempts"
        }
        $reply  # Write the reply to the success stream
    }
    
    foreach ($url in $urlArray.urls) {
        $resultVar = Get-UrlResponse $baseUrl $url.url $base64AuthInfo
        Write-Host $resultVar
        Write-Host $url
    }