1

我的问题是,如何将日期存储在变量中,然后将新日期推送到那里。我将解释我的代码:-获取今天的日期并从文件的上次写入时间中减去它-获取小时数-当上次写入时间大于或等于 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
                                }  
4

1 回答 1

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

}
于 2012-07-24T12:16:02.353 回答