如何修复此脚本?
是的,我在foreach循环中更改了集合,这就是导致此错误的原因。
枚举集合时发生错误:集合已修改;枚举操作可能无法执行.. 在C:\ Users \ user \ Documents \ PowerShell \ ChangeAllListsV2.ps1:47 char:20 + foreach<<<< ($ webLists中的$ list) + 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
答案 0 :(得分:20)
SPListCollection
倾向于在更新其属性(字段,事件接收器等)时修改集合。您可以改为使用for循环:
for ($i = 0; $i -lt $webLists.Count; $i++)
{
$list = $web.Lists[$i];
# ...
}
答案 1 :(得分:3)
您可以尝试将当前正在迭代的集合复制到另一个集合(数组或列表),然后迭代该新集合。
这样的事情:
$collection = @(1, 2, 3, 4)
$copy = @($collection)
$collection[0] = 10
$collection -join " "
$copy -join " "
上面的代码给出了以下输出:
10 2 3 4
1 2 3 4
请注意,$copy
变量指的是不同的集合。
答案 2 :(得分:0)
检查:http://soreddymanjunath.blogspot.in/2014/07/collection-was-modified-enumeration.html
以下是同一问题的另一个例子
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()
}
第一次它将通过循环foreach将删除支持的文化为第一次迭代,然后它会抛出异常“收集被修改;枚举操作可能不会执行“,
解决上述问题是将存储到要在Arraylist中修改的值并尝试修改哪个将解决问题,在这里我存储名为enumcul的Arraylist并将值插入其中并修改它...
$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()
}
答案 3 :(得分:0)
我知道这是一个很老的话题。这是给最终进入此页面寻找答案的任何人的。
这个想法是,像其他答案所建议的那样,将集合(使用 clone()方法)复制到另一个并迭代“另一个”并在循环内修改原始变量,而不必使用 for 代替 foreach :
ArrayList类型的集合:
[System.Collections.ArrayList]$collection1 = "Foo","bar","baz"
$($collection1.Clone()) | foreach {
$collection1.Remove("bar")
}
输出:
PS H:\> $collection1
Foo
baz
Hashtable类型的集合:
[System.Collections.Hashtable]$collection2 = @{
"Forum" = "Stackoverflow"
"Topic" = "PowerShell"
}
$($collection2.Clone())| foreach {
$collection2.Remove("Forum")
}
输出: PS H:> $ collection2
Name Value
---- -----
Topic PowerShell
还有一个基本数组:
[System.Array]$collection3 = 1, 2, 3, 4
$($collection3.Clone()) | foreach {
$collection3[$collection3.IndexOf($_)] = 10
}
输出:
PS H:\> $collection3
10
10
10
10
只要您的收藏不是固定大小的。