PowerShell:如何确定 Invoke-WebRequest 是否超时?

PowerShell: How can I determine if Invoke-WebRequest has timed out?

我需要在 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..."
  }
}

在 Windows PowerShell 中,抛出的异常将是 [System.Net.WebException],其中 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
}