system.Net.WebRequest - powershell - 和'操作已经超时'

时间:2016-08-01 17:49:19

标签: .net powershell

我需要向powershell询问很多url,并检查状态代码

$Request = $null
$Request= [system.Net.WebRequest]::Create($Url)
$Request.Timeout = 100
$Request.AllowAutoRedirect = $true

 try {

       $Response = $Request.GetResponse()

     } catch [System.Net.WebException] {

       $someexception   = $_.Exception.Message 
     }

     $http_status      =  [int]$res.StatusCode

    if ([int]$Response.StatusCode.value__ -ne 200) #если не 200 - алерт
    {
        $state       = 'ERROR'


    }
        else
    {
        $state = 'OK' 


    }

$Request.Close

我试图在服务器上使用它的任何方式 - 它可以通过手动测试,但如果我运行了很多脚本(在M $ SCOM中进行监控) - 其中一些将失败并且操作已经超时

如果我要检查问题网址 - 没关系

为什么它可以在一次大量脚本的情况下超时?如何调试它?它甚至是来自服务器日志的http 200 ..

2 个答案:

答案 0 :(得分:1)

The timeout property that you're setting to 100 is in milliseconds

这很短。可能想把它设置为几秒钟。

这是因为服务器端花了比实际发送数据的时间更长的时间。它可能已发送200响应,但整个请求未在分配的时间内完成。如果您一次发送大量请求,那肯定是可能的。

答案 1 :(得分:1)

在请求创建解决问题之前,

[System.Net.ServicePointManager]::DefaultConnectionLimit = 1024

相关问题