从循环输出到CSV文件的正确做法是什么?我的目标是检查所有文件的属性,这些文件匹配一个月内未访问过的一系列扩展,并输出到csv文件以供稍后阅读。
它只输出第一个文件的信息。并错误如下:
Add-Member : Cannot add a member with the name "FileName" because a member with that name already exists. If you want to overwrite the member a
nyway, use the Force parameter to overwrite it.
At C:\Users\claw\Desktop\checkFileAge.ps1:10 char:25
+ $out2csv | add-member <<<< -MemberType NoteProperty -Name FileName -Value $i.fullname
+ CategoryInfo : InvalidOperation: (@{FileName=C:\u...012 9:33:46 AM}:PSObject) [Add-Member], InvalidOperationException
+ FullyQualifiedErrorId : MemberAlreadyExists,Microsoft.PowerShell.Commands.AddMemberCommand
以下是我的例子:
$out2csv = New-Object PSObject;
foreach ($i in get-childitem c:\users -recurse -include *.doc, *.xls, *.ppt, *.mdb, *.docx, *.xlsx, *.pptx, *.mdbx, *.jpeg, *.jpg, *.mov, *.avi, *.mp3, *.mp4, *.ogg)
{if ($i.lastaccesstime -lt ($(Get-Date).AddMonths(-1)))
{
$out2csv | add-member -MemberType NoteProperty -Name FileName -Value $i.fullname
$out2csv | add-member -MemberType NoteProperty -Name LastAccess -Value $i.LastAccessTime
}
} $out2csv | Export-Csv "C:\FileAccessInformation.csv" -NoTypeInformation -Force
答案 0 :(得分:2)
尝试这样的事情。这是一个更典型的PowerShell。
$ext = '*.doc', '*.xls',...
Get-ChildItem C:\Users -r -inc $ext |
Where {$_.LastAccessTime -lt [DateTime]::Now.AddMonths(-1)} |
Select FullName, LastAccessTime |
Export-Csv -NoTypeInformation -Force C:\FileAccessInformation.csv