我基本上有这个简单的 powershell 脚本,它执行一个 ssrs 报告 url 并将其保存到网络。
它通常运行良好,但有时会超时,当它超时时,它仍然说它成功了。我尝试了一些没有运气的事情。
我的脚本的简化版本如下所示:
-----------------------------
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"}
-------------------------------
我试过这样的东西没有运气:
RunReport "$url" "$outputfile"
If($?)
{Write-Host "Success"}
ELSE
{Write-Host "Failed"}
和
RunReport "$url" "$outputfile"
If($? -eq true)
{Write-Host "Success"}
ELSE
{Write-Host "Failed"}
我正在处理的超时错误是:
使用“0”参数调用“GetResponse”的异常:“操作已超时”在 C:\data\powershell\script.ps1:9 char:49 + [Net.HttpWebResponse] $result = $req.GetResponse <<<< () + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException
您不能在空值表达式上调用方法。在 C:\data\powershell\script.ps1:10 char:48 + [IO.Stream] $stream = $result.GetResponseStream <<<< () + CategoryInfo : InvalidOperation: (GetResponseStream:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
您不能在空值表达式上调用方法。在 C:\data\powershell\script.ps1:18 char:38 + $count = $stream.Read <<<< ($buffer, 0, $buffer.Length) + CategoryInfo : InvalidOperation: (Read:String) [ ], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
您不能在空值表达式上调用方法。在 C:\data\powershell\script.ps1:23 char:14 + $stream.Close <<<< () + CategoryInfo : InvalidOperation: (Close:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
任何帮助将不胜感激。我认为这应该相当容易,只是没有正确的语法?谢谢