Sharepoint Powershell Online - 按日期删除项目

时间:2018-04-06 06:44:13

标签: powershell sharepoint-online

您好我在执行此操作时遇到此错误。你能帮我吗?

foreach($item in $items)
{       
   $created = $item["Created"]
   if ($created -lt $days)
   { 
      Write-Host  "Title - " $item["Title"] "Created - " $item["Created"]

      $test = $item["ID"]
      write-host $test
      $item["ID"].DeleteObject() 
      #$item[].DeleteObject() 
   }
}   

$context.ExecuteQuery()
  

方法调用失败,因为[System.Int32]不包含方法   命名为“DeleteObject”。   在F:\ JoshScript \ deletebydate.ps1:46 char:2   + $ item [“ID”]。DeleteObject()   + ~~~~~~~~~~~~~~~~~~~~~~~~~~       + CategoryInfo:InvalidOperation:(:) [],RuntimeException       + FullyQualifiedErrorId:MethodNotFound

1 个答案:

答案 0 :(得分:1)

我建议您查看SharePoint PnP PowerShell cmdlet,它们可以让您轻松管理大量小任务。

这是使用cmdlet的相同脚本

Connect-PnPOnline -Url <tenant>.sharepoint.com

$listName = "Shared Documents"
$items = Get-PnPListItem -List $listName -Fields "Title","Created","ID","GUID"
$days = (Get-Date).AddDays(-7)

foreach($item in $items) 
{
    $created = $item["Created"]

    Write-Host "Item created on $created"
    if($created -gt $days) 
    {
        continue;
    }

    try
    {
        Remove-PnPListItem -List $listName -Identity $item.Id -Force -ErrorAction Stop
    }
    catch
    {
        Write-Host "Unable to delete $($item.Id) in list $listName"
    }
}

如果您运行的是Windows 10,则只需运行

即可
Install-Module -Name SharePointPnPPowerShellOnline

它将从here下载并安装它们。