我试图弄清楚如何在文件夹中打印文件路径的列表,以及它们随附的校验和以及最后修改的日期。
我可以获得文件路径和校验和的列表:Get-ChildItem -Recurse | Get-FileHash | Export-Csv -Path C:\Temp\ListOfHashes.csv
我还可以获得文件路径,校验和和最后修改日期的列表:
Get-ChildItem -File -Recurse |
Select DirectoryName,Name,@{N='Version';E={$_.VersionInfo.ProductVersion}},LastWriteTime,Length,@{N='FileHash';E={(Get-FileHash $_).Hash}} | Export-Csv -Path c:\temp\test11.csv
但是奇怪的是,第一个脚本(仅打印哈希值)对每个文件都有一个哈希值。第二个脚本显示文件路径,最后修改日期和哈希,但并不总是包含哈希。有人知道为什么吗?
![enter image description here](https://i.sstatic.net/kW555.png)
我发现了问题所在:如果文件打开,它将无法写入哈希。该脚本以及下面的脚本都可以工作。
编辑:菲利普·弗里(Philip Fourie)的加入至关重要:
Get-ChildItem -File -Recurse |
Select DirectoryName,Name,@{N='Version';E={$_.VersionInfo.ProductVersion}},LastWriteTime,Length,@{N='FileHash'; E={(Get-FileHash -LiteralPath $_.FullName).Hash}} | Export-Csv -Path c:\temp \test789.csv`
最佳答案
这是你所追求的吗?
Clear-Host
Get-ChildItem -Path 'D:\temp' -File |
Select-Object -Property * -First 3 |
ForEach{
[PSCustomObject]@{
'BaseName' = $PSItem.BaseName
'FullName' = $PSItem.FullName
'Length' = $PSItem.Length
'LastWriteTime' = $PSItem.LastWriteTime
'HashCode Based on Name' = $PSItem.GetHashCode()
'HashCode Based on FullName' = $PSItem.FullName.GetHashCode()
'File Hash Only' = (Get-FileHash -Path $PSItem.FullName).Hash
'File Hash' = Get-FileHash -Path $PSItem.FullName
}
}
<#
# Results
BaseName : 23694d1213305764-revision-number-in-excel-book1
FullName : D:\temp\23694d1213305764-revision-number-in-excel-book1.xls
Length : 28817
LastWriteTime : 06-Feb-20 14:02:47
HashCode Based on Name : 62001128
HashCode Based on FullName : -1676880214
File Hash Only : A3CB4415D3FAAAB38A6F7A8D959F9BE08C2E06B9A21DFC8DFEA7F0387D6F231A
File Hash : @{Algorithm=SHA256; Hash=A3CB4415D3FAAAB38A6F7A8D959F9BE08C2E06B9A21DFC8DFEA7F0387D6F231A;
Path=D:\temp\23694d1213305764-revision-number-in-excel-book1.xls}
BaseName : 5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube
FullName : D:\temp\5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url
Length : 69
LastWriteTime : 29-Dec-19 21:50:56
HashCode Based on Name : 62001128
HashCode Based on FullName : -1214175701
File Hash Only : 3427AD8DC44986F90F22FCCAEB108E32214A01F4917BC4F7AA159E547169BB2F
File Hash : @{Algorithm=SHA256; Hash=3427AD8DC44986F90F22FCCAEB108E32214A01F4917BC4F7AA159E547169BB2F; Path=D:\temp\5 Free Software You'll Wish You Knew Earlier!
2019 - YouTube.url}
BaseName : abc
FullName : D:\temp\abc.txt
Length : 70
LastWriteTime : 05-Mar-20 16:05:56
HashCode Based on Name : 62001128
HashCode Based on FullName : -808200336
File Hash Only : 63231FC845361CF6AD167D63782778DDB0528F39A188893AE6E2D8CA1F3362A6
File Hash : @{Algorithm=SHA256; Hash=63231FC845361CF6AD167D63782778DDB0528F39A188893AE6E2D8CA1F3362A6; Path=D:\temp\abc.txt}
#>
关于powershell - 是否可以同时打印文件字符串,文件哈希和上次修改日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60555712/