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

use*_*590 3 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
$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()
}
Run Code Online (Sandbox Code Playgroud)

dot*_*tom 5

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

为了从后面修复此迭代项:

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