iNoob
iNoob

Reputation: 1395

Modifying a Date variable in PowerShell

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
    }
}

Upvotes: 0

Views: 2113

Answers (2)

iNoob
iNoob

Reputation: 1395

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)

Upvotes: 0

AdminOfThings
AdminOfThings

Reputation: 25001

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.

Upvotes: 1

Related Questions