我基本上有这个简单的powershell脚本,它执行一个ssrs报告url并将其保存到网络。Powershell - 简单地检查一个函数是否成功完成(超时)
它通常运行良好,但有时它超时,当它确实,它仍然说它成功。我尝试了一些没有运气的事情。
我的脚本的简化版本是这样的:
-----------------------------
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:\数据\的powershell \ script.ps1:10字符:48 + [IO.Stream] $流= $ result.GetResponseStream < < < <() + CategoryInfo:InvalidOperation:(的GetResponseStream:字符串)[] ,RuntimeException + FullyQualifiedErrorId:InvokeMethodOnNull
您无法在空值表达式上调用方法。 在C:\数据\的powershell \ script.ps1:18字符:38 + $计数= $ stream.Read < < < <($缓冲液,0,$ buffer.Length) + CategoryInfo:InvalidOperation:(读: String)[],RuntimeException + FullyQualifiedErrorId:InvokeMethodOnNull
您无法在空值表达式上调用方法。 在C:\数据\的powershell \ script.ps1:23字符:14 + $ stream.Close < < < <() + CategoryInfo:InvalidOperation:(关闭:字符串)[],的RuntimeException + FullyQualifiedErrorId:InvokeMethodOnNull
任何帮助将不胜感激。我认为这应该相当简单,只是没有正确的语法?谢谢
什么您使用的是PowerShell版本吗?你有没有考虑过使用Invoke-WebRequest作为PS版本3的一部分? – Colyn1337