question

Swabha avatar image
0 Votes"
Swabha asked Swabha edited

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

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered Swabha edited

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.



capture.jpg (51.8 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80, I have used the web session parameters for the subsequent calls as attached:

  [1]: /answers/storage/attachments/145164-capture.png

145202-statuscode.txt


0 Votes 0 ·
capture.png (244.3 KiB)
statuscode.txt (4.4 KiB)

Are you sure you want to use POST as the method?

Also, on what line(s) in your code is that error generated?

0 Votes 0 ·
Swabha avatar image Swabha RichMatheisen-8856 ·

Rich, I am getting error after using the session variable while calling subsequent suburls

 function Get-SubUrlResponse($hostUri, $Relativeurl)
     {
                     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"
                         
                  }
                  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 $($SleepInterval) sec till next attempt."
              Start-Sleep -Seconds $SleepInterval
          }
       }




0 Votes 0 ·
MotoX80 avatar image
0 Votes"
MotoX80 answered MotoX80 commented

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?

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX8O, please find the screenshots for the lines 54 and 55

145301-image.png


0 Votes 0 ·
image.png (49.2 KiB)

Well, yeah, that matches up with the .txt file that you posted. Did those 2 calls work? As I said: For your initial testing, don't use the Get-SubUrlResponse function. Just do one "suburl" inline with the login call. Get that to work first.

I have to question how much value any retry logic will work when dealing with "suburl's". Your session state may or may not be valid. You may have to add in more retry/re-login code.

0 Votes 0 ·

MotoX80, I have removed Get-SubUrlResponse function. Please find the code aswell as output. 145808-code.txt145873-capture.png


0 Votes 0 ·
code.txt (2.6 KiB)
capture.png (1010.7 KiB)
Show more comments