PowerShell - Enumerating through a collection and modifying a collection

How can I fix this script?

Yes, I am changing the collection in the foreach loop, and this is the cause of this error.

An error occurred while enumerating through the collection: the collection was modified; enumeration operation cannot be performed. In C: \ Users \ user \ Documents \ PowerShell \ ChangeAllListsV2.ps1: 47 char: 20 + foreach <<($ list in $ webLists) + CategoryInfo: InvalidOperation: (Microsoft.Share ... on + SPEnumerator: SPEnumerator) [ ], RuntimeException + FullyQualifiedErrorId: BadEnumeration

#Script change in all lists the required field property "testfield" to false #Part 0 - Configuration $urlWebApp = "http://dev.sharepoint.com" $countFound = 0 $countList = 0 $countFoundAndChange = 0 #Part 1 - PreScript $snapin = Get-PSSnapin | Where-Object {$_.Name -eq "Microsoft.SharePoint.Powershell"} if ($snapin -eq $null) { Write-Host "Loading SharePoint Powershell" Add-PSSnapin Microsoft.SharePoint.Powershell } #Part 2 - Script $webApp = Get-SPWebApplication $urlWebApp #$webApp | fl $webAppSites = $webApp.sites foreach($site in $webAppSites) { Write-Host "***********************************************************************" Write-Host "Found site: " $site -foreground blue $siteAllWebs = $site.AllWebs foreach($web in $siteAllWebs) { Write-Host "Found web: " $web -foreground blue #$web | fl $webLists = $web.Lists foreach($list in $webLists) { $countList ++ Write-Host "Found list: " $list -foreground blue #Change list property $field = $Null $field = $list.Fields["testfield"] if($field){ Write-Host "Field found: " $list -foreground green #Write-Host "in web: " $web -foreground green $countFound ++ try{ if($field.Required) { ####################################################### $field.Required = $False $field.Update() ####################################################### $field = $Null Write-Host "Done!: Change list: " $list -foreground green $countFoundAndChange ++ }else{ Write-Host "Already!: Change list: " $list -foreground green } } catch{ $field = $Null Write-Host "Error!: Change list: " $list -foreground red Write-Host "in web: " $web -foreground red $_ } } } } } Write-Host "Found lists: " $countList Write-Host "Found lists with column [testfield]: " $countFound Write-Host "Change lists with column [testfield]: " $countFoundAndChange 
+7
source share
3 answers

SPListCollection tends to change the collection when updating its properties (fields, event receivers, etc.). You can use for-loop instead:

 for ($i = 0; $i -lt $webLists.Count; $i++) { $list = $web.Lists[$i]; # ... } 
+20
source

You can try to copy the collection that you are currently iterating into another collection (array or list), and then iterate over the new collection.

Something like that:

 $collection = @(1, 2, 3, 4) $copy = @($collection) $collection[0] = 10 $collection -join " " $copy -join " " 

The above code gives the following output:

 10 2 3 4 1 2 3 4 

Note that the $copy variable belongs to a different collection.

+3
source

Check out: http://soreddymanjunath.blogspot.in/2014/07/collection-was-modified-enumeration.html

Here is anonther example for the same problem

  if($web.IsMultilingual -eq $true ) { foreach($cul in $web.SupportedUICultures) { if($cul.LCID -ne $webCul.LCID -and $cul.LCID -ne "1033") { $web.RemoveSupportedUICulture($cul) } } $web.Update() } 

the first time it goes through the foreach loop, it will remove the supported culture for frist time, when it comes to the loop for the second iteration, then it will throw you an exception "Collection has been changed, the enumeration operation cannot be performed",

The solution to the above problem is to save the values ​​changed in the Arraylist and try to change, which will fix the problem. Here I save the Arraylist called enumcul and inserting values ​​into it and changing it ...

 $enumcul=New-Object Collections.ArrayList $i=0 if($web.IsMultilingual -eq $true ) { foreach($cul in $web.SupportedUICultures) { if($cul.LCID -ne $webCul.LCID -and $cul.LCID -ne "1033") { $enumcul.Insert($i, $cul) $i=$i+1 } } foreach( $k in $enumcul) { $web.RemoveSupportedUICulture($k) $web.Update() } 
0
source

All Articles