为什么此powershell for循环在4次迭代后停止

为什么此powershell for循环在4次迭代后停止,powershell,sharepoint,for-loop,Powershell,Sharepoint,For Loop,我知道我必须删除8个列表自定义列表和库。下面只删除4,然后我必须再次执行,它删除2,然后删除1,然后删除1。知道为什么吗 $site = "http://inside.comp.com/sales" $featureID = "b432665a-07a6-4cc7-a687-3e1e03e92b9f" $str = "ArcGIS" Disable-SPFeature $featureID -Url $site -Confirm:$False start-sleep -seconds 5 $si

我知道我必须删除8个列表自定义列表和库。下面只删除4,然后我必须再次执行,它删除2,然后删除1,然后删除1。知道为什么吗

$site = "http://inside.comp.com/sales"
$featureID = "b432665a-07a6-4cc7-a687-3e1e03e92b9f"
$str = "ArcGIS"
Disable-SPFeature $featureID -Url $site -Confirm:$False
start-sleep -seconds 5
$site = get-spsite $site
foreach($SPweb in $site.AllWebs)
{   
    for($i = 0; $i -lt $SPweb.Lists.Count; $i++)
    {
        $spList = $SPweb.Lists[$i]
        if($SPList.Title.Contains($str))    
        { 
            write-host "Deleting " $SPList.Title -foregroundcolor "magenta"
            $spList.Delete()            
            #$SPweb.Update()
        }
    }
    $SPweb.Update()
}

这是因为当您删除列表中的项目0时,列表项目1将变为0,并且在此次运行中将跳过该项目。然后同样的事情再重复3次,只删除3项

要修复此问题,请从后面迭代项目:

for($i = $SPweb.Lists.Count - 1; $i -ge 0; $i--)
{
    # The rest of the cycle
}

哇!工作得很有魅力。非常感谢你。