1

I have written a powershell script which downloads files from sharepoint using PNP. This works fine however I am trying to do some error catching when the file does not exist add it to a csv however the error is not getting caught. code below:

try{
        Get-PnPFile -Url $fileUrl -Path $fullExportPath -FileName $fileName -AsFile 

    }catch [Microsoft.SharePoint.Client.ServerException]
        {             
            Write-Output "File not found: skipping to next list item...";

            $failedDocItem = New-Object System.Object;
            $failedDocItem | Add-Member -MemberType NoteProperty -Name Absolute_Url -Value $fileUrl;
            $failedDocItem | Add-Member -MemberType NoteProperty -Name SBI -Value $sbiFolderName;

            $failedDocItems += $failedDocItem;

            continue;
        }
        $error[0].Exception.GetType().FullName
         #write out CSV of failed XML Items (XML files not found in SharePoint)
    $outputFailedItemsCSVfilePath = $logFileDirectory + "\" + "Failed_MapFiles_Log_Output_" + $logfileDateStr + ".csv";
    $failedDocItems | Export-Csv -NoTypeInformation -Path $outputFailedItemsCSVfilePath -Verbose;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.