PowerShell Invoke-RestMethod超时

时间:2018-12-13 06:34:49

标签: powershell

如果通过PowerShell命令可用,我会检查许多服务(大约15个)。如果该服务可用,则脚本运行时间很好,但是如果某些服务不可用,则脚本运行时间不好!那么如何确定Invoke-RestMethod有特定的请求/响应时间呢?我知道有一个参数-TimeoutSec。 例如,如果我在4秒超时后没有响应,则请求并抛出错误!

Invoke-RestMethod $uri -TimeoutSec

也许我误解了参数-TimeoutSec

1 个答案:

答案 0 :(得分:0)

根据帮助:

> get-help Invoke-RestMethod -Parameter Timeoutsec

-TimeoutSec <Int32>
    Specifies how long the request can be pending before it times out. Enter a value in seconds. The default value, 0, specifies an indefinite time-out.

因此该参数需要一个整数来表示超时时间。

Invoke-RestMethod $uri -TimeoutSec 4

请注意帮助中的警告:

A Domain Name System (DNS) query can take up to 15 seconds to return or time out. If your request contains a host name that requires resolution, and you set TimeoutSec to a value greater than zero, but less than 15 seconds, it can take 15 
seconds or more before a WebException is thrown, and your request times out.