29

I have a long running web page that I need Powershell to call. I run it on a nightly basis from the task manager, with the following:

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

but the powershell timeout occurs before the website responds. Is there any way to set the timeout on Invoke-WebRequest to be longer than the standard 60 seconds?

sodawillow
  • 12,497
  • 4
  • 34
  • 44
Josh
  • 10,352
  • 12
  • 58
  • 109

4 Answers4

34

There should be an -TimeoutSec parameter you can feed an integer value to when calling the Invoke-WebRequest cmdlet.

Invoke-WebRequest https://www.example.com/longrunningtask -TimeoutSec 60
sodawillow
  • 12,497
  • 4
  • 34
  • 44
Jeffrey Eldredge
  • 899
  • 7
  • 12
  • 6
    I saw that but that is for DNS resolution. The DNS is found immediately, but the query takes longer than the default time. Is there a global PS setting somewhere that can be updated? – Josh Dec 07 '15 at 15:02
  • 3
    There's not a global setting per se. The default timeout for most of the web cmdlets are read out of [System.Net.HttpWebRequest.Timeout]. You could always try to make a slightly customized copy of the WebClient class and just change the Timeout property. http://stackoverflow.com/questions/1789627/how-to-change-the-timeout-on-a-net-webclient-object to see if that works. – Jeffrey Eldredge Dec 07 '15 at 15:12
6

You might be able to work around the timeout by setting the static ServicePointManager.MaxServicePointIdleTime property. Default value is 100000ms (100 seconds):

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

# Now run your Invoke-WebRequest after making the change

Changes to ServicePointManager only applies to the current appdomain, and will not persist beyond the session (ie. you need to do it every time you run your script)

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • I changed the statement to powershell -command "[System.Net.ServicePointManager]::MaxServicePointIdleTime = 600000; invoke-webrequest http://www.example.com/longrunningtask" and it still kills at 100 seconds. – Josh Dec 07 '15 at 17:41
  • No success, the answer from Jeffrey is working fine though – hanjo Apr 10 '18 at 15:01
1

Here is the command i've tried successfully for powershell to make a web request with ampersand included:

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

please note the double quote added, hope it helps for someone who need to execute web request from command line:)

Ryan Ye
  • 121
  • 1
  • 6
0

I know it's not exactly what you need, since with this approach you are basically setting a timeout for the whole call to be performed:

  1. build client stuff
  2. resolve the address on the DNS
  3. eventually make the handshake if you are using a secure connection
  4. send the request
  5. wait for the response
  6. build all the objects in which the response is wrapped

BUT, if you just need to set an approximative timeout, considering that in most common cases most of the time is spent on the points 4 and 5, with a Start-Job + Wait-Job -Timeout + Receive-Job you should get the job done:

$job = Start-Job -ScriptBolck { Invoke-WebRequest "https://www.example.com/longrunningtask"}
# Wait for the termination of the job, or 60s, whichever occurs first
Wait-Job -Timeout 60
$response = Receive-Job $job