Problem with If statement in script

by cryolyte at 2012-12-10 06:11:59

I have a script that is supposed to start a Backup Exec 2010 job IF the date is after the last Friday of the month. The script does successfully start the Backup Exec job, but it does it’s ignoring the IF logic.

#Get the date of the last Friday of this month
$Dayname = "Friday"

#Calculate the last friday of the month
$LastDayOfMonth = (Get-Date -Year (Get-Date).Year -Month (Get-Date).Month -Day 1).AddMonths(1).AddDays(-1)
If($LastDayOfMonth.DayOfWeek -eq $DayName)
{
$Answer = $LastDayOfMonth

}
Else {
While($Answer -eq $Null)
{
$LastDayOfMonth = $LastDayOfMonth.AddDays(-1)
If($LastDayOfMonth.DayOfWeek -eq $DayName)
{
$Answer = $LastDayOfMonth
}

}
}

#Get Today’s date in a format suitable for comparison. Format for comparison.
$Date = ((get-date).ToShortDateString())
#Convert last day of month for comparison
$LastFriday = ($Answer.ToShortDateString())
#See if today is the first of the month (if so, copy job will run).
$Date2 = ((get-date).day)

#If Today’s date is past or equal to the date of the last friday OR today is the 1st of the month, start the BE job that writes monthly Veeam backups to tape
If ($Date -ge $LastFriday -or $date2 -eq 1){
start-process "C:\Program Files\Symantec\Backup Exec\bemcmd.exe" -ArgumentList ‘-o1 -j"CITYBU01 - Veeam to Tape"’
} #End If

So, IF the $date is greater than the last friday of the month, OR the date is the first of the month (in which case it is also past the last friday of the month) then the job will start.
I ran everything up to the point of the IF statement, and then evaluated the logic.

$date evaluates to 12/10/2012
$lastfriday evaluates to 12/28/2012
$date2 evaluates to 10

Then I tested the actual logic:
$date -ge $lastfriday evaluates to false
$date2 -eq 1 evaluates to false

YET THE ACTION WITHIN THE IF STATEMENT STILL STARTED!!!
What am I missing here?
by nohandle at 2012-12-10 11:06:51
not saying it will help in this case but for me the golden rule of any condition is to enclose any pair in brackets.
by nohandle at 2012-12-10 11:11:54
$Date -ge $LastFriday -or $date2 -eq 1 returns false for me. the start process is not executed on my console.
by cryolyte at 2012-12-10 11:33:55
It evaluates false for me as well. What’s mystifying is that the process DID start.
by nohandle at 2012-12-10 11:36:20
If you are really going for the last friday of month I think this script is less universal but more readable:
#get-todays date
$today = Get-Date
#test for this month $today = Get-Date -Day 29
#get-last Friday of month
$LastDayOfMonth = (Get-date -Day 1).AddMonths(1).AddDays(-1)
$MinusDays = switch (($LastDayOfMonth.DayOfWeek))
{
'Friday' {0}
'Saturday' {1}
'Sunday' {2}
'Monday' {3}
'Tuesday' {4}
'Wednesday' {5}
'Thursday' {6}

}
$lastFridayOfMonth = $LastDayOfMonth.AddDays(-$minusDays)

#If Today's date is past or equal to the date of the last friday OR today is the 1st of the month,
#start the BE job that writes monthly Veeam backups to tape
If (($today -ge $LastFriday) -or ($today.day -eq 1))
{
start-process "C:\Program Files\Symantec\Backup Exec\bemcmd.exe" -ArgumentList '-o1 -j"CITYBU01 - Veeam to Tape"'
}