Issue with Incremental Backup Script

Hey all, I was wondering if I could pick some brains about a script I' m being asked to write at work. The script needs to do a full backup on Sunday and incremental backups Mon-Sat. But it also has to keep two different sets of backups going, so week 1, week 2, week 3 overwrites week 1, ect.  This is what I have so far but I'm having trouble getting Powershell to identify the day of the week.

$HN = Hostname
$b = Get-Date -Format "yyyy-MM-dd"
$a = Get-Date
"Day: " + $a.Day
"Month: " + $a.Month
"Year: " + $a.Year
"Day of Week: " + $a.DayOfWeek
"Day of Year: " + $a.DayOfYear
$Days = (New-TimeSpan -Start 1/1/1900 -End (Get-Date)).Days
$Weeks = [int]($Days / 7)
$SourceFolder = "C:\Users\Mark\Documents\reports"
$DestinationFolder1 = "\\x.x.x.x\lcms\$HN\Test2\$b"
$DestinationFolder2 = "\\x.x.x.x\lcms\$HN\Test3\$b"

IF ($a.DayOfWeek = "Day of Week: Sunday" -and $Weeks % 2 -eq 0)
    {Robocopy $SourceFolder $DestinationFolder1 /S /R:3 /W:30 /XO}
ELSE
    {Robocopy $SourceFolder $DestinationFolder1 /S /R:3 /W:30 /XO /MAXAGE:1}

IF ($a.DayOfWeek = "Day of Week: Sunday" -and $Weeks % 2 -ne 0)
    {Robocopy $SourceFolder $DestinationFolder2 /S /R:3 /W:30 /XO}
ELSE
    {Robocopy $SourceFolder $DestinationFolder2 /S /R:3 /W:30 /XO /MAXAGE:1}

*Error Text Below*

'DayOfWeek' is a ReadOnly property.
At C:\Users\Mark\Desktop\Scripts\Lab Backup.PS1:15 char:5
+ IF ($a.DayOfWeek = "Day of Week: Sunday" -and $Weeks % 2 -eq 0)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException
  • Edited by Baiseley Friday, November 01, 2013 12:40 PM
November 1st, 2013 2:50pm

Hi Baiseley,

There are a few problems.  First is that you're using the "=" (assignment) operator instead of "-eq" (testing for equality).  That's what's causing the Property Assignment error.  Second, you're trying to check the DayOfWeek property for a string of "Day of Week: Sunday", which will never be True.  Just "Sunday" will do.

The last issue is in how the logic of your If statements is set up.  Try this:

$a = Get-Date
$Days = (New-TimeSpan -Start 1/1/1900 -End $a).Days
$Weeks = [int]($Days / 7)

# First, save a variable with the destination folder based on whether it's
# an even or odd numbered week.

if ($Weeks % 2 -eq 0)
{
    $Destination = $DestinationFolder1
}
else
{
    $Destination = $DestinationFolder2
}

if ($a.DayOfWeek -eq 'Sunday')
{
    # It's Sunday, perform a full backup to $Destination
}
else
{
    # It's not Sunday, perform an incremental backup to $Destination
}

  • Marked as answer by Baiseley Friday, November 01, 2013 2:00 PM
Free Windows Admin Tool Kit Click here and download it now
November 1st, 2013 4:38pm

That worked perfectly! Thank you so much, I suppose I was over thinking the complexity of the script.
November 1st, 2013 5:00pm

One other thing:  1/1/1900 was Monday.  If you want your even / odd numbered week logic to work, you should choose a Sunday for that line of the code (whether it's 12/31/1899, or something more recent, doesn't matter.)
  • Edited by David Wyatt Friday, November 01, 2013 2:05 PM
Free Windows Admin Tool Kit Click here and download it now
November 1st, 2013 5:05pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics