creating folder in PowerShell with try and catch

  • 1
  • Problem
  • Updated 9 months ago

Sometimes when creating folder i'm getting error message

Send-SfRequest : InternalServerError: A system error occurred.
At C:\Scripts\Sharefile\Sharefile_Copy_Directory\Copy Folder (v2 do not use).ps1:63 char:35

  • ... $folder = Send-SfRequest -Client $sfClient -Entity Items -Method PO ...
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (Items:String) [Send-SfRequest], Exception
    • FullyQualifiedErrorId : InternalServerError,ShareFile.Api.Powershell.SendSfRequest

I tried to build script which will create folder and retry when fail however script still throwing the same error. Any ideas why?

            $retries = 5 
            $secondsDelay = 2
            $retrycount = 0
            $completed = $false

             while (-not $completed) {
                try {
                    $folder = Send-SfRequest -Client $sfClient -Entity Items -Method POST -Id $rootTarget -Navigation Folder -BodyText $folderInfo
                    Write-Host ("Recreating folder: ", $item.Name,   " successful")
                    $completed = $true
                } catch {
                    if ($retrycount -ge $retries) {
                        Write-Host ("Recreating folder failed the maximum number of {1} times." -f $retrycount)
                        throw
                    } else {
                        Write-Host ("Recreating folder failed. Retrying in {1} seconds." -f $secondsDelay)
                        Start-Sleep $secondsDelay
                        $retrycount++
                    }
                } 
            } 
Photo of Lucas Slowik

Lucas Slowik

  • 1 Post
  • 0 Reply Likes

Posted 9 months ago

  • 1

Be the first to post a reply!