添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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 Will this keep running for every minute until we get a True response from $GetStatusResponse? – Shahboz Jan 28 '18 at 21:49 @Hashim77 Yes. For the 2nd call. The condition $GetStatusResponse -eq $False is simplified; you will probably have to modify it to fit your code. – gms0ulman Jan 28 '18 at 21:54 Right, once the 2nd call is called I get a response as such: { "d": { "ApiKey": "#####", "Status": "DONE" } } – Shahboz Jan 28 '18 at 21:56 If it is not DONE, I need to re-check after 1 minute. So I assume, I need to extract the Status from response body and check it like while($GetStatusResponse.Status eq "Done")? – Shahboz Jan 28 '18 at 21:58 @Hashim77 Exactly! Except you want -ne, as in "keep running while status is NOT Done". – gms0ulman Jan 28 '18 at 22:02

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