从循环输出到 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