Answer the question
In order to leave comments, you need to log in
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
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
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question