F
F
FlyingBrick2014-02-14 14:24:47
PowerShell
FlyingBrick, 2014-02-14 14:24:47

Powershell: how to make several attempts to connect to the server and only then throw an exception?

Often such a task arises when something can interfere with its execution, and several attempts must be made before "spitting" with an exception. As an example of a connection to SSRS:

$Proxy = New-WebServiceProxy -Uri $Uri -Namespace SSRS.ReportingService2010 -Credential $credentials

I usually write a bicycle:
for($i = 0; $i -lt 5; $i++) #try 5 times
            {
                try
                    {
                        $Proxy = New-WebServiceProxy -Uri $Uri -Namespace SSRS.ReportingService2010 -Credential $credentials
                    }
                catch
                    {
                        if($i -eq 4)
                            {
                                Throw $_.Exception.Message
                            }
                        Start-Sleep -s 5 #pause on 5 second
                    }
            }

Maybe there is a more elegant solution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ace_foster, 2015-11-16
@ace_foster

In fact, it is quite a normal design.
Just try to use the K&R style when writing code. Reduce the number of lines and improve readability

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question