0

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
    }
}
2
  • 1
    New-Timespan doesn't need -Start if the starting time is now. Is the execution time always the same? You could just use New-Timespan -Hours 8 for example. Commented Nov 11, 2020 at 14:24
  • Thanks, I wasn't aware of that. Commented Nov 11, 2020 at 14:44

2 Answers 2

1

New-Timespancreates a TimeSpan object regardless of the parameter set being used. If you know how long a timespan should be, then you can statically create that combination of days, hours, minutes, and seconds, making end time irrelevant.

$date = Get-Date -DisplayHint Date
if ($date.DayOfWeek -eq 'Friday') {
    $countdown = New-Timespan -Hours 8
}

You could have a situation where you know you want the task to span 2 days but want it to end at 01:00. This would make the total time variable. You can make adjustments based on start time:

$date = Get-Date -DisplayHint Date
if ($date.DayOfWeek -eq 'Friday') {
    $end = Get-Date -Day $date.AddDays(2).Day -Month $date.AddDays(2).Month -Hour 1 -Minute 0 -Second 0
    $countdown = New-TimeSpan -End $end
}

Edit:

If you reach a target day of week and want to create a timer that lasts until the next 01:00 hour from that point in time, you can do the following:

$date = Get-Date
if ($date.DayOfWeek -eq 'Friday') {
    if ($date.Hour -lt 1) {
        # dynamic end date - start date
        $countdown = (Get-Date $date -Hour 1 -Minute 0 -Second 0) - $date
    } else {
        # dynamic end date - start date
        $countdown = (Get-Date $date.AddDays(1) -Hour 1 -Minute 0 -Second 0) - $date
    }
}
$countdown.TotalSeconds # total seconds of the time span

As an aside, if you expect the starting time to be now, you can skip the -Start parameter. Leaving off -Start assumes the starting time is now.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for the response, how would I know what hours to put in -Hours? It seems I would still need to dynamically identify the time difference between now and the expected execution time? Apologies if I am missing something
How do you determine expected execution time? There must be logic there that we can code.
O you added more since my comment post. Apologies if my question doesnt now make sense
I am using a YAML config to catch expected execution. So let's say Every Friday at 01:00. This being the time the execution of whatever process runs. It will run until it finishes.
Let's say for example, today is Friday and the time is 12:00. Do you expect the task to run for 6 more days then to the next Friday?
|
0

Thanks for the help, I have no idea why I didn't think of it before but I have come up with

if ($date.DayOfWeek -eq 'Friday') 

{   
     $StartDate = (GET-DATE)
     $tempDate = (GET-DATE).ToString() 
     $tempDate, $tmpTime = $tempDate.Split(' ')
     datetime]$DateTime = $tempDate + " $($schedule.Time)"
     $Timer = NEW-TIMESPAN -End $DateTime 
}

Just need to understand how to use the timer now (all in seconds)

Comments

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.