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

标签 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()
}

最佳答案

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

为了从后面修复这个迭代项目:

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

关于powershell - 为什么这个 powershell for 循环在 4 次迭代后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28886941/

相关文章:

powershell - Start-Job 输出去哪里了?

sharepoint - 是否可以在 sharepoint 2010 中获取当前日期?

python - 如何在这个素数筛子的 for 循环中花费更少的时间?

python - 如何隔离 python 'for loop' 中函数的计算以避免广播形状冲突?

windows - 如何在 PowerShell 中获取用户环境变量?

powershell - 使用 Powershell 关闭特定的 Excel 文件

powershell - 针对 Azure 应用服务运行自定义脚本扩展

powershell - 设置权限/共享子文件夹以使用PowerShell将库分组

c# - SharePoint 代码中的内存泄漏?

javascript - 在 JS for 循环中对记录进行排序