Powershell在日期之间获取文件(Get files between two dates in PowerShell)

编程入门 行业动态 更新时间:2024-10-21 09:33:59
Powershell在日期之间获取文件(Get files between two dates in PowerShell)

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

$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 }

我只得到第一个日志文件并继续错误:

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

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

I need to select files in a folder only between the first day in the current month and the last day in the current month. I tried:

$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 }

I get only the first log file and continue error:

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

The second problem: only from the first level of the path (no deep recursion).

最满意答案

$path = "\\logserver\C$\WINDOWS\SYSTEM32\LOGFILES" $Include=@("*.log") $cntDate = Get-Date # First day of current month $firstDayMonth = Get-Date -Day 1 -Month $cntDate.Month -Year $cntDate.Year -Hour 0 -Minute 0 -Second 0 # Last day of 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 }

我用一个nother unc路径测试它,它的工作原理! 因此,您提供"C:\Windows\System32\LogFiles"的示例只能由System访问。

$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 }

I tested it with another UNC path and it works! So the example you give "C:\Windows\System32\LogFiles" is accessible only to System.

更多推荐

本文发布于:2023-07-05 06:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034399.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   文件   Powershell   PowerShell   dates

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!