I have noticed that if the correct time is present when this script is executed, it operates properly within the Do-While loop. If the time is incorrect when the script starts, it stays in the while-loop forever and never re-checks the IF statements.
I did have them as else-ifs but changed it to troubleshoot. Did not work.
Does anyone know why the nested IF statements do not execute once the time is correct even though the do-while is set to loop infinitely?
I know that the actual IF conditions do work and I have tested them.
The Script is testing for whether it is the end of a 31 day month, 30 day month, 28 day month, a Friday arvo and for any day of the week after 5pm.
I would like to check these conditions every 10 minutes but you will see I have used 5 seconds for testing purposes
$WShell = New-Object -com "Wscript.Shell"
$Time=(Get-Date -Format HH:mm)
$Month=(Get-Date -Format MMMM)
$Day =(Get-Date -Format dd)
$Minute=(Get-Date -Format mm)
$DayName =(Get-Date -Format dddd)
$SleepTime = 10
DO{
if ($Month -eq "August" -or $Month -eq "January" -or $Month -eq "March" -or $Month -eq "May" -or $Month -eq "July" -or $Month -eq "October" -or $Month -eq "December" -and $Day -eq "31" -and $Time -gt "15:30" -and $Time -lt "15:49" )
{#for months with 31 days
"It's the end of a 31 day month, please save and submit your timesheets"
Start-Process "Do something"
Start-Sleep -Seconds $SleepTime
}
if ($Month -eq "April" -or $Month -eq "June" -or $Month -eq "September" -or $Month -eq "November" -and $Day -eq "30" -and $Time -gt "15:30" -and $Time -lt "15:49" )
{# for months with 30 days
"It's the end of a 30 day month, please save and submit your timesheets"
Start-Process "Do something"
Start-Sleep -Seconds $SleepTime
}
if ($Month -eq "February" -and $Day -eq "28" -and $Time -gt "15:30" -and $Time -lt "15:49" )
{#for February
"It's the end of the month, please save and submit your timesheets"
Start-Process "Do something"
Start-Sleep -Seconds $SleepTime
}
if($DayName -eq "Friday" -and $Time -gt "15:30" -and $Time -lt "15:49" )
{# For Friday's
"It's the end of the week, please submit and save your timesheet"
Start-Process "Do something"
Start-Sleep -Seconds $SleepTime
}
if($Time -gt "08:26"-and $Time -lt "17:14"){
"It's not the end of the week or month, no need to submit"
Start-Process "Do something"
Start-Sleep -Seconds $SleepTime
}
#"is it time to do timesheets? Not yet"
#"Checking again in 10 Minutes"
"testing while loop"
Start-Sleep -Seconds 5
}while($true)
-inoperator allows your to shorten conditionals such as$Month -eq "August" -or $Month -eq "January" -or ...to$Month -in 'August', 'January', ...