2011-01-07 65 views
1

我已經been using this()腳本刪除較舊的共享點備份,但它會刪除所有備份而不是14天以上的備份。如何更正此PowerShell腳本自動刪除Sharepoint備份

我通過powershell_ise.exe運行它,並在其中包含$_.SPStartTime的行下放置一個斷點,並且它顯示$_.SPStartTime =就好像日期未填充一樣。我看着裏面的$sp.SPBackupRestoreHistory.SPHistoryObject,包含我期望的數據。

這是有問題的部分是在這條線:

# Find the old backups in spbrtoc.xml 
$old = $sp.SPBackupRestoreHistory.SPHistoryObject | 
? { $_.SPStartTime -lt ((get-date).adddays(-$days)) } 

我得到的所有的日期輸出(這是我所期望的)的。這告訴我在'哪裏'或'?'的問題 - 我知道它們是可以互換的。無論如何,$ old總是顯示爲空。

的要求:

<?xml version="1.0" encoding="utf-8"?> 
<SPBackupRestoreHistory> 
    <SPHistoryObject> 
     <SPId>a8a03c50-6bc2-4af4-87b3-caf60e750fa0</SPId> 
     <SPRequestedBy>ASERVER\AUSER</SPRequestedBy> 
     <SPBackupMethod>Full</SPBackupMethod> 
     <SPRestoreMethod>None</SPRestoreMethod> 
     <SPStartTime>01/09/2011 00:00:13</SPStartTime> 
     <SPFinishTime>01/09/2011 00:05:22</SPFinishTime> 
     <SPIsBackup>True</SPIsBackup> 
     <SPConfigurationOnly>False</SPConfigurationOnly> 
     <SPBackupDirectory>E:\Backups\spbr0003\</SPBackupDirectory> 
     <SPDirectoryName>spbr0003</SPDirectoryName> 
     <SPDirectoryNumber>3</SPDirectoryNumber> 
     <SPTopComponent>Farm</SPTopComponent> 
     <SPTopComponentId>689d7f0b-4f64-45d4-ac58-7ab225223625</SPTopComponentId> 
     <SPWarningCount>0</SPWarningCount> 
     <SPErrorCount>0</SPErrorCount> 
    </SPHistoryObject> 
    <SPHistoryObject> 
     <SPId>22dace04-c300-41d0-a9f1-7cfe638809ef</SPId> 
     <SPRequestedBy>ASERVER\AUSER</SPRequestedBy> 
     <SPBackupMethod>Full</SPBackupMethod> 
     <SPRestoreMethod>None</SPRestoreMethod> 
     <SPStartTime>01/08/2011 00:00:13</SPStartTime> 
     <SPFinishTime>01/08/2011 00:05:26</SPFinishTime> 
     <SPIsBackup>True</SPIsBackup> 
     <SPConfigurationOnly>False</SPConfigurationOnly> 
     <SPBackupDirectory>E:\Backups\spbr0002\</SPBackupDirectory> 
     <SPDirectoryName>spbr0002</SPDirectoryName> 
     <SPDirectoryNumber>2</SPDirectoryNumber> 
     <SPTopComponent>Farm</SPTopComponent> 
     <SPTopComponentId>689d7f0b-4f64-45d4-ac58-7ab225223625</SPTopComponentId> 
     <SPWarningCount>0</SPWarningCount> 
     <SPErrorCount>0</SPErrorCount> 
    </SPHistoryObject> 
</SPBackupRestoreHistory> 

回答

5

它看起來對我像你結束了與比較是基於字符串的,而不是基於日期的,因此,例如:

"10/08/2007 20:20:13" -lt (Get-Date -Year 1900) 

的數字始終會小於「星期日」或「星期一」,或者當DateTime對象投射到字符串時,您會在字符串的前部得到的任何數字...

我沒有CCESS一組備份我可以測試這個,但對於初學者來說,你應該解決這個問題,並在同一時間,確保你不刪除備份只是因爲值爲null:

# Find the old backups in spbrtoc.xml 
$old = $sp.SPBackupRestoreHistory.SPHistoryObject | 
Where { (Get-Date $_.SPStartTime) -lt ((get-date).adddays(-$days)) } 

XML文件中的日期字符串格式(根據the docs page)是Get-Date可以輕鬆解析的日期字符串格式,因此應該沒有任何問題。

順便說一句,你的假設是正確的約$ _是從陣列的當前迭代對象;)

+0

我會看看這個,但你的說法是有道理的......驚訝沒有人注意到這一點在他們的博客。 – 2011-01-10 11:42:14

+0

查看更新後的問題 - 我認爲問題出在過濾,因爲$ old始終爲空 – 2011-01-10 12:31:31

6

我相信這個問題是與日期格式做。

最終工作的腳本:

# Location of spbrtoc.xml 
$spbrtoc = "E:\Backups\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 | 
    ? { ((
      [datetime]::ParseExact($_.SPStartTime, "MM/dd/yyyy HH:mm:ss", [System.Globalization.CultureInfo]::InvariantCulture) 
     ) -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."