如何更正此PowerShell脚本自动删除Sharepoint备份
问题描述:
我已经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>
答
它看起来对我像你结束了与比较是基于字符串的,而不是基于日期的,因此,例如:
"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可以轻松解析的日期字符串格式,因此应该没有任何问题。
顺便说一句,你的假设是正确的约$ _是从阵列的当前迭代对象;)
答
我相信这个问题是与日期格式做。
最终工作的脚本:
# 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."
我会看看这个,但你的说法是有道理的......惊讶没有人注意到这一点在他们的博客。 – 2011-01-10 11:42:14
查看更新后的问题 - 我认为问题出在过滤,因为$ old始终为空 – 2011-01-10 12:31:31