【问题标题】:Sharepoint Powershell Online - Delete items by dateSharepoint Powershell Online - 按日期删除项目
【发布时间】:2018-04-06 06:44:13
【问题描述】:

您好,执行此操作时出现此错误。你能帮帮我吗?

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] 不包含方法 命名为“删除对象”。 在 F:\JoshScript\deletebydate.ps1:46 char:2 + $item["ID"].DeleteObject() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound

【问题讨论】:

  • 你试过$item.DeleteObject()吗?您可以添加创建$items 的行吗?它是什么类型/对象?
  • 是的,我也试过了,但它只删除了第一项。
  • 删除 listitems.Title - 测试已创建 - 2018 年 4 月 2 日上午 7:05:58 2 集合已修改。枚举操作可能无法执行。在 F:\JoshScript\deletebydate.ps1:34 char:10 + foreach($item in $items) + ~~~~~~ + CategoryInfo : OperationStopped: (:) [], InvalidOperationException + FullyQualifiedErrorId : System.InvalidOperationException 项目是从列表中删除。

标签: powershell sharepoint-online


【解决方案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下载并安装它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 2020-09-07
    • 1970-01-01
    • 2018-10-16
    • 2020-09-17
    • 1970-01-01
    • 2023-03-19
    • 2017-12-23
    相关资源
    最近更新 更多