我目前正在开发一个程序,该程序将持续运行以在特定时间范围之外创建文件。这意味着如果当前时间不在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
}
问题是我的代码始终处于“正在启动创建文件”模式。我也尝试使用New-timespan
,但仍然遇到相同的问题。
if ($current -lt $starttime -or $current -gt $endtime)