Datetime 在日期为的时间间隔之外生成文件

Datetime 在日期为的时间间隔之外生成文件,datetime,intervals,powershell-5.0,Datetime,Intervals,Powershell 5.0,我目前正在开发一个持续运行的程序,以创建特定时间范围之外的文件。这意味着如果当前时间在starttime和endtime之外,则需要创建文件 这是我的密码: $a = get-date -format yyyyMMdd $b = get-date while ($true) { $starttime = (get-date "21:00") #9pm today $endtime = (get-date "05:00").AddDays(1) #5am next day

我目前正在开发一个持续运行的程序,以创建特定时间范围之外的文件。这意味着如果当前时间在
starttime
endtime
之外,则需要创建文件

这是我的密码:

$a = get-date -format yyyyMMdd
$b = get-date
while ($true)
{
    $starttime = (get-date "21:00") #9pm today
    $endtime = (get-date "05:00").AddDays(1) #5am next day
    $current = get-date #current time

    ##Something wrong in my checking method, trying to use 'New-timespan' with no luck
    if(($current.TimeOfDay -gt $starttime.TimeOfDay) -and ($current.TimeOfDay -lt $endtime.TimeOfDay)){
        $b = get-date
        Add-Content "$PSScriptRoot\logging_$($a)_tested.txt" "[$b]-> No Need create file"

    } else{
        $b = get-date
        Add-Content "$PSScriptRoot\logging_$($a)_tested.txt" "[$b]-> Starting create file"
    }
    start-sleep -Seconds 30
}

问题是我的代码始终处于“启动创建文件”模式。我也尝试使用
newtimespan
,但仍然遇到了相同的问题。

我发现了问题,我不得不将
if
语句更改为

if ($current -lt $starttime -or $current -gt $endtime)