2016-08-03 36 views
1

我對此很新,它讓我發瘋。我試圖在我們的文件服務器上獲取超過15年(5475天)的文件夾創建日期列表,並將其導出到Excel。PowerShell中的文件夾創建日期列表

我已經嘗試了幾種不同的方法和跨一個我以爲是工作來了,但事實證明,它只是拋出所有文件夾日期爲.csv文件笑我知道這個代碼是可能是一個災難,所以我正在尋找一些幫助。非常感謝:)

Function Get-NeglectedFiles {  
    Param([string[]]$path, [int]$numberDays) 

    $cutOffDate = (Get-Date).AddDays(-$numberDays) 

    Get-ChildItem -Recurse -Path $path 
    Where-Object { $_.CreationTime -le $cutOffDate -and { $_.Attributes -eq 'Directory' } } 
} 

Get-NeglectedFiles -path "S:\Test" -numberDays 2000 | 
    Select-Object FullName, CreationTime | 
    Export-Csv -Path C:\Scripts\Test.csv -NoTypeInformation 
+3

你缺少 「|」 Get-ChildItem和Where-Object之間。你的過濾器從來沒有申請,因爲它沒有任何工作。 'Get-ChildItem -Recurse -Path $ path | Where-Object ...' –

+0

或者你可以簡化一下命令'Get-ChildItem -Recurse -Path $ path -Attributes Directory | Where-Object CreationTime -le $ cutOffDate' – Honzajscz

+0

哦......哈。謝謝你。我會給它一個鏡頭。 編輯:CreationTime一塊工作完美!謝謝。但它抓取所有文件類型,而不僅僅是目錄。我必須嘗試清理那部分。 –

回答

1

試試這個

Function Get-NeglectedFiles 
{ 
    Param(
    [string[]]$path, 
    [int]$numberDays) 

    $cutOffDate = (Get-Date).AddDays(-$numberDays) 
    Get-ChildItem -Recurse -Path $path -Attributes Directory | 
    Where-Object CreationTime -le $cutOffDate 
} 
+0

嗨,你可以請包括一個簡短的解釋與代碼?它可能看起來很明顯,但對OP來說似乎並不一樣。 – sodawillow

+0

'$ cutOffDate =(Get-Date).AddDays( - $ numberDays)'從當前日期中減去所需的天數。 'Get-ChildItem -Recurse -Path $ path -Attributes Directory'通過'$ path'的所有子目錄,並且只返回目錄(省略文件)。這些目錄然後被傳送到 'Where-Object CreationTime -le $ cutOffDate',它保持屬性'CreationTime'小於'$ cutOffDate'的目錄。結果然後隱式地從函數返回。明白了嗎? – Honzajscz

+0

謝謝你的回覆。我試過了,並且出現一個錯誤,說 「Get-ChildItem:找不到與參數名稱'Attributes'匹配的參數。」 它可能是PowerShell版本問題嗎? –

相關問題