gpt4 book ai didi

sharepoint - PowerShell-枚举集合并更改集合

转载 作者:行者123 更新时间:2023-12-04 01:46:47 26 4
gpt4 key购买 nike

如何修复此脚本?

是的,我正在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

最佳答案

SPListCollection倾向于在更新其属性(字段,事件接收器等)时修改集合。您可以改用for循环:

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

关于sharepoint - PowerShell-枚举集合并更改集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9005323/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com