我正在尝试使用文件夹中包含的文件的所有属性来填充csv文件。我有麻烦恢复fileversion&装配
$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"
答案 0 :(得分:2)
试试这个
$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 :(得分:1)
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"
#
答案 2 :(得分:0)
为了获得fileversion,你必须使用它:
$obj | Add-Member NoteProperty Version ($_.VersionInfo.FileVersion)
您无法从VersionInfo
获取AssemblyVersion