New-WebServiceProxy Not Responding and hanging my script

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 3 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
ClipperXTP
Posts: 55
Last visit: Thu Jun 24, 2021 3:05 am

New-WebServiceProxy Not Responding and hanging my script

Post by ClipperXTP »

Hi Guys

I am retrieving a table from a web server and using it to update a DataGridView in my GUI.

Occasionally, the web server becomes unresponsive and my script just hangs - the PowerShell Studio GUI just goes 'Not Responding'

I have tried putting the two lines of code in a try / catch block but the try doesn't recognize it as being unresponsive.

The code is as follows. When it is working fine, it takes about two seconds to update the datagridview:

$Groups = New-WebServiceProxy -uri http://GRH.server023.net/webservices/GRH.asmx
$assignments = $Groups.GetAssignmentsByComputer($($computer), "Yes").tables

$DatagridviewAssigments.datasource = $($Assignments)

As the try / catch wasn't working I went down the route of using a job with a timeout.
However the Job takes about 8 seconds to run the same two lines of code, which is real terms is a lot of waiting around for my users staring at the GUI

$job1 = Start-Job -ScriptBlock {$Groups = New-WebServiceProxy -uri http://GRH.server023.net/webservices/GRH.asmx

$Groups.GetAssignmentsByComputer($($args[0]), "Yes").tables

} -ArgumentList $computername


$Assignments = $Job1 | Wait-Job -Timeout 15 | Receive-Job

Can anyone suggest a better way of doing this?

Many thanks
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: New-WebServiceProxy Not Responding and hanging my script

Post by jvierra »

You can use Test-Connection to check the server. The Web link will timeout after about two minutes.
If the connection never times out then you will want to contact the webmaster and get the service fixed.

With come services asking for a large amount of data can cause the response to take minutes if not longer. This is not a failure as it may be what the service has been designed for. It can also be a result of a badly designed database.
ClipperXTP
Posts: 55
Last visit: Thu Jun 24, 2021 3:05 am

Re: New-WebServiceProxy Not Responding and hanging my script

Post by ClipperXTP »

Many thanks for the reply
Test-Connection doesn't help me if it times out after two mins.
My powershell job times out after 15 seconds when the server is unresponsive.
The job takes about 8 seconds when the server is its normal responsive self.
Note, the server works fine most days, maybe once every few weeks it grinds to a halt, I am trying to build resiliency in my script.
My question is can anyone think of an option that works fast than what I have done - the PS Job which is my resiliency, takes 8 seconds to run the two lines of code inside it, whereas the two lines of code outside the job take just two seconds?
Thanks
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: New-WebServiceProxy Not Responding and hanging my script

Post by jvierra »

Test-Connection doesn't time out it tests and returns if there is no response in a few milliseconds. It does not check if the data is available only if the server is alive. "Invoke-WebRequest" is what takes 2 minutes to time out if the server does not respond or does not use "keep-alives".


There is really no other way to solve your issue. Either test or use a job.
ClipperXTP
Posts: 55
Last visit: Thu Jun 24, 2021 3:05 am

Re: New-WebServiceProxy Not Responding and hanging my script

Post by ClipperXTP »

Ah OK, gotcha sorry I misread your email.

Yes, test-connection isn't really an option as other services work fine on server when this one is not responding.

Thanks for taking the time to look at it
This topic is 3 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked