2014-07-15 62 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