arrays - 将数组对象导出到 CSV 文件
问题描述
我正在尝试将数组对象列表输出到 CSV 文件。但是,我希望将目录、名称、lastwritetime 和所有者放在单独的列中,而不是将它们合并到一个列中。不确定,如何解决这种情况。请参阅下面的当前代码。输出到 gridview 和 text 工作正常,但不是 csv 输出。
谢谢。
$Path = "A:\Test"
$PathArray = @()
$Results = "A:\Test\test_results_7.csv"
$extension = "xml"
# This code snippet gets all the files in $Path that end in extension parameter.
# where the file update was made in the last 30 days
Get-ChildItem $Path -Filter "*.$extension" -recurse |
Where-object { $_.LastWriteTime -ge (Get-Date).AddDays(-30)} |
ForEach-Object {
$PathArray += -join ($_.Directory , " , " , $_.Name , " , " , $_.LastWriteTime , " , " , ((Get-ACL $_.Fullname).Owner) )
}
$PathArray += -join ("Path Location" , " , " , "File name" , " , " , "Last Write Time" , " , " , "Owner" )
#$PathArray | Out-GridView
$PathArray | select-object $PathArray | ForEach-Object {$_} | Export-csv $Results
解决方案
尝试这个:
$folders = Get-ChildItem $Path -Filter "*.$extension" -recurse | Where-object {$_.LastWriteTime -ge (Get-Date).AddDays(-30)}
$outarray = @()
foreach($folder in $folders)
{
$outarray += New-Object PsObject -property @{
'Name' = $folder.FullName
'Directory' = $folder.Directory
'LastWrite' = $folder.LastWriteTime
}
}
$outarray | export-csv $results
我在这里得到了答案
推荐阅读
- azure - 在 Azure AD 中,如何在首次登录时排除一部分用户需要身份验证信息
- flutter - 如何在相机上添加矩形叠加层并使用颤振将图像保存到画廊
- c++ - 多重继承C++的钻石问题
- ios - Swift UI 测试无法在 iOS 12.3 之前的设备上启动
- tortoisegit - 如何在 TortoiseGit Git 克隆对话框中默认选中 LFS 复选框?
- c# - 从列表中获取 3 个项目的最大值
- apache-kafka - Kafka聚合:分组键更改时的问题
- ios - 如何设置tableViewHeader
- python - 当我的 CNN 模型不改变其准确性和损失并在任何输入图像中输出相同的预测时该怎么办
- kubernetes - 我很难在 gke 上设置 kafka 并且想知道设置它的最佳方式?