Powershell script with Invoke-Restmethod ends before getting APIresponse

Welcome Forums General PowerShell Q&A Powershell script with Invoke-Restmethod ends before getting APIresponse

Viewing 3 reply threads
  • Author
    Posts
    • #282661
      Participant
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      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.

      • This topic was modified 3 weeks ago by Vivek.
    • #282682
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,986
      Helping Hand
      Rank: Community Hero

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

      https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7.1

      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.

    • #283177
      Participant
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      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.

    • #283192
      Participant
      Topics: 0
      Replies: 3
      Points: 25
      Helping Hand
      Rank: Member
Viewing 3 reply threads
  • You must be logged in to reply to this topic.