首页 > 解决方案 > 基于小时和分钟的Powershell搜索文件?

问题描述

我有一个 powershell 脚本选择在特定时间之间创建的文件,如下所示

if ($a.CreationTime      -gt (get-date).date -and 
    $a.CreationTime.Hour -ge 14 -and 
    $a.CreationTime.Hour -le 15){...}

这将从当天以及 和 之间拾取2PM文件3PM

现在我想将时间范围更改为介于1:30 PM和之间3:30 PM,如何处理脚本中的分钟数?

标签: powershell

解决方案


这是一口但你不能使用:

Get-ChildItem | ForEach-Object{
    switch ($_.CreationTime.Hour){
        #creation hour is 1, check if minutes are over 30
        1 { if ($_.CreationTime.Minute -ge 30){ Write-Host $_; break} }
        #since hour is 2, no other check needed
        2 { Write-Host $_; break}
        #creation hour is 3, check if minutes is under 30
        3 { if ($_.CreationTime.Minute-le 30){ Write-Host $_; break } }
        #hour isn't 1,2 or 3 so do nothing
        default {break}
    }
}

推荐阅读