Share via


Automating SharePoint 2010 Administration with PowerShell: Cleaning up Central Admin Backup with PowerShell

How about getting rid of all those Backups that has failed? They are just sitting there doing nothing useful anyway.

Here is what my Backup and Restore History page looks like…

clip_image002

What this gives me is a bunch of SPHistoryObject that have failed:

$xmldata = [xml](Get-Content 'C:\SPBackup\spbrtoc.xml')
$xmldata.SPBackupRestoreHistory.SPHistoryObject | Where-Object {$_.SPErrorCount -gt '0'}

Now let’s store it in the variable $BadBackups and clean it …

$BadBackups | % { $xmldata.SPBackupRestoreHistory.RemoveChild($_) } # this removes the xml Nodes
$BadBackups | % { Remove-Item $_.SPBackupDirectory -Recurse } # This deletes those directories
$xmldata.Save('C:\SPBackup\spbrtoc.xml') # Save the file

Voila! All failed backups have been removed

clip_image004

Here is the full script

    1: Clear-Host
    2:  
    3: # Start Loading SharePoint Snap-in
    4: $snapin = (Get-PSSnapin -name Microsoft.SharePoint.PowerShell -EA SilentlyContinue)
    5:  
    6: IF ($snapin -ne $null){write-host -f Green "SharePoint Snap-in is loaded... No Action taken"}
    7: ELSE  {
    8: write-host -f Yellow "SharePoint Snap-in not found... Loading now"
    9: Add-PSSnapin Microsoft.SharePoint.PowerShell
   10: write-host -f Green "SharePoint Snap-in is now loaded"}
   11:  
   12:  
   13: $xmldata = [xml](Get-Content 'C:\SPBackup\spbrtoc.xml')
   14: $BadBackups =$xmldata.SPBackupRestoreHistory.SPHistoryObject | Where-Object {$_.SPErrorCount -gt '0'}
   15:  
   16: IF($badbackups -ne $null)
   17: {"Entering Bad backup  loop"
   18:  
   19: $BadBackups | % { $xmldata.SPBackupRestoreHistory.RemoveChild($_) } 
   20:  
   21: $BadBackups | % { Remove-Item $_.SPBackupDirectory -Recurse } 
   22:  
   23: $xmldata.Save('C:\SPBackup\spbrtoc.xml')
   24:  
   25: "CLeaned all Failed Backups."}
   26:  
   27: ELSE {Write-Host -f Green "No Backup exists with failure status" }

 

Cheers,

Priyo