如何更正自动删除Sharepoint备份的PowerShell脚本

Mr *_*ubs 1 powershell sharepoint sharepoint-2010

我一直在使用这个(和这个)脚本来删除旧的sharepoint备份,但它会删除所有备份而不是14天以上的备份.

我通过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)) }
Run Code Online (Sandbox Code Playgroud)

我得到了所有的日期输出(我期望).这告诉我'where'或'?'中的问题 - 我知道它们是可以互换的.无论如何,$ 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>
Run Code Online (Sandbox Code Playgroud)

Mr *_*ubs 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."
Run Code Online (Sandbox Code Playgroud)


Jay*_*kul 5

它看起来像你最终的比较是基于字符串,而不是基于日期,所以例如:

"10/08/2007 20:20:13" -lt (Get-Date -Year 1900)
Run Code Online (Sandbox Code Playgroud)

当DateTime对象被强制转换为字符串时,该数字总是小于"星期日"或"星期一"或者在字符串前面得到的数字......

我无法访问我可以测试它的一组备份,但是对于初学者,你应该修复它,同时确保你没有因为值为null而删除备份:

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

XML文件中的日期字符串格式(根据文档页面)是Get-Date可以轻松解析的格式,因此应该没有任何问题.

顺便说一句,你的假设是关于$ _是数组中当前的迭代对象;)