无法在Powershell中使用变量触发调用Rest方法

问题描述 投票:0回答:1

我正在尝试调用 2 个休息方法并在循环中执行第二个方法,如下面的代码所示。

$resultIdRes = (Invoke-RestMethod -Uri "https://neoload-api.saas.neotys.com/v3/workspaces/tests/abcdefgh/start?testResultName=ADOTest6" -Method Post -Headers @{ accountToken = "123456789"}).resultId

Write-Host "Response of Neoload Run API $resultIdRes "

$url = "https://neoload-api.saas.neotys.com/v3/workspaces/abchdsefg/test-results/$resultIdRes/logs"
$expvalue = "Exit with status"

Write-Host "result url $url"

$maxAttempts = 15
$retryIntervalsSeconds = 60
$currentAttempt =1

while ($currentAttempt -le $maxAttempts) {
Write-Host "Log $currentAttempt.."

$response = Invoke-RestMethod -Uri $url -Method Get -Headers @{ accountToken = "123456789"}
Write-Host "$response"

if ($response  -match $expvalue){
Write-Host "Test is Completed"
break
}
Start-Sleep -Seconds $retryIntervalsSeconds
$currentAttempt++
}

代码在 while 循环中的第二次 Invoke Rest 处失败,下面是输出

 Response of Neoload Run API 938c1a85
    result url https://neoload-api.saas.neotys.com/v3/workspaces/abchdsefg/test-results/938c1a85/logs
    
Log 1..
    Invoke-RestMethod : {
          "code" : 201,
          "message" : "Test result not found."
        }
Log 2..
Log 3..

进入第二次迭代,等待 60 秒,然后进入第三次,依此类推。另外,我无法在 while 循环中看到写入主机输出。请求帮助

powershell invoke-restmethod
1个回答
0
投票
  1. 通过在调用结果 API 之前添加等待时间,解决了“未找到测试结果”错误的第一个问题。测试结果可能尚未完全准备好获取。感谢@mclayton 的建议

  2. 记录 $response 的输出时出现问题,将输出格式转换为字符串解决了它

    $responseDataString=$response|转换为 Json -深度 10

© www.soinside.com 2019 - 2024. All rights reserved.