Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have strange situation with PowerShell script which delete older Backup Files in SharePoint Backup Folder. I use two PowerShell script, first make Backup Farm and second check Folder with Backup and delete older then 14 days files with backup. Everything worked fine but after New Year I seen that script which delete older Backup delete only backup made in 2013? It looks like I don't have any new backup because script delete it...

I don't know what is wrong, I pasted this scrip below:

# Location of spbrtoc.xml
$spbrtoc = "\\XXX\spbrtoc.xml" 


# Days of backup that will be remaining after backup cleanup.
$days = 14 


# Import the Sharepoint backup report xml file
[xml]$sp = gc $spbrtoc 


# Find the old backups in spbrtoc.xml
$old = $sp.SPBackupRestoreHistory.SPHistoryObject |
? { $_.SPStartTime -lt ((get-date).adddays(-$days)) }
if ($old -eq $Null) { write-host "No reports of backups older than $days days found in spbrtoc.xml.`nspbrtoc.xml isn't changed and no files are removed.`n" ; break} 


# Delete the old backups from the Sharepoint backup report xml file
$old | % { $sp.SPBackupRestoreHistory.RemoveChild($_) } 


# Delete the physical folders in which the old backups were located
$old | % { Remove-Item $_.SPBackupDirectory -Recurse } 


# Save the new Sharepoint backup report xml file
$sp.Save($spbrtoc)
Write-host "Backup(s) entries older than $days days are removed from spbrtoc.xml and harddisc."

Could you check it?

I will be very grateful for ant help, Best regards

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted
$_.SPStartTime -lt ((get-date).adddays(-$days)

This is probably your faulty code. Since today is the Jan 2, 2013 the subtraction of multiple days will error out. You need to have the script handle previous years as well...

My guess is that Jan 15th, the script should be working again.

share|improve this answer
    
Thanks for your fast support and help. –  Andrzej Jan 2 '13 at 15:10
add comment

I have a similiar issue. My Farm in en-US and my system is de-CH. This results in the DateTime to the spbrtoc.xml in en-US format.

The way I clean up is slightly different to yours but essentially it is down to the formatting of the date.

How I get around this is:

$startTime = $item.SPStartTime

# Get System Culture (Local) and set DateTimeFormat
$deletionDate = Get-Date -Format (Get-Culture).DateTimeFormat.UniversalSortableDateTimePattern
$deletionDate = ([DateTime]$deletionDate).AddDays(-$days)

$startTime = ([DateTime]$startTime)

if ($startTime -lt $deletionDate)
{
Remove-Item $item.SPBackupDirectory -Recurse -ErrorAction Stop
$item.ParentNode.RemoveChild($item)
}

Just wanted to extend the answer slightly or offer an alternative. Ive tested this extensively and it works:


function Convert-DateTime
{
    param ([string]$date)

    # Get Farm Locale (Language/Regional Settings)
    $Locale = [Microsoft.SharePoint.SPRegionalSettings]::GlobalServerLanguage
    $Locale = [System.Globalization.CultureInfo]::GetCultureInfo($Locale.LCID)

    # Get CultureInfo for parsing date
    $ci = New-Object System.Globalization.CultureInfo($Locale)
    $dateOut = [DateTime]::Parse($date, $ci)
    return $dateOut
}

$startTime = $item.SPtartTime

Convert-DateTime $startTime
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.