0

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)
1
  • 1
    As an aside: use of the -in operator allows your to shorten conditionals such as $Month -eq "August" -or $Month -eq "January" -or ... to $Month -in 'August', 'January', ... Commented Sep 6, 2021 at 0:03

1 Answer 1

3

You are only getting the date/time once before you start the Do/While loop so the date/time variables are always going to be the same.

You need to move the following into your Do block so that these variable update with the current time on each loop.

$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)
Sign up to request clarification or add additional context in comments.

2 Comments

Maaate, thank you. Such an easy mistake! I am still learning, as you can tell, but that's just helped me with a bit of understanding. Thanks again
You're very welcome. Glad I could help. We've all been there!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.