我正在使用Invoke-WebRequest cmdlet来检查一个网站在打开VM后是否已经启动并运行。通常,windows服务设置为延迟启动需要10-15分钟。我使用with循环3次尝试和300秒的TimeOutSec参数。然而,我的3次循环尝试在3-4分钟内就结束了.持续了15分钟。少了什么?有什么建议吗?我可以使用开始睡眠,但在这种情况下这是不标准的。
下面是示例代码:
$retryCount = 3
while ($true)
{
try {
$Response = Invoke-WebRequest -Uri $serviceUrl -TimeoutSec 300 -UseDefaultCredentials -UseBasicParsing
Write-Host "$serviceUrl is up and running.'"
break
}
catch {
Write-Host "Windowservice set to delayed start. Trying again..."
Write-Host $_
--$retryCount
if ($retryCount -eq 0)
{
Write-Host "$serviceUrl might not up yet. Please check from Browser."
break
}
}
}我使用的是PowerShell 5.1版本。上面的脚本在服务已经启动或者我在循环中放置启动-睡眠3分钟时运行良好。
我正在尝试启动TFS服务,以下异常在catch块中被捕获:
TF246018: The database operation exceeded the timeout limit and has been cancelled. Verify that the parameters of the operation are correct.发布于 2022-11-09 05:01:33
我想我自己找到了答案。调用-WebRequest在给定时间之前超时的原因是TFS服务返回了异常。抛出异常一段时间后,TFS启动并运行。这就是为什么很难调试这个问题的原因。在手动检查时,我还从浏览器确认了相同的行为。
总之,调用-WebRequest的TimeoutSec属性没有问题。然而,这样的场景可能很常见,我们不能仅仅依靠TimeoutSec参数。
https://stackoverflow.com/questions/74333732
复制相似问题