Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell - Enumerating through a collection and change the collection

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
like image 855
LaPhi Avatar asked Jan 25 '12 15:01

LaPhi


3 Answers

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];
  # ...
}
like image 113
Stefan Avatar answered Nov 19 '22 22:11

Stefan


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.

like image 6
Niranjan Raguraman Avatar answered Nov 19 '22 21:11

Niranjan Raguraman


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.

like image 4
Gebb Avatar answered Nov 19 '22 20:11

Gebb