首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PowerShell中获取两个日期之间的文件

在PowerShell中获取两个日期之间的文件
EN

Stack Overflow用户
提问于 2014-07-15 06:36:45
回答 1查看 9.2K关注 0票数 1

我只需要在当前月份的第一天和当前月份的最后一天之间选择文件夹中的文件。我试过:

代码语言:javascript
复制
$curr_month = (Get-Date -format "MM/01/yyyy 0:00:00")
$next_month = (Get-Date  $curr_moth).addmonths(1)
Get-ChildItem "\\logserver\C$\WINDOWS\SYSTEM32\LOGFILES\" -Recurse -include @("*.log") |
 where {$_.CreationTime -ge $curr_month -and
        $_.CreationTime -lt $next_month
        }

我只获得第一个日志文件并继续出错:

代码语言:javascript
复制
Get-ChildItem : The specified network name is no longer available.

At line:3 char:18
+     Get-ChildItem <<<<  "\\logserver\C$\WINDOWS\SYSTEM32\LOGFILES\" -Recurse -include @("*.log") |
    + CategoryInfo          : ReadError: (\\logserver\C$\WI...ES\WMI\RtBackup:String) [Get-ChildItem], IOException
    + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

第二个问题:仅从路径的第一级(没有深度递归)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-15 12:30:44

代码语言:javascript
复制
$path = "\\logserver\C$\WINDOWS\SYSTEM32\LOGFILES"
$Include=@("*.log")
$cntDate = Get-Date

# First day of the current month
$firstDayMonth = Get-Date -Day 1 -Month $cntDate.Month -Year $cntDate.Year -Hour 0 -Minute 0 -Second 0

# Last day of the current month
$lastDayOfMonth = (($firstDayMonth).AddMonths(1).AddSeconds(-1))

$firstDayMonth
$lastDayOfMonth

Get-ChildItem -Path $path -recurse -include "$Include" |
 where {$_.CreationTime -ge $firstDayMonth -and
        $_.CreationTime -lt $lastDayOfMonth
        }

我用另一个UNC路径测试了它,它工作了!因此,您给出的示例"C:\Windows\System32\LogFiles"仅可供系统访问。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24751417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档