-3

我的共享点站点列表中有太多项目,我需要从中删除 10000 个文件。如何使用 powershell 脚本来做到这一点。

4

1 回答 1

0

创建查询:

$list =  (Get-Spweb http://devmy101).GetList("http://devmy101/Lists/smarEnteredTerritorialWaters")
$query = New-Object Microsoft.SharePoint.SPQuery;
$query.ViewAttributes = "Scope='Recursive'";
$query.RowLimit = 2000;
$query.Query = '<Where><Gt><FieldRef Name="Created"/><Value Type="DateTime" IncludeTimeValue="TRUE">2013-07-10T14:20:00Z</Value></Gt></Where>';

构建命令(请注意,查询仅限于一次返回 2000 个项目,并使用 ListItemCollectionPosition 属性继续分批检索 2000 个项目,直到查询完所有项目。有关详细信息,请参阅此 MSDN 文档。)

$itemCount = 0;
$listId = $list.ID;
[System.Text.StringBuilder]$batchXml = New-Object "System.Text.StringBuilder";
$batchXml.Append("<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch>");
$command = [System.String]::Format( "<Method><SetList>{0}</SetList><SetVar Name=`"ID`">{1}</SetVar><SetVar Name=`"Cmd`">Delete</SetVar></Method>", $listId, "{0}" );

do
{
    $listItems = $list.GetItems($query)
    $query.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
    foreach ($item in $listItems)
    {
        if($item -ne $null){$batchXml.Append([System.String]::Format($command, $item.ID.ToString())) | Out-Null;$itemCount++;}
    }
}
while ($query.ListItemCollectionPosition -ne $null)

$batchXml.Append("</Batch>");
$itemCount;

最后(也是最重要的!),运行查询

$web = Get-Spweb http://inceweb/HKMarineDB;
$web.ProcessBatchData($batchXml.ToString()) | Out-Null;

您将希望以管理员身份在 SharePoint 命令行管理程序中运行它。这是 Matthew Yarrett 的博客文章中的逐字记录。我在这里发布了大部分帖子,以防他的博客消失。http://matthewyarrett.blogspot.com/2013/07/well-that-was-fun-bulk-deleting-items.html

于 2019-05-13T14:18:03.347 回答