Igor Kuznetsov
Igor Kuznetsov

Reputation: 415

system.Net.WebRequest - powershell - and 'The operation has timed out'

I need to ask a lot of url`s with powershell and check status code

$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

Any way i am trying to use it on server - its ok via manual test, but if i run a lot of scripts (in M$ SCOM for monitoring) - SOME of them will fail with The operation has timed out

If i will check problem url - it is ok

Why it can time out in case lot of scripts in one time?how to debug it? Its is even http 200 from server logs ..

Upvotes: 4

Views: 6646

Answers (2)

Igor Kuznetsov
Igor Kuznetsov

Reputation: 415

[System.Net.ServicePointManager]::DefaultConnectionLimit = 1024 before the request creation solved the problem.

Upvotes: 4

briantist
briantist

Reputation: 47772

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

That's pretty short. Might want to set it to a few seconds.

It happens because the server side took longer than that amount of time to actually send the data. It may have sent a 200 response but the entire request did not complete in the time allotted. Definitely possible if you're sending a lot of requests at once.

Upvotes: 1

Related Questions