Invoke-WebRequest设置超时

时间:2015-12-07 14:36:13

标签: powershell

我有一个长期运行的网页,我需要Powershell来打电话。我每晚都会从任务管理器运行它,具体如下:

powershell -Command "Invoke-WebRequest https://www.example.com/longrunningtask" 

但PowerShell超时发生在网站响应之前。有没有办法将Invoke-WebRequest上的超时设置为超过标准的60秒?

3 个答案:

答案 0 :(得分:15)

在调用-TimeoutSec cmdlet时,应该有一个Invoke-WebRequest参数可以提供整数值。

Invoke-WebRequest https://www.example.com/longrunningtask -TimeoutSec 60

答案 1 :(得分:0)

您可以通过设置静态ServicePointManager.MaxServicePointIdleTime属性来解决超时问题。默认值为100000毫秒(100秒):

# Bump it up to 180 seconds (3 minutes)
[System.Net.ServicePointManager]::MaxServicePointIdleTime = 180000

# Now run your Invoke-WebRequest after making the change

ServicePointManager的更改仅适用于当前的应用域,并且不会在会话之后持续存在(即,每次运行脚本时都需要执行此操作)

答案 2 :(得分:0)

这是我为Powershell成功尝试的命令,其中包括&符:

powershell-命令“ Invoke-WebRequest”“ http://localhost/index.php?option=val1”“&”“ view = val2”“&”“ key = val3”“&”“ profile = 2”“” -TimeoutSec 600

请注意添加的双引号,希望对需要从命令行执行Web请求的人有所帮助

相关问题