# Powershell script with Invoke-Restmethod ends before getting APIresponse

**URL:** <https://forums.powershell.org/t/powershell-script-with-invoke-restmethod-ends-before-getting-apiresponse/15433>\
**Category:** PowerShell Help\
**Created:** [December 27, 2020, 11:34pm UTC](https://forums.powershell.org/t/powershell-script-with-invoke-restmethod-ends-before-getting-apiresponse/15433 "2020-12-27T23:34:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![viveke](https://avatars.discourse-cdn.com/v4/letter/v/977dab/32.png) [@viveke](https://forums.powershell.org/u/viveke)\
**Post date:** [December 27, 2020, 11:34pm UTC](https://forums.powershell.org/t/powershell-script-with-invoke-restmethod-ends-before-getting-apiresponse/15433/1 "2020-12-27T23:34:12Z")

</div>

I have a powershell script which uses Invoke-RestMethod to invoke an API to trigger test execution. The test execution has takes about 3 hours. However, powershell script ends after 1Hr 40 Mins.

The powershell scripts is triggered as part of Jenkins Job.

Invoke-RestMethod is as below.

$result = Invoke-RestMethod -Headers $basic -Uri $URI -Body $Body -Method Post -ContentType “application/x-www-form-urlencoded;charset=UTF-8” -TimeoutSec 600

How to have the powershell script run till Inoke-RestMethod gets a response?

I tried calling the powershell script as part of Start-Process with -Wait parameter but it does not help.

$Outfilename = $Env:Outputfile  
$proc = Start-Process powershell -argument “C:\scripts\xxxxexecution.ps1” -wait -PassThru -RedirectStandardOutput “Outputfile.txt”  
$proc.WaitForExit();  
if ($proc.ExitCode -ne 0) {  
Write-Host “$\_ exited with status code $($proc.ExitCode)”  
Get-Content .\Outputfile.txt  
Exit 1  
}  
else {  
Write-Host “$\_ exited with status code $($proc.ExitCode)”  
Get-Content .\Outputfile.txt  
Exit 0  
}

Any help will be appreciated.

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [December 28, 2020, 4:03am UTC](https://forums.powershell.org/t/powershell-script-with-invoke-restmethod-ends-before-getting-apiresponse/15433/2 "2020-12-28T04:03:21Z")

</div>

According to the documentation, TimeOutSec defaults to 0, which is no timeout:

> **[Invoke-RestMethod (Microsoft.PowerShell.Utility) - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7.2&viewFallbackFrom=powershell-7.1)**
>
> The Invoke-RestMethod cmdlet sends HTTP and HTTPS requests to Representational State Transfer (REST) web services that return richly structured data. PowerShell formats the response based to the data type. For an RSS or ATOM feed, PowerShell returns...

What does the API documentation state for session length? Are you getting an error? You specified it ends at a very specific time, 1 hr 40 minutes, 100 minutes or **6000** seconds. The code you posted has a limit of **600** seconds, which is 10 minutes. If you are specifying 6000, then it will stop at 1hr 40 min, so no TimeOutSec should be provided or use 0.

---

<div class="post-metadata">

**Author:** ![viveke](https://avatars.discourse-cdn.com/v4/letter/v/977dab/32.png) [@viveke](https://forums.powershell.org/u/viveke)\
**Post date:** [December 29, 2020, 10:13pm UTC](https://forums.powershell.org/t/powershell-script-with-invoke-restmethod-ends-before-getting-apiresponse/15433/3 "2020-12-29T22:13:09Z")

</div>

Thank you Rob Simmers. I have actually given TimeoutSec as 6000. That was the reason why is it stopping after 1 Hr 40 Mins. I have changed the TimeOutSec to 0, it worked fine.

---

<div class="post-metadata">

**Author:** ![pola34](https://avatars.discourse-cdn.com/v4/letter/p/5fc32e/32.png) [@pola34](https://forums.powershell.org/u/pola34)\
**Post date:** [December 29, 2020, 10:29pm UTC](https://forums.powershell.org/t/powershell-script-with-invoke-restmethod-ends-before-getting-apiresponse/15433/4 "2020-12-29T22:29:38Z")

</div>

[Burning Series](https://burningseriess.at/)

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/powershell-script-with-invoke-restmethod-ends-before-getting-apiresponse/15433/5 "2024-05-16T20:28:05Z")

</div>

Closed on May 16, 2024, 8:28pm UTC


