无法通过PowerShell脚本获取文件上次修改时间

时间:2015-10-01 15:53:30

标签: windows powershell

我尝试做的是搜索目录,将文件路径,名称,版本和上次修改时间输出到txt文件中。

我的代码如下:

    function Get-Version($filePath)
    {
       $name = @{Name="Name";Expression= {split-path -leaf $_.FileName}}
       $path = @{Name="Path";Expression= {split-path $_.FileName}}
       $time = @{Name="Last Modified"; Expression={Get-Date $_.LastWriteTime}}
       dir -recurse -path $filePath | % { if ($_.Name -match "(.*exe)$") {$_.VersionInfo} } | select $path, $name,$time, FileVersion
    }

    Get-Version('E:\PS test') >> "version_info.txt"

但输出txt具有名称,路径和版本,但没有上次修改时间。

任何提示?

谢谢!

1 个答案:

答案 0 :(得分:1)

这是因为您从.VersionInfoForEach-Object)调用返回%属性,.LastWriteTime是文件对象的属性,而不是版本信息。看看这个:

 function Get-Version($filePath)
{
   $name = @{Name="Name";Expression= {split-path -leaf $_.VersionInfo.FileName}}
   $path = @{Name="Path";Expression= {split-path $_.VersionInfo.FileName}}
   $time = @{Name="Last Modified"; Expression={Get-Date $_.LastWriteTime}}
   $version = @{Name="FileVersion"; Expression={$_.VersionInfo.FileVersion}}
   dir -recurse -path $filePath | ? { $_.Name -match "(.*exe)$" } | select $path, $name,$time, $version
}

通过更改$name$path的定义以直接引用版本信息,您可以对原始对象进行操作。我还有$version来查看您在选择中提到的FileVersion

这会使ForEach-Object变得多余,因为你只是传递了输入。由于您只是检查其中的条件,因此更容易将其转换为Where-Object?)。

扩展别名会使它看起来像这样:

function Get-Version($filePath)
{
    $name = @{Name="Name";Expression= {Split-Path -Leaf $_.VersionInfo.FileName}}
    $path = @{Name="Path";Expression= {Split-Path $_.VersionInfo.FileName}}
    $time = @{Name="Last Modified"; Expression={Get-Date $_.LastWriteTime}}
    $version = @{Name="FileVersion"; Expression={$_.VersionInfo.FileVersion}}
    Get-ChildItem -Recurse -Path $filePath | Where-Object { $_.Name -match "(.*exe)$" } | Select-Object $path, $name,$time, $version
}

但是我还应该指出,您可以直接在dirGet-ChildItem)中过滤文件名,使Where-Object也是多余的:

function Get-Version($filePath)
{
    $name = @{Name="Name";Expression= {Split-Path -Leaf $_.VersionInfo.FileName}}
    $path = @{Name="Path";Expression= {Split-Path $_.VersionInfo.FileName}}
    $time = @{Name="Last Modified"; Expression={Get-Date $_.LastWriteTime}}
    $version = @{Name="FileVersion"; Expression={$_.VersionInfo.FileVersion}}
    Get-ChildItem -Recurse -Path $filePath -Filter *.exe | Select-Object $path, $name,$time, $version
}

然后根据你的评论,我意识到它可以更简化:

function Get-Version($filePath)
{
    $path = @{Name="Path";Expression= {$_.DirectoryName}}
    $time = @{Name="Last Modified"; Expression={$_.LastWriteTime}}
    $version = @{Name="FileVersion"; Expression={$_.VersionInfo.FileVersion}}
    Get-ChildItem -Recurse -Path $filePath -Filter *.exe | Select-Object $path, Name,$time, $version
}
不需要

$name,因为文件对象已经具有名为.Name的属性,该属性具有文件名。

$path可以简化,因为$_.DirectoryName已经有了路径。

$time可以简化,因为.LastWriteTime属性已经是[DateTime],因此您不需要Get-Date

您仍需要名称/表达式哈希的唯一原因是将字段命名为基础属性以外的名称。如果你不关心这个,你可以这样做:

function Get-Version($filePath)
{
    $version = @{Name="FileVersion"; Expression={$_.VersionInfo.FileVersion}}
    Get-ChildItem -Recurse -Path $filePath -Filter *.exe | Select-Object DirectoryName, Name, LastWriteTime, $version
}
相关问题