2011-01-07 12 views
1

古い共有ポイントのバックアップを削除するにはbeen using thisand this)のスクリプトを使用しましたが、14日以上の古いバックアップはすべて削除します。Sharepointバックアップを自動的に削除するこのPowerShellスクリプトを修正する方法

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は常にnullとみなされます。例えば、あなたが比較されて、文字列ベースではなく、日付ベースで終わるているように私には見えます

<?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オブジェクトが文字列にキャストされたときに文字列の先頭に何が得られるかは...

私は

# Find the old backups in spbrtoc.xml 
$old = $sp.SPBackupRestoreHistory.SPHistoryObject | 
Where { (Get-Date $_.SPStartTime) -lt ((get-date).adddays(-$days)) } 
:私はこれでテストできますが、初心者のために、あなたはそれを修正する必要があり、同時に、値がnullであるという理由だけでバックアップを削除していないことを確認して、バックアップのセットにCCESS

XMLファイルの日付文字列形式(the docs page)は、Get-Dateが簡単に解析できるものなので、問題なく動作するはずです。

ところで、あなたの仮定が正しいの約$です_配列から現​​在の反復対象となる;)

+0

が、私はこのに見ているだろうが、何あなたの格言が理にかなって...は、誰もが自分のブログでこれを気づいていない驚かせました。 –

+0

更新された質問を見る - 私は問題が$ oldが常にnullであるというフィルタリングにあると思います –

+0

サンプルXMLファイルを投稿できますか?私は困惑しています:) – Jaykul

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." 
+0

これを共有してくれてありがとう!私はいくつかのサーバーが、新年以来、多分すべてを削除し始めました。これは、ファイルを適切に保持しているようです。 –

関連する問題