清理文件共享伺服器

清理文件共享伺服器

我一直在嘗試製作一個PowerShell 腳本,列出所有文件和資料夾、它們的創建日期和上次訪問日期、大小(以MB 為單位),如果可能的話,還列出最後訪問它的人,並導出到CSV 檔案。

我已經測試了一些腳本,甚至從其他帖子中得到了幫助,但它並沒有提取所有文件。

這是我目前正在運行的腳本:

 Get-ChildItem c:\Users\iceledon -Recurse -ErrorAction SilentlyContinue |  Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-365)} |
Export-Csv "C:\Users\iceledon\Desktop\files.csv" -NoTypeInformation

答案1

這是您可以從文件屬性中獲得的全部內容。選擇的第一個文件的

# This is all you can get from file properties. of the first file selected
(Get-ChildItem 'd:\temp\*.txt')[0] | Select-Object -Property *

# Results

PSPath            : Microsoft.PowerShell.Core\FileSystem::D:\temp\1 passwordchangelog.txt
PSParentPath      : Microsoft.PowerShell.Core\FileSystem::D:\temp
PSChildName       : 1 passwordchangelog.txt
PSDrive           : D
PSProvider        : Microsoft.PowerShell.Core\FileSystem
PSIsContainer     : False
Mode              : -a----
VersionInfo       : File:             D:\temp\1 passwordchangelog.txt
                    InternalName:     
                    OriginalFilename: 
                    FileVersion:      
                    FileDescription:  
                    Product:          
                    ProductVersion:   
                    Debug:            False
                    Patched:          False
                    PreRelease:       False
                    PrivateBuild:     False
                    SpecialBuild:     False
                    Language:         

BaseName          : 1 passwordchangelog
Target            : {}
LinkType          : 
Name              : 1 passwordchangelog.txt
Length            : 24
DirectoryName     : D:\temp
Directory         : D:\temp
IsReadOnly        : False
Exists            : True
FullName          : D:\temp\1 passwordchangelog.txt
Extension         : .txt
CreationTime      : 10-Jul-18 16:30:22
CreationTimeUtc   : 10-Jul-18 23:30:22
LastAccessTime    : 10-Jul-18 16:30:22
LastAccessTimeUtc : 10-Jul-18 23:30:22
LastWriteTime     : 06-Jul-18 22:16:24
LastWriteTimeUtc  : 07-Jul-18 05:16:24
Attributes        : Archive

從文件物件的上述屬性可以看出。沒有關於誰最後訪問它的資訊。最後你必須將長度轉換為KB、MB等。

所以,你這樣做...(只是不要使用格式表進行輸出。這僅適用於螢幕)

Get-ChildItem 'd:\temp' -Recurse -ErrorAction SilentlyContinue `
| Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-365)} `
| Select FullName,CreationTime,LastAccessTime,
    @{Name='Size(kb)';Expression={“{0:F2}” -f ($_.length/1KB)}},
    @{Name='Size(mb)';Expression={“{0:F2}” -f ($_.length/1MB)}} `
| Sort-Object -Property LastAccessTime `
| Format-Table -AutoSize

FullName                                       CreationTime       LastAccessTime     Size(kb) Size(mb)
--------                                       ------------       --------------     -------- --------
D:\temp\4 passwordchangelog.txt                05-Jul-18 13:15:04 05-Jul-18 13:15:04 0.02     0.00    
D:\temp\1 passwordchangelog.txt                10-Jul-18 16:30:22 10-Jul-18 16:30:22 0.02     0.00    
D:\temp\10 passwordchangelog.txt               10-Jul-18 16:30:26 10-Jul-18 16:30:26 0.02     0.00 
...

你是什​​麼意思...

但它不會拉取所有文件。

只要您有權限,GCI 就會提供您要求的所有文件。如果您無論如何進行過濾,這就是將返回的全部內容。

加入天數(-365)

這意味著,只給我舊的文件。因此,任何不早於設計的東西,你都不會得到。

相關內容