Can I pass basic authentication header for multiple suburls to check the status codes in powershell.

Prabha 236 Reputation points
2021-10-29T18:06:00.987+00:00

So basically - there is no login URL we need to perform, but rather just pass those credentials (basic auth headers) to every request

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

2 answers

Sort by: Most helpful
  1. MotoX80 31,571 Reputation points
    2021-10-29T21:24:53.513+00:00

    Does the script that you posted work? What error do you get? I don't have any way to test against your web site.

    Did you look at the web site that I mentioned in your other question?

    https://powershellcookbook.com/recipe/vODQ/script-a-web-application-session

    145059-capture.jpg

    I would think that you would only need to login once. If you maintain a session, the web server should "know that it's you" on subsequent calls.


  2. MotoX80 31,571 Reputation points
    2021-10-31T12:47:02.477+00:00

    As I commented earlier, I have no way to test against your site and I have no way to know what the correct call sequence should be.

    Your image shows 2 errors which appear to come from line 66. But I don't know what happened prior to that. Did the invoke commands on lines 54 and 55 work?

    Since the call is in a function, in order to update the $lp varianble, I believe that you will need to refer to the variable like this.

    $reply = Invoke-WebRequest -Uri $requestUri -WebSession $script:lp -Body $Login
    

    For your initial testing, don't use the Get-SubUrlResponse function. Just do one "suburl" inline with login call. Get that to work first.

    And this section doesn't make sense to me.

                $reply = Invoke-WebRequest -Uri $requestUri -WebSession $lp -Body $Login
                 Write-Host "$($i) attempt = Request $($requestUri)  "
    
                 if ($reply.StatusCode -eq 200) 
                 {
                     Write-Host "$($i) attempt = Request StatusCode: $($reply.StatusCode), done!" -ForegroundColor DarkGreen
                     break;
                     # track execution time:
                     $timeTaken = Measure-Command -Expression { $site = Invoke-WebRequest -Uri $requestUri}
                     $milliseconds = $timeTaken.TotalMilliseconds
                     $milliseconds = [Math]::Round($milliseconds, 1) 
                     "This took $milliseconds ms to execute"
     
    

    If the Invoke-WebRequest works (200), you turn around and measure a second Invoke-WebRequest. Why? If you are interested in how long a request took, wouldn't you want to measure the first request?