我正在尝试运行一个PowerShell脚本,它遍历系统中的所有文件,并创建一个文本文件,每行一个文件。我需要名称,目录,大小,版本和最后修改。下面的代码在版本5中运行良好(如果访问被拒绝,它将抛出错误)......但不是在版本2 ....
Get-ChildItem -Path . -Recurse |
foreach-object {
[pscustomobject]@{
Name = $_.fullname;
DateModified = $_.LastWriteTime;
Version = $_.VersionInfo.FileVersion;
Length = $_.length;
}
} | Export-Csv -Path "c:\workingfiles\post.csv" -NoTypeInformation
每条线看起来像:
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","4"
如何在版本2中获得我需要的内容? (无法升级)
-Ken
答案 0 :(得分:1)
不是将哈希表投射到pscustomobject
,而是调用New-Object -Property
:
Get-ChildItem -Path . -Recurse |ForEach-Object {
New-Object psobject -Property @{
Name = $_.FullName;
DateModified = $_.LastWriteTime;
Version = $_.VersionInfo.FileVersion;
Length = $_.Length;
}
} | Export-Csv -Path "c:\workingfiles\post.csv" -NoTypeInformation