Powershell Invoke-WebRequest not timing out

Walkerx 141 Reputation points
2021-08-02T11:29:48.753+00:00

I've created a script that downloads files from various sources and save these to a local network share for processing

If I run the script directly from powershell it works and reports error if cannot find the file needed

But if I run the powershell script through SQL Server, if the web file is not found it just hangs the job and does not timeout or report back the error 404

using the command

$Response = Invoke-WebRequest -Uri $source -Outfile $destination -Credential $cred
$StatusCode = $Response.StatusCode

I've tried using the -TimeoutSec and -MaximumRetryCount but these don't work either

Any ideas

Thanks

Windows for business Windows Server User experience PowerShell
SQL Server Other
{count} votes

Accepted answer
  1. Walkerx 141 Reputation points
    2021-08-08T10:10:49.5+00:00

    Found the issue, its when it can't overwrite the file during the download process it fails and just hangs


7 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2021-08-02T18:53:09.563+00:00

    You should add a SQL server tag to your question. There isn't enough information in your question to know what's going on in your script, or how you're tasking the SQL agent to run the script.

    Since the script functions normally when it's run from PowerShell, the problem is likely to be in how the end of the script is handled by SQL agent.

    0 comments No comments

  2. Walkerx 141 Reputation points
    2021-08-02T21:59:15.3+00:00

    the script is running using SQL Server Agent as a daily job.

    the script will exit fine if the file is found and does not receive a 404 error

    if it receives a 404 error then i've set the script to exit with -1 which informs sql to treat the job as failed


  3. Walkerx 141 Reputation points
    2021-08-03T05:13:53.347+00:00

    SQL Agent see's an error if it is required, the issue is that invoke-webrequest just hangs if the url returns a page not found, all other links in the script work.

    the powershell script is working fine when not running via the sql server agent, just because things work from cmd does not mean they will work when running through their own environment within sql server agent.

    0 comments No comments

  4. Seeya Xi-MSFT 16,586 Reputation points
    2021-08-03T07:04:55.217+00:00

    Hi @Walkerx ,

    Please refer to this article. Quote from the bottom of it.
    To bubble up Windows PowerShell errors to SQL Server Agent, you’ll need to do one of the following:
    A. Set your $ErrorActionPreference = “Stop”
    B. Set ErrorAction at the cmdlet-level (more granular)
    C. Use Try/Catch with ErrorActionPreference or ErrorAction
    D. Continue, and fail the SQL Server Agent job

    Best regards,
    Seeya


    If the response is helpful, please click "Accept Answer" and upvote it, as this could help other community members looking for similar queries.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.