Таймерная функция Powershell не возвращает правильное время - PullRequest
0 голосов
/ 23 апреля 2020

Я пишу функцию безопасности для моего сценария powershell, чтобы он не работал в рабочее время в случае сбоя. Это означает, что я могу автоматизировать больше, так как мне не нужно беспокоиться о том, что скрипт будет слишком долго, поскольку он будет останавливаться при необходимости, а затем возобновляться.

К сожалению, когда он должен работать, он все еще спит. Любые указатели на то, что мне не хватает, будут оценены.

Function WorkingHours {
    $WorkingHours = "20:00-7:00"

    if ($WorkingHours -match '^[0-9]{1,2}:[0-5][0-9]-[0-9]{1,2}:[0-5][0-9]$') {

        $current = Get-Date
        $start = Get-Date ($WorkingHours.split("-")[0])
        $end = Get-Date ($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)) {


            # sleep until the operational window starts again
            $sleepSeconds = ($start - $current).TotalSeconds

            Write-Host 'Starting to Sleep for'$sleepSeconds

            if ($sleepSeconds -lt 0) {
                # correct for hours that span overnight
                $sleepSeconds = ($start.addDays(1) - $current).TotalSeconds
            }
            # sleep until the wake up interval
            Start-Sleep -Seconds $sleepSeconds
        }
    }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...