Jay
Jay

Reputation: 465

Powershell script webclient timeout (1:40) calling an SSRS report

In active batch, I have a report that runs using a Powershell script. The script looks like this:

$wc=new-object system.net.webclient -ErrorVariable err
$wc.UseDefaultCredentials = $true
$url = "http://SSRS-SERVER/etc...&rs:Command=Render&rs:Format=EXCEL"
$file = "ReportName.xls"
echo $url
echo $file
$wc.downloadfile($url,$file)
$err.count
$err 

This script runs perfectly fine when it's quicker than 1 min and 40 seconds, which I've read the default timeout for webclient is 100 - so that makes sense.

I've tried adding:

$wc.Timeout = 500

But no luck. It says:

Property 'Timeout' cannot be found on this object; make sure it exists and is settable.

Can someone please let me know how to extend the timeout period on this script?

Upvotes: 2

Views: 5198

Answers (2)

Andrew West
Andrew West

Reputation: 71

You can extend the C# class in powershell to expose the timeout property.

$Source = @" 
using System.Net;

public class ExtendedWebClient : WebClient { 
    public int Timeout;

    protected override WebRequest GetWebRequest(System.Uri address) { 
        WebRequest request = base.GetWebRequest(address); 
        if (request != null) {
            request.Timeout = Timeout;
        }
        return request;
    }

    public ExtendedWebClient() {
        Timeout = 100000; // Timeout value by default 
    } 
} 
"@;

Add-Type -TypeDefinition $Source -Language CSharp

$webClient = New-Object ExtendedWebClient; 
# Change timeout for webClient 
$webClient.Timeout = 1800000; // milliseconds
$loadData = $webClient.downloadString('http://your_url')

Credit to: Mikhail Kozlov

Upvotes: 2

Alex
Alex

Reputation: 521

If you're using Powershell 3.0 or higher, you can use Invoke-WebRequest and set the timeout

Invoke-WebRequest $url -UseDefaultCredentials -OutFile ReportName.xls -TimeoutSec 500

Upvotes: 4

Related Questions