Search code examples
powershellloopsmappingretry-logic

How to handle a network mapping with retry and looping in powershell?


I would like to mapping a network. I need to use retry if the mapping fail. After maximum retry it will fail out and continue to another process. If the mapping pass, then continue to another process too. I've tried this way, but seems the retry is not working. Once the mapping fail, it does not do the network mapping process, only process the looping. Anyone can give me idea please.

$Stoploop = $false
[int]$Retrycount = "0"
do {
     try {
          
          $net = New-Object -ComObject WScript.Network                   
          $net.MapNetworkDrive("$Path", "\\$IP\$Folder", $False, "$Server\$user", "$pass")
          $Message = "NetWork Mapping : " + $Path + " Mount Successful"
          Write-Host $Message
          $Stoploop = $true
          CaptureLog "$Message`n "
     }
     catch {
          if ($Retrycount -eq 15){
          $Message_1 = "Could not mount after 15 retries." + " $_"
          $Stoploop = $true
          CaptureLog $Message_1
          ErrorHandling $Message_1
          }
          else {

               $Message = "NetWork Mapping : " + $Path + " Trying to mount..."
               Write-Host $Message
               CaptureLog $Message
               Start-Sleep -Seconds 3
               $Retrycount = $Retrycount + 1
          }
     }

}
while ($Stoploop -eq $false) {
}

Thank you so much for the advice. Appreciated!


Solution

  • Both comments are valid and useful (com object outside the loop and Get-PSDrive, New-PSDrive usage).

    Now, I tested your script and it should do what you expect. The only case where it misses the point is when the network drive is already in use. To catch this with minimum modifications, you can simple add this check before your loop even starts:

    if(Test-Path $Path) {
        # if you want to unmap the drive, use: $net.RemoveNetworkDrive($Path, $true) and remove the $Stoploop = $true
        $Message = "NetWork Drive " + $Path + " Already in use..."
        Write-Host $Message
        CaptureLog "$Message`n "
        $Stoploop = $true
    }
    

    Now, if you want to simplify your script, you can do something like this:

    $net = New-Object -ComObject WScript.Network
    $retrycount = 0
    
    if(Test-Path $Path) {
        # if you want to unmap the drive, use: $net.RemoveNetworkDrive($Path, $true) and remove the $retrycount = 20
        $Message = "NetWork Drive " + $Path + " Already in use..."
        Write-Host $Message
        CaptureLog "$Message`n "
        $retrycount = 20
    }
        
    while($retrycount -lt 15) {
        $retrycount += 1
        try {
            $net.MapNetworkDrive($Path, "\\$IP\$Folder", $False, "$Server\$user", $pass)
            $Message = "NetWork Mapping (try $($retrycount)) - $($Path) Mount Successful"
            Write-Host $Message
            CaptureLog "$Message`n "
            $retrycount = 100 # this will exit the loop and indicate successful mapping
        } catch {
            $Message = "NetWork Mapping (try $($retrycount)) - $($Path) Mount failed: $($_.Exception.Message.Replace("`n",''))"
            Write-Host $Message
            CaptureLog "$Message`n "
            Start-Sleep -Seconds 3
        }
    }