I accidently managed to schedule a GPO task out at the wrong time which caused no amount of trouble so I have decided to add a time check to my powershell scripts to make sure they only run outside of core working hours. I had this code in one of my folders from ages ago that I found on a gist, but unfortunately cannot find the link to give proper credit.
If it is run in daytime hours it works perfectly but when the hours are overnight, it returns a minus int error.
Here is the following code:
Function TimeCheck {
$script:WorkingHours = "18:00-7:00"
if ($script:WorkingHours -match '^[0-9]{1,2}:[0-5][0-9]- [0-9]{1,2}:[0-5][0-9]$') {
$current = Get-Date
$start = Get-Date ($script:WorkingHours.split("-")[0])
$end = Get-Date ($script:WorkingHours.split("-")[1])
# correct for hours that span overnight
if (($end-$start).hours -lt 0) {
$start = $start.AddDays(-1)
}
# if the current time is past the start time
$startCheck = $current -ge $start
# if the current time is less than the end time
$endCheck = $current -le $end
# if the current time falls outside the window
if ((-not $startCheck) -or (-not $endCheck)) {
write-host "[-] Time is outside operational window"
# sleep until the operational window starts again
$sleepSeconds = ($start - $current).TotalSeconds
if($sleepSeconds -lt 0) {
# correct for hours that span overnight
$sleepSeconds = ($start.addDays(1) - $current).TotalSeconds
}
# sleep until the wake up interval
write-host '[!] sleeping for' $sleepSeconds
Start-Sleep -Seconds $sleepSeconds
}
}
}
All ideas are welcome, hopefully someone can see something I cannot!
Thanks