I basically have this simple powershell script that executes an ssrs report url and saves it to the network.
It usually runs fine, but sometimes it times out and when it does, it still says it succeeded. I've tried a handful of things with no luck.
A simplified version of my script looks like this:
-----------------------------
function RunReport($url,$outputfile) {
# // create a request
[Net.HttpWebRequest] $req = [Net.WebRequest]::create($url)
$req.Method = "GET"
$req.Timeout = 600000 # = 10 minutes
# // Set credentials
$req.UseDefaultCredentials = $true
#echo "Getting Response"
[Net.HttpWebResponse] $result = $req.GetResponse()
[IO.Stream] $stream = $result.GetResponseStream()
#[IO.StreamReader] $reader = New-Object IO.StreamReader($stream)
[System.IO.FileStream]$writeStream = New-Object System.IO.FileStream($outputfile, [System.IO.FileMode]::Create);
# // write to file
[byte[]]$buffer = new-object byte[] 4096
[int]$total = [int]$count = 0
do
{
$count = $stream.Read($buffer, 0, $buffer.Length)
$writeStream.Write($buffer, 0, $count)
} while ($count -gt 0)
$writeStream.Close()
#$stream.flush()
$stream.Close()
}
$url=...
$outputfile=...
IF(RunReport "$url" "$outputfile")
{Write-Host "Success"}
ELSE
{Write-Host "Failed"}
-------------------------------
I've tried stuff like this with no luck:
RunReport "$url" "$outputfile"
If($?)
{Write-Host "Success"}
ELSE
{Write-Host "Failed"}
and
RunReport "$url" "$outputfile"
If($? -eq true)
{Write-Host "Success"}
ELSE
{Write-Host "Failed"}
The timeout error I'm dealing with is:
Exception calling "GetResponse" with "0" argument(s): "The operation has timed out" At C:\data\powershell\script.ps1:9 char:49 + [Net.HttpWebResponse] $result = $req.GetResponse <<<< () + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException
You cannot call a method on a null-valued expression. At C:\data\powershell\script.ps1:10 char:48 + [IO.Stream] $stream = $result.GetResponseStream <<<< () + CategoryInfo : InvalidOperation: (GetResponseStream:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression. At C:\data\powershell\script.ps1:18 char:38 + $count = $stream.Read <<<< ($buffer, 0, $buffer.Length) + CategoryInfo : InvalidOperation: (Read:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression. At C:\data\powershell\script.ps1:23 char:14 + $stream.Close <<<< () + CategoryInfo : InvalidOperation: (Close:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
Any help would be greatly appreciated. I assume this should be fairly easy, just don't have the correct syntax ? Thanks