I just recently started on writing Powershell scripts. I have a project where I'm performing Load Tests. I Run the test and Generate a Report based on the result of the test. The tool I am using does that all through their API's. So I have 3 Rest API's that I am sending calls using Powershell script.
1st call: Starts a Load Testing for however many iteration is setup in the configuration (Maybe it runs for hours):
Invoke-RestMethod -Method Post -Uri $StartTestUrl -ContentType "application/json" -OutVariable StartTestResponse -Body $StartTestRequestBody | ConvertTo-Json
2nd call: Gets status of the Load Test we just ran/or still running:
Invoke-RestMethod -Method Post -Uri $GetStatusUrl -ContentType "application/json" -OutVariable GetStatusResponse -Body $GetStatusRequestBody | ConvertTo-Json
3rd call: Generate Report From the finished Test Run:
Invoke-RestMethod -Method Post -Uri $GenerateReportUrl -ContentType "application/json" -OutVariable GenerateReportResponse -Body $GenerateReportRequestBody | ConvertTo-Json
Goal: I want to able to write a DO-WHILE loop or other loops in Powershell that checks the status of the test by calling 2nd api every minute for the status "DONE" in the response. And then start the 3rd call, because if the test is not DONE, I cannot generate a report.
Example:
foreach(var minute in minutes)
// if(status.Done)
// CALL GenerateReport
// else
//keep checking every minute
You can do this using a do-while loop. In this example, it's assumed that the $GetStatusResponse
is just a $true
/$false
value. In reality you will need to modify the code to check for the actual "DONE" message.
Invoke-RestMethod -Method Post -Uri $StartTestUrl -ContentType "application/json" -OutVariable StartTestResponse -Body $StartTestRequestBody | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri $GetStatusUrl -ContentType "application/json" -OutVariable GetStatusResponse -Body $GetStatusRequestBody | ConvertTo-Json
if($GetStatusResponse -eq $False){
Start-Sleep -Seconds 60
}while($GetStatusResponse -eq $False)
Invoke-RestMethod -Method Post -Uri $GenerateReportUrl -ContentType "application/json" -OutVariable GenerateReportResponse -Body $GenerateReportRequestBody | ConvertTo-Json
–
–
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0
with attribution required.
rev 2020.2.12.36053