从库中删除元素

时间:2022-08-27 12:32:53

I have a library in a list which contains a document I want to delete. This has to be done for site collections, so I decided to make a script do it.

我在列表中有一个库,其中包含我要删除的文档。这必须为网站集完成,所以我决定编写一个脚本。

#Set the Error Action

$ErrorActionPreference = "Stop"

Try{
    $customers = $SiteURL.GetFolder
    foreach ($customer in $customers){
        #customer is a list that contains the library sitepages
        $customerlists = $customer.List.TryGetList("Site Pages")
        #correctitem is the document item that I want to remove
        $correctitem = $customerlists.List.TryGetList("123")
        if ($correctitem){
        #delete it (havent gotten here yet)
        }
        }
        }
catch {
    Write-Host $_.Exception.Message -ForegroundColor Red
}
finally {
    #Reset the Error Action to Default
    $ErrorActionPreference = "Continue"
}

However, I cant get it to work. The URL works, but the customers variable, when I try to Write-Host it does not display anything. Any ideas?

但是,我不能让它工作。 URL工作,但客户变量,当我尝试Write-Host时它不显示任何内容。有任何想法吗?

1 个解决方案

#1


0  

If you want to delete a document from a document library, the PowerShell script below for your reference:

如果要从文档库中删除文档,请参阅下面的PowerShell脚本以供参考:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = new-object Microsoft.SharePoint.SPSite("http://sharepoint-site-url")
$web = $site.openweb()
$list=$web.Lists["Site Pages"]
$listItems = $list.Items
$listItemsTotal = $listItems.Count
Write-Host $listItemsTotal
for ($x=$listItemsTotal-1;$x -ge 0; $x--)
{
  if($listItems[$x].name.Contains("123")) # file refers to the name of the document
  {
    Write-Host("DELETED: " + $listItems[$x].name)
    $listItems[$x].Delete()
  }
}

#1


0  

If you want to delete a document from a document library, the PowerShell script below for your reference:

如果要从文档库中删除文档,请参阅下面的PowerShell脚本以供参考:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = new-object Microsoft.SharePoint.SPSite("http://sharepoint-site-url")
$web = $site.openweb()
$list=$web.Lists["Site Pages"]
$listItems = $list.Items
$listItemsTotal = $listItems.Count
Write-Host $listItemsTotal
for ($x=$listItemsTotal-1;$x -ge 0; $x--)
{
  if($listItems[$x].name.Contains("123")) # file refers to the name of the document
  {
    Write-Host("DELETED: " + $listItems[$x].name)
    $listItems[$x].Delete()
  }
}