我只需要在当月的第一天和当月的最后一天之间选择文件夹中的文件。我试过了:
$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
第二个问题:仅从路径的第一级(没有深度递归)。
答案 0 :(得分:2)
$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"
的示例只能由System访问。