Powershell递归地列出文件属性到csv

Powershell list recursively files attributes to csv

我正在尝试用文件夹中包含的文件的所有属性填充一个 csv 文件。我在恢复文件版本和程序集时遇到问题

$arr = @()
gci C:\Temp -recurse | ? {$_.PSIsContainer -eq $False} | % {
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Directory $_.DirectoryName
$obj | Add-Member NoteProperty Name $_.Name
$obj | Add-Member NoteProperty Size (Get-Item $_.Length/1MB)
$obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
$obj | Add-Member NoteProperty LastAccess $_.LastAccessTime 
$obj | Add-Member NoteProperty Extension $_.Extension
$obj | Add-Member NoteProperty Creation $_.CreationTime
$obj | Add-Member NoteProperty LastWrite $_.LastWriteTime
$obj | Add-Member NoteProperty ReadOnly $_.IsReadOnly
$obj | Add-Member NoteProperty FullName $_.FullName
$obj | Add-Member NoteProperty Date (Get-Date -format "yyyy-MM-d HH:mm")
$obj | Add-Member NoteProperty Version ($_.FileVersion)
$obj | Add-Member NoteProperty Assembly ($_.AssemblyVersion)
$arr += $obj
}
$arr | Export-CSV -notypeinformation "c:\temp\File\report.csv"

为了获得文件版本,你必须使用这个:

$obj | Add-Member NoteProperty Version ($_.VersionInfo.FileVersion)

您无法从 VersionInfo 获取 AssemblyVersion

试试这个

$arr = @()
gci C:\Temp -recurse -File -Filter *.dll |  % {
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Directory $_.DirectoryName
$obj | Add-Member NoteProperty Name $_.Name
$obj | Add-Member NoteProperty Size (Get-Item $_.Length/1MB)
$obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
$obj | Add-Member NoteProperty LastAccess $_.LastAccessTime 
$obj | Add-Member NoteProperty Extension $_.Extension
$obj | Add-Member NoteProperty Creation $_.CreationTime
$obj | Add-Member NoteProperty LastWrite $_.LastWriteTime
$obj | Add-Member NoteProperty ReadOnly $_.IsReadOnly
$obj | Add-Member NoteProperty FullName $_.FullName
$obj | Add-Member NoteProperty Date (Get-Date -format "yyyy-MM-d HH:mm")
$obj | Add-Member NoteProperty Version ($_.VersionInfo.FileVersion)
$obj | Add-Member NoteProperty Assembly ([Reflection.AssemblyName]::GetAssemblyName($_.FullName).Version)
$arr += $obj
}
$arr | Export-CSV -notypeinformation "c:\temp\File\report.csv"

你可以像这样简化你的代码

gci C:\Temp -recurse -File -Filter *.dll |  % {
New-Object PSObject -Property @{
Directory= $_.DirectoryName
Name= $_.Name
Size= $_.Length/1MB
Owner= ((Get-ACL $_.FullName).Owner)
LastAccess= $_.LastAccessTime 
Extension= $_.Extension
Creation= $_.CreationTime
LastWrite= $_.LastWriteTime
ReadOnly= $_.IsReadOnly
FullName= $_.FullName
Date= (Get-Date -format "yyyy-MM-d HH:mm")
Version= ($_.VersionInfo.FileVersion)
Assembly= ([Reflection.AssemblyName]::GetAssemblyName($_.FullName).Version)
}
} | Export-CSV -notypeinformation "c:\temp\File\report.csv"

1)您遇到的问题是权限问题。尝试 运行 在提升模式下运行脚本(运行 以管理员身份)。 2) 你不应该试图在 C:\temp 文件夹下保存任何东西。最好在 D:\ 或 E:\ 中创建一个文件夹并将其作为 D:\temp_dump\report.csv 放在那里 3) 如果您将 get-item 放在大小上,那么这不是有效的,因为您必须选择文件夹中的任何内容。

下面的脚本工作正常。

##########################################################

$arr = @()
$Folder_path="E:\PS"

gci $Folder_path | ? {$_.PSIsContainer -eq $False} | % {

$obj = New-Object PSObject

$obj | Add-Member NoteProperty Directory $_.DirectoryName

$obj | Add-Member NoteProperty Name $_.Name

$obj | Add-Member NoteProperty Size (Get-ChildItem $Folder_path | Measure-Object -property length -sum)

$obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)

$obj | Add-Member NoteProperty LastAccess $_.LastAccessTime 

$obj | Add-Member NoteProperty Extension $_.Extension

$obj | Add-Member NoteProperty Creation $_.CreationTime

$obj | Add-Member NoteProperty LastWrite $_.LastWriteTime

$obj | Add-Member NoteProperty ReadOnly $_.IsReadOnly

$obj | Add-Member NoteProperty FullName $_.FullName

$obj | Add-Member NoteProperty Date (Get-Date -format "yyyy-MM-d HH:mm")

$obj | Add-Member NoteProperty Version ($_.FileVersion)

$obj | Add-Member NoteProperty Assembly ($_.AssemblyVersion)

$arr += $obj

}

$arr | Export-CSV -notypeinformation "E:\report.csv"
#