从库中删除元素

时间:2018-01-24 19:46:40

标签: powershell sharepoint scripting

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

#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"
}

然而,我无法让它发挥作用。 URL工作,但客户变量,当我尝试Write-Host时它不显示任何内容。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果要从文档库中删除文档,请参阅下面的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()
  }
}