Answered Can not compare dates

  • Tuesday, August 09, 2011 2:19 PM
     
      Has Code
    Good time to everyone!
    I try to schedule Sharepoint backup. (The issue is not regarding sharepoint so I'm at this forum)). I use the article http://imperfectit.blogspot.com/2010/03/automate-sharepoint-2010-farm-backups.html. Thanks Imperfect IT for it.
    I use the following script to clean backup jobs after 31 days.
    # Location of spbrtoc.xml
    $spbrtoc = "E:\Backups\spbrtoc.xml" 
    # Days of backup that will be remaining after backup cleanup.
    $days = 31
    # 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."
    
    
    

    The spbrtoc.xml is an xml file in the following format:
    <?xml version="1.0" encoding="utf-8"?>
    <SPBackupRestoreHistory>
      <SPHistoryObject>
        <SPId>33bda4d7-2b18-408e-8b54-e3a050e085e9</SPId>
        <SPRequestedBy>FABRIKAM\sally</SPRequestedBy>
        <SPBackupMethod>Full</SPBackupMethod>
        <SPRestoreMethod>None</SPRestoreMethod>
        <SPStartTime>10/08/2007 20:35:47</SPStartTime>
        <SPFinishTime>10/08/2007 20:35:55</SPFinishTime>
        <SPIsBackup>True</SPIsBackup>
        <SPBackupDirectory>\\server01\WSSBackups\spbr0002\</SPBackupDirectory>
        <SPDirectoryName>spbr0002</SPDirectoryName>
        <SPDirectoryNumber>2</SPDirectoryNumber>
        <SPTopComponent>Farm\WSS_Administration\Web Application\SharePoint_AdminContent_d86ba3d1-cce5-4391-8135-fae450dd8332</SPTopComponent>
        <SPTopComponentId>a80e3734-75b3-4c7c-b04f-d6003ac3045f</SPTopComponentId>
        <SPWarningCount>0</SPWarningCount>
        <SPErrorCount>0</SPErrorCount>
      </SPHistoryObject>
    </SPBackupRestoreHistory>
    
    

    Now I'm assigned a task to have only 3 backup jobs. They are:
    - yesterday backup
    - week backup
    - month backup.
    I try to modify the script to clean all backups except above.
    I find the last day of week as:
    ([System.Datetime] $date = $(get-date))
    $NumDaysSinceSunday = $date.DayOfWeek.value__ 
    $PreviousSunday = $date.AddDays(- $NumDaysSinceSunday)
    

    and the last day of month as:
    $NumDaysSinceLastDayOfMonth = $date.Day + 3
    $PreviousLastDayOfMonth = $date.AddDays(-$NumDaysSinceLastDayOfMonth)
    

    Then I tried to find date entries which has for example last sunday date.
    $old = $sp.SPBackupRestoreHistory.SPHistoryObject | 
        ? {
           $_.SPStartTime -eq $PreviousLastDayOfMonth
          }
    

    But it found nothing. If I use -lt or gt operators everthing is ok but not with -eq. Where am I wrong? 
     

Answers

  • Tuesday, August 09, 2011 2:33 PM
     
     Answered Has Code

    I think the problem you're running into is that when you use the -eq operator, it compares the entire DateTime object -- not just the month, day, year, but also the hour, minute, second. So -eq will return $false unless the hour/min/sec also match.

    You could compare only the year/month/day with something like this:

     

     ? {
      ($_.SPStartTime.Year -eq $PreviousLastDayOfMonth.Year) -and
    		($_.SPStartTime.Month -eq $PreviousLastDayOfMonth.Month) -and
    		($_.SPStartTime.Day -eq $PreviousLastDayOfMonth.Day)
      }
    


     


    • Marked As Answer by egoncharov Wednesday, August 10, 2011 7:05 AM
    •  
  • Tuesday, August 09, 2011 3:28 PM
    Moderator
     
     Answered

    Convert SPStartTime to a datetime object:

    $date.Date -eq ([datetime]$_.SPStartTime).Date


    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar

All Replies

  • Tuesday, August 09, 2011 2:33 PM
     
     Answered Has Code

    I think the problem you're running into is that when you use the -eq operator, it compares the entire DateTime object -- not just the month, day, year, but also the hour, minute, second. So -eq will return $false unless the hour/min/sec also match.

    You could compare only the year/month/day with something like this:

     

     ? {
      ($_.SPStartTime.Year -eq $PreviousLastDayOfMonth.Year) -and
    		($_.SPStartTime.Month -eq $PreviousLastDayOfMonth.Month) -and
    		($_.SPStartTime.Day -eq $PreviousLastDayOfMonth.Day)
      }
    


     


    • Marked As Answer by egoncharov Wednesday, August 10, 2011 7:05 AM
    •  
  • Tuesday, August 09, 2011 3:04 PM
    Moderator
     
     

    The date is compared against all of its parts, day,month,year and time parts and likely one date is not the same as another. You can compare on the Date property of each date time object:

    $date1.date -eq $date2.date

     

    Another option is to compare the dates based on their short date string:

    $date1.ToShortDateString() -eq $date2.ToShortDateString()

     


    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar
  • Tuesday, August 09, 2011 3:13 PM
     
     

    Thank for the answer!

    I tried to use your solution. The result is the same. SPStartTime is in 07/29/2011 21:15:13 format. $PreviousLastDayOfMonth in 28 July 2011 13:53:31 format. May be this is the source of the problem?



    Furthermore $old = $sp.SPBackupRestoreHistory.SPHistoryObject | ft SPStartTime.Year result in nothing.
  • Tuesday, August 09, 2011 3:23 PM
     
     

    How can I compare $date with SPStartTime xml element?

  • Tuesday, August 09, 2011 3:28 PM
    Moderator
     
     Answered

    Convert SPStartTime to a datetime object:

    $date.Date -eq ([datetime]$_.SPStartTime).Date


    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar
  • Wednesday, August 10, 2011 7:05 AM
     
     

    Thanks to everyone for the answers!

    Your solution do works!!!

  • Thursday, January 19, 2012 8:52 PM
     
     

    This looks like the answer I've been looking for. I'm new to powershell so could you explain where this would go in my script? 

     

    # Location of spbrtoc.xml

    $spbrtoc = "\\Servername\shareName\spbrtoc.xml" 

     

    # Days of backup that will be remaining after backup cleanup.

    $days =18

    #$minutes =60480

     

    # 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)) }

    #? { $_.SPStartTime -lt ((get-date).addminutes(-$minutes)) }

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

    #Write-host "Backup(s) entries older than $minutes minutes are removed from spbrtoc.xml and harddisc."

  • Sunday, January 22, 2012 7:37 AM
    Moderator
     
     
    $sp.SPBackupRestoreHistory.SPHistoryObject |? { [datetime]$_.SPStartTime -lt ((get-date).adddays(-$days)) }
    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar