-1

So i need a script that would change the date and time to a specific date and time. My problem is that i need a cycle of 24 hours on one specific date and then it goes back 24 so the date does not change. For ex. 25. 1. 2021 00:00:00 Am ----> 25. 1. 2021 11:59:59 Pm ----> 25. 1. 2021 00:00:00 Am and so on. I tried to run a script in powershell that would run every 24 hours every day, but in Task scheduler it moves the next trigger day by 24 hours so if i run the script on the 25. 1. 2021 11:59:59 Pm the next trigger day is on the 26. 01. 2021 and that is 48 hours and then on 27. 01. 2021 and so on. Is there a solution for this problem? The code that i run Register-ScheduledJob -Name 'XXXX' -FilePath 'C\\\.ps1' -Trigger (New-JobTrigger -Once -At "12:00 am" -RepetitionInterval (New-TimeSpan -Minutes 1440) -RepetitionDuration ([TimeSpan]::MaxValue)) In the file`function Set-Time([string]$dateTime)

{
$newDate = Get-Date $dateTime
Set-Date $newDate
}

Set-Time 'Monday, January 25,2021 12:00:00 AM'`
Wospa
  • 3
  • 1

1 Answers1

0

Normally, I would develop a Windows Service that sleeps for 24 hours. When it stops sleeping, it would reset the date/time.

Presuming development is not desired, one option could be to have the scheduled task run the Powershell script once at the desired time (January 25, 2021, 12:00:00 am). Have this same task create a new schedule to run the same script after the time has changed. In other words, the script bootstraps itself for the next event. You have to schedule it manually the first time.