Papierkörbe einer WebApplication Auflisten & Löschen

​Zur “Bereinigung” einer WebApplication kann man dann- und wann mal die Paprierkörbe ansehen und-/oder löschen.

Ich habe da mal ein keines Skript dazu Vorbereitet:

<#
.SYNOPSIS
List or Remove elements from RecycleBin(s) of a WebApplication
.DESCRIPTION
This script lists- and optionally removes all items from all RecycleBins
of a WebApplication, including the End-Users (1st Stage) and
Administrators (2nd Stage) RecycleBins.
.NOTES
File Name : Delete-Site-Recycle-Bin.ps1
Author : Nils Andresen - nils@nils-andresen.de
.Example
.\Empty-SPRecycleBin.ps1 -WebApp http://sp.dev/ -FirstStageCleanup RemovePermanent -SecondStageCleanup RemovePermanent
Removes all deleted items (1st and 2nd stage) from all Sites/Webs of the WebApplication
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$True, HelpMessage = "Url to the WebApp")]
[string]$WebApp,
[Parameter(HelpMessage = "What to do with the 1st-stage Recycle Bins")]
[ValidateSet("ListOnly", "MoveTo2nd", "RemovePermanent")]
[string]$FirstStageCleanup = "ListOnly",
[Parameter(HelpMessage = "What to do with the 2nd-stage Recycle Bins")]
[ValidateSet("ListOnly", "RemovePermanent")]
[string]$SecondStageCleanup = "ListOnly"
)
if((Get-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
$Global:TotalRemovedSize = 0;
function Format-ForPc {
[CmdletBinding()]
param(
[Parameter(Position=0,
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[int]$size
)
if($size -lt 1MB) {
return "{0:0.0#}KB" -f ($size / 1KB);
}
if($size -lt 1GB) {
return "{0:0.0#}MB" -f ($size / 1MB);
}
if($size -lt 1TB) {
return "{0:0.0#}GB" -f ($size / 1GB);
}
return "{0:0.0#}TB" -f ($size / 1TB);
}
function Process-Web {
[CmdletBinding()]
param(
[Parameter(Position=0,
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[Microsoft.SharePoint.SPWeb[]]$web
)
Process {
Write-Verbose "Accessing Web: $($web.Url)";
if((-not $web.RecycleBinEnabled) -or ($web.RecycleBin.Count -lt 1)) {
return;
}
$bin = ,$web.RecycleBin; # mind the "," - no unrolling here...
$size = 0;
$bin | %{ $size += $_.Size }
Write-Output "Web $($web.Title) has $($bin.Count) items ($($size | Format-ForPc)) in Users-RecycleBin";
switch ($FirstStageCleanup) {
"ListOnly" {
$bin | %{ Write-Output "- $($_.ItemType):$($_.Title) ($($_.Size | Format-ForPc), Deleted by $($_.DeletedByName))" }
}
"RemovePermanent" {
$bin.DeleteAll();
$Global:TotalRemovedSize += $size;
Write-Output "- Deleted permanently";
}
"MoveTo2nd" {
$bin.MoveAllToSecondStage();
Write-Output "- Moved to second stage";
}
}
}
}
function Process-Site {
[CmdletBinding()]
param(
[Parameter(Position=0,
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[Microsoft.SharePoint.SPSite[]]$site
)
Process {
Write-Verbose "Accessing Site: $($site.Url)";
$secondStage = $site.RecycleBin | ? { $_.ItemState -eq [Microsoft.SharePoint.SPRecycleBinItemState]::SecondStageRecycleBin }
$site.AllWebs | Process-Web
if($secondStage.length -lt 1) {
return;
}
$size = 0;
$secondStage | %{ $size += $_.Size }
Write-Output "Site $($site.Title) ($($site.Url)) has $($secondStage.Length) items ($($size | Format-ForPc)) in Admin-RecycleBin";
switch ($SecondStageCleanup) {
"ListOnly" {
$secondStage | %{ Write-Output "- $($_.ItemType):$($_.Title) ($($_.Size | Format-ForPc), Deleted by $($_.DeletedByName))" }
}
"RemovePermanent" {
$secondStage | %{ $_.Delete(); }
$Global:TotalRemovedSize += $size;
Write-Output "- Deleted permanently";
}
}
}
}
$sa = Start-SPAssignment
$w = Get-SPWebApplication $WebApp -AssignmentCollection $sa;
$w.Sites | Process-Site;
Stop-SPAssignment $sa
if($SecondStageCleanup -eq "RemovePermanent" -and $FirstStageCleanup -eq "MoveTo2nd") {
Write-Warning "The selected combination of removing from second stage and moving from first to second possibly leaves items undeleted."
}
if($Global:TotalRemovedSize -gt 0) {
Write-Output "$($Global:TotalRemovedSize | Format-ForPc) were removed permanently.";
}