2014-07-15 65 views
0

只需在当前月份的第一天和当月的最后一天之间选择文件夹中的文件。我想:Powershell在日期之间获取文件

$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

有对可能的解决方案,但想知道,有什么不好与第一代码... $ curr_month =(获得最新-format “MM-01-YYYY 0:00:00”) $ next_month =( get-date $ curr_moth).addmonths(1) ls \\ logserver \ C $ \ WINDOWS \ SYSTEM32 \ LOGFILES \ *。log |其中{$ _。CreationTime -ge $ curr_month-和$ _。CreationTime -lt $ next_month} – Jerry1

+0

不应该是$ _。where子句中的CreationTime.Month吗? –

+0

变量的名字'curr_month'有点令人困惑,但正如你从第一行看到的那样,si格式为MM/dd/yyyy(dd = 01),所以where子句是好的。奇怪的是,错误... – Jerry1

回答

1
$path = "\\logserver\C$\WINDOWS\SYSTEM32\LOGFILES" 
[email protected]("*.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 
     } 

我测试了它与一个unher unc路径,它的工作原理! 所以你给"C:\Windows\System32\LogFiles"的例子只能被System访问。

+0

谢谢,但我有同样的错误: Get-ChildItem:指定的网络名称不再可用。 是的,输出也给它。但是,这个错误令我惊讶...... – Jerry1

+1

尝试'Get-ChildItem -Path $ path -recurse -ErrorAction继续-ErrorVariable + gcierror' ...它应该包含您无权访问的所有文件夹的路径。 – Oli

+0

它是一个访问问题,代码工程.. – Oli