I'm looking to create a schedule type check.
I want to check today's date and if it's not the correct Day of the week for a task to run it continues.
Once the day is correct, I will then create a timer based on the difference between the expected execution time (let's say 01:00) and now. I have had a few ideas on how to achieve this, one of them being below. The problem I am facing is I am unsure of how to create a CountDown timer from the NEW-TIMESPAN output and how to dynamically change the $EndDate time value to meet the relevant schedule value. Any pointers would be appreciated.
$date = Get-Date -DisplayHint Date
if ($date.DayOfWeek -eq 'Friday')
{
$StartDate = (GET-DATE)
# I am unsure of how to dynamically change the $EndDate variable time value and create the 'CountDown variable' from the New-TimSpan output.
$EndDate = [datetime]"11/13/2020 01:00"
$countdown = NEW-TIMESPAN -Start $StartDate -End $EndDate
}
Write-Host $date
EDIT
With some help from @AdminOfTHings I have come up with the following solution.
if ($date.DayOfWeek -eq $schedule.Day)
{
$StartDate = (GET-DATE)
$tempDate = (GET-DATE).ToString()
$tempDate, $tempTime = $tempDate.Split(' ')
[datetime]$DateTime = $tempDate + " $($schedule.Time)"
$Timer = NEW-TIMESPAN -End $DateTime
$CountDown = $Timer.TotalSeconds
while ($CountDown -gt 0)
{
sleep 1
$CountDown--
}
else
{
#START SERVICE
}
}
New-Timespandoesn't need-Startif the starting time is now. Is the execution time always the same? You could just useNew-Timespan -Hours 8for example.