【问题标题】:Powershell - refresh date in variablePowershell - 变量中的刷新日期
【发布时间】:2012-07-24 10:35:03
【问题描述】:

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

【问题讨论】:

  • 问题应该移到 StackOverflow。
  • 嗯,SU 上有 302 个 PS 问题。在我看来,脚本是一种有效的高级用户活动。

标签: powershell


【解决方案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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多