Powershell-如何在一轮中获得扩展属性和法线



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

Get-Item ABB667687BB581070240D48958F3BB0696921F09 | fl *
    PSChildName       : ABB667687BB581070240D48958F3BB0696921F09
    PSIsContainer     : False
            VersionInfo       : File:            \XYZABB667687B581070240D48958F3BB0696921F09
            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,产品,产品版本,文件版本,LastAccessTime。我该怎么做?

尝试过类似的东西:

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

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

就我个人而言,我觉得计算属性非常有用。其他解决方案(如添加成员)也适用。这是一个不错的参考不同的选择。

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

#Show Fullname, Name, and a few nested VersionInfo props for an object.
Get-Item 'C:Program FilesInternet Exploreriexplore.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 FilesInternet Exploreriexplore.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。谷歌围绕这个话题进行了许多讨论:)

干杯!

powershell一次只允许扩展一个属性。你不能在一轮中做你想做的事。你所能做的就是创建一个新的psobject:

$file=ls c:tempputty.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

您可以使用Tee对象以及计算的属性。假设您想从PowerShell.exe中获取这些值。然后,按以下方式执行:

gi $PSHOMEpowershell.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 

最新更新