首页 文章

Powershell - 只需检查函数是否成功完成(超时)

提问于
浏览
0

我基本上有这个简单的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

任何帮助将不胜感激 . 我认为这应该相当容易,只是没有正确的语法?谢谢

3 回答

  • 0

    也许你可以使用try / catch来处理这个部分,如下所示:

    try {
     [Net.HttpWebResponse] $result = $req.GetResponse() 
    }
    
    catch {
     return 1
    }
    

    您可以在其他您怀疑代码没有按预期执行的操作中应用相同的技术:

    try { 
    [System.IO.FileStream]$writeStream = New-Object System.IO.FileStream($outputfile, [System.IO.FileMode]::Create);
    }
    
    catch {
     return 1
    }
    

    一旦您发现问题发生的位置,您就可以查看异常

    catch { write-warning $_ ; exit 1 }
    
  • 1

    '无法调用空值表达式上的方法'错误都是因为.GetResponse方法失败并超时,导致$ result变量未分配 . 最简单的改变是将脚本的其余部分(在“$ result = $ req.GetResponse()”之后)放入“if($ result){}”块中 . 然后,您可以使用“then {}”块来执行错误处理 .

    更高级的方法是使用try {}和catch {}块,以捕获实际的超时异常并正确处理它 .

  • 0

    你有两个问题:

    • 超时(以及其中的级联错误)

    • 你的 RunReport 函数永远不会返回任何内容 .

    对于#2,您无法测试不存在的内容 . 因此,让您的函数向调用者返回某种成功/失败指示符 .

    对于#1,当您调用 GetResponse() 时,需要将其包装在try / catch块中,捕获异常,并退出函数并将相应的状态返回给调用者 .

    您可能还想查看有关使用SOAP方法调用SSRS的this SO post .

相关问题