PowerShell - Enumerating through a collection and change the collection
Asked Answered
S

5

11

How it is posible to fix this script?

Yes, I´m changing the collection in the foreach loop and this is the reason for this error.

An error occurred while enumerating through a collection: Collection was modified; enumeration operation may not execute.. At 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
Siegfried answered 25/1, 2012 at 15:31 Comment(0)
R
22

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

for ($i = 0; $i -lt $webLists.Count; $i++)
{
  $list = $web.Lists[$i];
  # ...
}
Resonator answered 25/1, 2012 at 15:47 Comment(0)
S
8

I know this is a pretty old thread. This is for anybody ending up to this page looking for an answer.

The idea is, like other answers suggest, to copy the collection (using the clone() method) to another and iterate "another" and modify the original variable inside the loop without having to use for in place of foreach:

A collection of type ArrayList:

[System.Collections.ArrayList]$collection1 = "Foo","bar","baz"
$($collection1.Clone()) | foreach {
$collection1.Remove("bar")
}

Output:

PS H:\> $collection1
Foo
baz

A collection of type Hashtable:

[System.Collections.Hashtable]$collection2 = @{
        "Forum" = "Stackoverflow"
        "Topic" = "PowerShell"
        }

$($collection2.Clone())| foreach {
$collection2.Remove("Forum")
}

Output: PS H:> $collection2

Name                           Value                                                              
----                           -----                                                              
Topic                          PowerShell                                                         

And, a basic array:

[System.Array]$collection3 = 1, 2, 3, 4
$($collection3.Clone()) | foreach {
$collection3[$collection3.IndexOf($_)] = 10
}

Output:

PS H:\> $collection3
10
10
10
10

As long as your collection is not of fixed size.

Stinkstone answered 16/10, 2020 at 17:10 Comment(0)
T
4

You can try copying the collection you're currently iterating on to another collection (an array or a list) and then iterate on that new collection.

Something like this:

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

The code above gives the following output:

10 2 3 4
1 2 3 4

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

Tipper answered 25/1, 2012 at 15:39 Comment(4)
You mean like $webAppSites = $webApp.sites ? I copy every collection and after this I´m using the foreachSiegfried
@LaPhi: No, not like that. What you're talking about is just an assignment, not copying. I updated my answer to show how an array is copied.Tipper
A deep copy of an entire site isn't the best of ideas. A regular 'for' loop is the way to go.Escalate
@Escalate I didn't suggest deep copying.Tipper
D
0

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

Here is anonther example for same issue

    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()
  }

for the first time it will go through the loop foreach will remove supported culture for frist time, when it comes to loop for the second iteration then it will throw you the exception “Collection was modified; enumeration operation may not execute”,

Solution to Above problem is to Store to values to modified in a Arraylist and try to modify which will fix the problem, Here i am storing Arraylist called enumcul and inserting values into it and modifying 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()
 }
Dermoid answered 3/7, 2014 at 4:16 Comment(0)
S
0

The fastest way I found for small number of items is

$bar = @{ 
   one = "1"
   two = "2"
}
foreach($foo in $($bar.Keys){
    $bar[$foo] = Read-Host -Prompt "new value is"
}

Wrap the collection in $($collection.Keys).

Sulphide answered 28/8, 2023 at 9:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.