Windows Task Scheduler - Run only during window of time at startup

95 views
Skip to first unread message

Max Coder

unread,
Dec 6, 2023, 11:18:35 AM12/6/23
to ntsysadmin
Hi,

I have a two tasks already set up in the Windows Scheduler (on Win Server 2019). It works great.  Can Task Sch. be setup to run every two weeks on Sunday at startup but only during a certain window of time. I mean , Let's say  I've rebooted machine for any reason except on Sunday. Then This Task won't work. I cannot figure out the logic to do it.

My task scheduler :

first Task : On a Schedule , Recur every 2 weeks on Sunday , Start Time : 12:00 PM  Name: Restart Script Task  restart-computer -force

Second Task : At Startup , working another powershell script

thanks,

Michael B. Smith

unread,
Dec 6, 2023, 11:48:44 AM12/6/23
to ntsys...@googlegroups.com

You can either modify the logic of the script so it looks at the day/time it’s running, or you have two different schedules – one for Sunday and one for every other day.

--
You received this message because you are subscribed to the Google Groups "ntsysadmin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ntsysadmin+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ntsysadmin/b27bfc7b-6305-4554-bc39-4c55c4ac7453n%40googlegroups.com.

maxcoder1

unread,
Dec 6, 2023, 1:49:45 PM12/6/23
to ntsys...@googlegroups.com
Thanks, But How can we check at the day/time it’s running ? 

My question is :  I will use if (($LastrunTime) -lt (get-date) ).  Is there a better way?

Script :

 $LastRunTime = Get-ScheduledTask -TaskName "Restart Server" |  Get-ScheduledTaskInfo | select -ExpandProperty LastRunTime
 $LastRunTime

#Wednesday, December 6, 2023 11:44:44 AM


if (($LastrunTime) -lt (get-date) )
{

Write-Host "Second Task is running."

}

else {Write-Host "Second Task is not running"}

maxcoder1

unread,
Dec 6, 2023, 2:38:07 PM12/6/23
to ntsys...@googlegroups.com
Also , Get-Date returns the current date/time value, and the "last" run is necessarily in the past, so your comparison will likely always be true. Not working correctly  (($LastrunTime) -lt (get-date) )

Michael B. Smith

unread,
Dec 6, 2023, 3:38:07 PM12/6/23
to ntsys...@googlegroups.com

Then your logic is wrong. Get-Date returns a lot more than just a date:

 

Date                 Property

Day                  Property

DayOfWeek            Property

DayOfYear            Property

Hour                 Property

Kind                 Property

Millisecond          Property

Minute               Property

Month                Property

Second               Property

Ticks                Property

TimeOfDay            Property

Year                 Property

DateTime             ScriptProperty

 

You probably need to look at DayOfWeek as well as the DateTIme default. Based on your OP, I don’t see any reason to look at the past run-date.

Or you have two different schedules – one for Sunday and one for every other day.

Belanger, Xavier B

unread,
Dec 6, 2023, 4:00:32 PM12/6/23
to ntsys...@googlegroups.com

Hi,

 

maxcoder1 wrote:

 

> My question is :  I will use if (($LastrunTime) -lt (get-date) ).  Is there a better way?

 

If you’re trying to compare two dates, I would suggest to have a look at the  New-TimeSpan cmdlet, that may help.

 

[ https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/new-timespan?view=powershell-7.3 ]

 

Sincerely,

--

Xavier Belanger

IT Security Architect | CISSP | Office of Information Security

University of North Carolina Wilmington

maxcoder1

unread,
Dec 7, 2023, 3:56:18 AM12/7/23
to ntsys...@googlegroups.com
Now , I have tested it again. it works very well.

$a = Get-ScheduledTask -TaskName "Restart Server" | Get-ScheduledTaskInfo | select -ExpandProperty LastRunTime
$a

$a = "{0:D}" -f ($a)
$a
$b = "{0:D}" -f (get-date)
$b
($a) -eq ($b)

Thursday, December 7, 2023 11:44:44 AM
Thursday, December 7, 2023
Thursday, December 7, 2023
True

--
You received this message because you are subscribed to the Google Groups "ntsysadmin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ntsysadmin+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages