哈希表数组的格式表

时间:2018-09-10 18:19:02

标签: powershell

我正在尝试使用Format-Table命令输出从TFS存储库中检出的所有文件的哈希表数组。

到目前为止,我的代码:

$arr = @();
#Take the string from the tf command, parse it and build an array of hash tables
(tf stat /recursive /user:* /format:detailed | Select-String -Pattern '^\$' -NotMatch | Select -SkipLast 3 | Out-String) -split '(\r\n){2}' | ForEach-Object {
    $ht = @{};
    if ($_ -ne '') {
        $str = $_ | Out-String;
        $str -split '\r?\n'| ForEach-Object {
            $key, $value = $_ -split '\s*:\s*';
            #Write-Host $key, $Value;
            try {
                $ht.Add($key, $value);
            } catch [ArgumentException] {
                Write-Host "Caught exception";
            }
        }
        $arr += ($ht);
    }
}

修改 看来我在这里错了。

$arr.ForEach({[PSCustomObject]$_}) | Format-Table -AutoSize

完全错误:

Cannot convert value "System.Collections.Hashtable" to type
"System.Management.Automation.LanguagePrimitives+InternalPSCustomObject". Error: "Cannot process argument because the
value of argument "name" is not valid. Change the value of the "name" argument and run the operation again."
At C:\Dev\Tools\powershell\Convert-TfsOutput.ps1:21 char:15
+ $arr.ForEach({[PSCustomObject]$_}) | Format-Table -AutoSize
+               ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastConstructorException

Edit2

当我将上述行替换为:

时,这是示例输出
$arr.ForEach({ $_ | Out-String })

Name                           Value
----                           -----
  Workspace                    work1
  Date                         {Wednesday, September 5, 2018 1, 38, 48 PM}
  Local item                   file1
  File type                    Windows-1252
  User                         user1
  Lock                         none

  Change                       edit




Name                           Value
----                           -----
  Workspace                    work2
  Date                         {Monday, September 10, 2018 12, 14, 56 PM}
  Local item                   file2
  User                         user2
  Lock                         none

  Change                       edit

编辑3

以下命令的输出

Write-Host $str;

  User       : User1
  Date       : Wednesday, September 5, 2018 1:38:48 PM
  Lock       : none
  Change     : edit
  Workspace  : Work1
  Local item : File1
  File type  : Windows-1252




  User       : User2
  Date       : Monday, September 10, 2018 12:14:56 PM
  Lock       : none
  Change     : edit
  Workspace  : Work2
  Local item : File2

想要以表格格式在列名下方显示行的输出:

Workspace | Date | Local item | File type | User | Lock | Change

试图在另一个答案中使用该代码,但无法正确输出。 Format-Table on Array of Hash Tables

1 个答案:

答案 0 :(得分:2)

将哈希表转换为自定义对象,然后再将其传递给Format-Table

... | Where-Object { $_ } | ForEach-Object {
    $ht = @{};
    ($_ | Out-String) -split '\r?\n'| ForEach-Object {
        ...
    }
    New-Object -Type PSObject -Property $ht
} | Format-Table

编辑::您的输入数据看起来像是空行,导致哈希表中的键带有空字符串,这会导致您观察到错误,因为对象的属性不能为空名称的字符串。

将您的哈希表/对象创建更改为以下内容:

... | Where-Object { $_ } | ForEach-Object {
    $ht = ($_ | Out-String).Trim() -replace '\s+:\s+', '=' |
          ConvertFrom-StringData
    New-Object -Type PSObject -Property $ht
} | Format-Table