【问题标题】:Powershell Property Value Data parsePowershell 属性值数据解析
【发布时间】:2014-03-11 14:41:18
【问题描述】:

我想创建一个 powershell 脚本来从特定文件的特定属性中提取特定数据。所以简而言之。我想从 .exe 的 versioninfo 属性中获取 FileVersion 数据。

我正在使用以下命令来获取文件并从上面查看属性值。

dir c:\windows\system32\dfc.exe | fl versioninfo

输出如下

VersionInfo : File:             C:\windows\system32\dfc.exe
          InternalName:     dfcmnd.exe
          OriginalFilename: DFC.exe
          FileVersion:      7,30,220,3852
          FileDescription:  Command line utility for Deep Freeze 7.00
          Product:          Deep Freeze 7.00
          ProductVersion:   7.30.220.3852
          Debug:            False
          Patched:          False
          PreRelease:       False
          PrivateBuild:     False
          SpecialBuild:     False
          Language:         English (United States)

但我想要的只是

Fileversion: 7.30.220.3852

我想不出办法只取出这些数据并丢弃其余数据。

谢谢。

【问题讨论】:

    标签: parsing powershell dir


    【解决方案1】:

    如果您只想要值,那么只需访问属性:

    PS D:\> (get-item c:\windows\system32\write.exe).VersionInfo.FileVersion
    6.1.7600.16385 (win7_rtm.090713-1255)
    

    注意如果您使用通配符来匹配多个文件,这将不适用于 Powershell v2,但它可以用于更新的 Powershell 版本。

    如果您想要一个只有 FileVersion 属性的对象,请使用 select:

    PS D:\> (get-item c:\windows\system32\write.exe).VersionInfo | select FileVersion
    
    FileVersion                                                                                                                                                                       
    -----------                                                                                                                                                                       
    6.1.7600.16385 (win7_rtm.090713-1255)                                                                                                                                             
    

    如果您希望属性名称和值在同一行上,则使用format-list 格式化输出:

    PS D:\> (get-item c:\windows\system32\write.exe).VersionInfo | select FileVersion | fl
    
    
    FileVersion : 6.1.7600.16385 (win7_rtm.090713-1255)
    

    【讨论】:

      【解决方案2】:

      fl 是格式列表的别名。在大多数情况下,应该在输出结果之前使用格式命令。

      如果您想选择特定的属性,您可以使用“.”操作符来完成。在你的情况下:

      (dir c:\windows\system32\dfc.exe).versioninfo.fileversion
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-14
        • 1970-01-01
        • 1970-01-01
        • 2017-01-29
        • 1970-01-01
        • 2021-12-13
        • 1970-01-01
        相关资源
        最近更新 更多