Answered by:
Error Copying Files From Library

Question
-
I'm working on a script that will copy files from a SP library to a file share, then delete the files from the library. The script works great, except for one spot. It will copy the first document from the library and delete, but then throws an exception:
An error occurred while enumerating through a collection: Collection was modified; enumeration operation may not execute..
At C:\Scripts\AuditLogArchive\Archive-AuditLogs.ps1:47 char:16
+ foreach <<<< ($file in $files)
+ CategoryInfo : InvalidOperation: (Microsoft.Share...on+SPEnumerator:SPEnumerator) [], RuntimeException
+ FullyQualifiedErrorId : BadEnumerationHere is how I'm doing it:
foreach ($site in $sites) { try { $web = Get-SPWeb $site.Url #$site.Dispose() $lib = $web.GetFolder("Audit Logs") if ($lib.ItemCount -ne 0) { Write-Host "Archiving Site: " $site.Url $files = $lib.Files } else { #Write-Host "Nothing to archive" Continue } } catch { Write-Host "Unable to get files from audit log library" Continue } # Download files from SharePoint to TemPath foreach ($file in $files) { Write-Host $file.Name $bytes = $file.OpenBinary() $temp = "$tempPath\"+$file.Name try { $stream = New-Object System.IO.FileStream($temp, "Create") $writer = New-Object System.IO.BinaryWriter($stream) $writer.write($bytes) } catch { Write-Host "Unable to copy "$file.Name" to $temp" Write-Host $_.Exception.Message } finally { $writer.Close() }
}
Any ideas what I'm doing wrong?
Wednesday, August 24, 2016 3:46 PM
Answers
-
Hello,
This is happening because you are trying to modify the collection while still enumerating.
I suggest you, don't delete under the same loop.
Instead use some variables to store references and then delete them
For anything related to SharePoint https://www.fiverr.com/rashu268
- Marked as answer by JedLO Friday, September 2, 2016 1:32 PM
Wednesday, August 24, 2016 5:57 PM
All replies
-
Hello,
This is happening because you are trying to modify the collection while still enumerating.
I suggest you, don't delete under the same loop.
Instead use some variables to store references and then delete them
For anything related to SharePoint https://www.fiverr.com/rashu268
- Marked as answer by JedLO Friday, September 2, 2016 1:32 PM
Wednesday, August 24, 2016 5:57 PM -
Hi JedLO,
For troubleshooting the issue, you could check things below:
1. Check if the brackets are matching in your script.
2. Use a for-loop instead of foreach.
Best regards,
Sara Fan
TechNet Community Support
Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.- Edited by Sara Fan Thursday, August 25, 2016 10:36 AM
- Proposed as answer by Victoria Xia Friday, September 2, 2016 9:09 AM
Thursday, August 25, 2016 6:51 AM -
Hi JedLO,
How is everything going?
Is there anything update about this issue?
If the reply is helpful to you, you could mark the reply as answer. Thanks for your understanding.
Best regards,
Sara Fan
TechNet Community Support
Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.Tuesday, August 30, 2016 1:13 AM -
That did the trick. Using one loop to collect the item id's and another to copy the items doesn't throw an exception. Thanks!Friday, September 2, 2016 1:34 PM
-
Hi JedLO,
It is very happy that you have resolved the problem.
Thank you for your sharing and it will help others have the same issue.
Best regards,
Sara FanPlease remember to mark the replies as an answers if they help and unmark them if they provide no help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.comMonday, September 5, 2016 12:24 AM