我的问题是,如何在变量中存储日期,然后推送新日期。 我会解释我的代码: - 记住今天的日期,并从文件的最后写入时间中减去它 - 小时数 - 最后写入时间大于或等于1小时通知编辑文件 - 最近的写作时间 - 然后睡10秒
问题是,即使我编辑文件,循环仍然显示上一个日期。我怎样才能使它工作?
$file = get-childitem $input_users
$time = (get-date)-$file.lastwritetime
$hours = $time.hours
while (($time.hours) -ge (1)){
write-host -BackgroundColor DarkRed -ForegroundColor White "$input was written to $hours hours ago, please edit it first"
write-host -BackgroundColor DarkRed -ForegroundColor White "Next check in 10 seconds."
$time = (get-date)-$file.lastwritetime
}
答案 0 :(得分:1)
您需要获取一个新文件对象并重新计算循环内的小时数:
$file = get-childitem $input_users
$time = (get-date)-$file.lastwritetime
$hours = $time.hours
while (($time.hours) -ge (1)){
write-host -BackgroundColor DarkRed -ForegroundColor White "$input was written to $hours hours ago, please edit it first"
write-host -BackgroundColor DarkRed -ForegroundColor White "Next check in 10 seconds."
$file = Get-ChildItem $input_users
$time = (Get-Date) - $file.LastWriteTime
}