Powershell - 如何在一轮中获得扩展属性和正常属性?

时间:2014-09-12 08:31:11

标签: powershell

我有一个文件服务器,文件以加密名称保存。现在我想做那样的事情:

Get-Item ABB667687BB581070240D48958F3BB0696921F09 | fl *

    PSChildName       : ABB667687BB581070240D48958F3BB0696921F09
    PSIsContainer     : False
            VersionInfo       : File:            \\XYZ\ABB667687B581070240D48958F3BB0696921F09
            InternalName:     Setup.exe
            OriginalFilename: Setup.exe
            FileVersion:      5,2,0,2946
            FileDescription:  Evernote Installation Package
            Product:          Evernote®
            ProductVersion:   5,2,0,2946
            ...continuing

    BaseName          : ABB667687BB581070240D48958F3BB0696921F09
    Mode              : -a---
    Name              : ABB667687BB581070240D48958F3BB0696921F09
    Length            : 83157856
    ...continuing
    LastAccessTime    : 05.03.2014 08:23:58

当我尝试使用以下

get-Item ABB667687BB581070240D48958F3BB0696921F09 | Select-Object -Expand VersionInfo | fl *

CompanyName        : Evernote Corp., 305 Walnut Street, Redwood City, CA 94063
FileVersion        : 5,2,0,2946
ProductVersion     : 5,2,0,2946
ProductName        : Evernote®

现在我想结合PSCHild,产品,产品版本,Fileversion,LastAccessTime。我怎么能这样做?

尝试使用类似的东西:

Get-Item ABB667687BB581070240D48958F3BB0696921F09 | ft -property @{n="Filename" ; e={$_.pschildname}}, @{n="Fileinformation";e={get-Item ABB667687BB581070240D48958F3BB0696921F09 | Select-Object -Expand VersionInfo | ft product}}

3 个答案:

答案 0 :(得分:1)

powershell允许一次仅扩展一个属性。 你不能在一个“圆”中做你想做的事。 你可以做的是创建一个像这样的新psobject:

$file=ls c:\temp\putty.exe
$inf=$file |select -expand versioninfo
$myobj="dummy" | select child,product,version,write

$myobj.product=$inf.ProductName
$myobj.version=$inf.ProductVersion
$myobj.child=$file.PSChildName
$myobj.write=$file.LastWriteTime

答案 1 :(得分:0)

首先,你绝对可以使用类似于Kayasax的解决方案。

就个人而言,我发现calculated properties非常有帮助。其他解决方案,如Add-Member也可以使用。关于不同选项的This is a decent reference

话虽如此,这里有一段代码,涵盖了你想要的一些东西(根据需要进行调整):

#Show Fullname, Name, and a few nested VersionInfo props for an object.
Get-Item 'C:\Program Files\Internet Explorer\iexplore.exe' |
    Select-Object -Property FullName,
        Name,
        @{ label = "CompanyName"; expression = {$_.VersionInfo.CompanyName} },
        @{ label = "FileVersion"; expression = {$_.VersionInfo.FileVersion} },
        @{ label = "ProductVersion"; expression = {$_.VersionInfo.ProductVersion} },
        @{ label = "ProductName"; expression = {$_.VersionInfo.ProductName} }

<#  Output

    FullName       : C:\Program Files\Internet Explorer\iexplore.exe
    Name           : iexplore.exe
    CompanyName    : Microsoft Corporation
    FileVersion    : 11.00.9600.16384 (winblue_rtm.130821-1623)
    ProductVersion : 11.00.9600.16384
    ProductName    : Internet Explorer
#>

最后,作为警告,您应该很少使用Format-Table(ft)。这会生成无法使用的文本,仅供您查看。您可能会在详细输出中看到它,但如果您计划再次使用该数据,则不应使用Format-Table。谷歌围绕这个话题进行了很多讨论:)

干杯!

答案 2 :(得分:0)

您可以使用Tee-Object以及计算属性。假设您要从PowerShell.exe获取这些值。然后,这样做:

gi $PSHOME\powershell.exe | 
    tee -var pow | 
    select name, 
        @{n='Product';e={$pow.versioninfo.productname}}, 
        @{n='Product Version';e={$pow.versioninfo.productversion}}, 
        @{n='FileVersion';e={$pow.versioninfo.fileversion}}, 
        lastaccesstime | 
    fl