2

I would like to mapping the network. I need to use retry if the mapping fail, and the maximum retry 5 times. I've tried this way, but I can't figure out how to add the maximum retry.

Do{
    Try{     
        $net = new-object -ComObject WScript.Network                   
        $net.MapNetworkDrive("$Directory", "\\IP\$Folder", $False, "$Server\$SQL", "$pass")
        $Message = "Mapping : " + $Directory + "Successful"
        Write-Host $Message
        
    }
    Catch{

         $Message= "Mapping : " + $Directory + " Fault" + " $_"
         Write-Host $Message
         # in here there is error handling.
         CallErrorHandlingFunction
    }
}While($? -ne $true)

# in here there is next process after network mapping  succesfull.
CallNextProcess

Anyone can help really appreciated. Thanks

Cheries
  • 834
  • 1
  • 13
  • 32

1 Answers1

3

There are many ways to approach this, here is one using a script block, note that this example only works because you're using Write-Host which's outputs goes to the Information Stream and it's output is not captured unless redirected (6>&1).

$action = {
    Try
    {
        $net = New-Object -ComObject WScript.Network                   
        $net.MapNetworkDrive(
            "$Directory", "\\IP\$Folder", $False, "$Server\$SQL", "$pass"
        )
        $Message = "Mapping : " + $Directory + "Successful"
        Write-Host $Message
        $true # => if everything goes right $result = $true
    }
    Catch
    {
        $Message = "Mapping : " + $Directory + " Fault" + " $_"
        Write-Host $Message
        $false # => if fails $result = $false
    }
}

$maxRetries = 5

do { $result = & $action }             # do this
until (-not --$maxRetries -or $result) # until $result is True OR
                                       # $maxRetries reaches 0

Honestly a much easier alternative:

$maxRetries = 5

1..$maxRetries | ForEach-Object {
    if( & $action ) { break } # => if action = True stop the loop
}
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • hi thank you @Santiago Squarzon, but it still endless loop. – Cheries Dec 21 '21 at 04:05
  • @Cheries both alternatives are working for me, I did some edits you may be using an old copy of the code. – Santiago Squarzon Dec 21 '21 at 04:09
  • @Cheries the `Write-Host $Message` on the catch block is not captured so you should see it on the console same if you were using `Write-Warning $_.Exception.Message` – Santiago Squarzon Dec 21 '21 at 04:11
  • HI @Santiago Squarzon, I found a problem, I use the second way of your answer, but my expectation is, if the action is true, I don't need to stop the process, because I have another process afer mapping the network. And the second problem, I have another process if the mapping fail, which error handling, the error handling will process 5 times same as the total retry, but my expectation, the error handling will process after retry done, not in looping. I hope you can help me to figure it out. Thanks – Cheries Dec 21 '21 at 07:03
  • @Cheries _"I don't need to stop the process, because I have another process afer mapping the network."_: put all the code inside that if and leave the `break` at the end. _"but my expectation, the error handling will process after retry done, not in looping."_ this should've been clarified in your question. This answer was posted to solve your initial question. – Santiago Squarzon Dec 21 '21 at 12:53
  • Thanks for help. I create another question in here for my update question. If you don't mind, please check it out. Thanks a lot https://stackoverflow.com/q/70574082/11076819 – Cheries Jan 04 '22 at 05:34