2

当 Invoke-WebRequest 超过 -TimeoutSec 参数设置的时间限制时,我需要产生一些输出。在这种情况下,我如何构建一个运行的 If 条件?

换句话说; 什么代替??????在下面的例子中?:

Try {
  Invoke-RestMethod -Uri $Uri -contentType "application/json"
   -Method Post -Headers $Headers -Body $Body -TimeoutSec 8
}
Catch {
 If (?????) {
    Write-Host "the request timed out..."
  }
}
4

1 回答 1

0

[System.Net.WebException]在 Windows PowerShell 中,抛出的异常将是一个Status字段设置为Timeout

try{
  Invoke-WebRequest 'http://hostname.fqdn/really/slow/endpoint/' -TimeoutSec 3 -UseBasicParsing
}
catch [System.Net.WebException] {
  if($_.Exception.Status -eq 'Timeout'){
    # the request timed out
  }
  # something else went wrong during the web request
}
于 2020-09-20T14:07:54.853 回答