powershell - 从 pscustomobject 的单列格式化哈希表

标签 powershell

我无法从哈希表 JSON 对象中提取键值

$personnelresult = Invoke-RestMethod -Method Get www.url.com -WebSession $currentsession             
               # Create Table Format
$personnelresult = @{expression={$_.id};Label="ID";Width=37},
                   @{expression={$_.name};Label="Name";Width=32},
                   @{expression={$_.Atributes};Label="Attributes";Width=128}
               # Print Results
Write-Host "----------------" -ForegroundColor Green
Write-Host "Personnel report" -ForegroundColor Green
Write-Host "----------------" -ForegroundColor Green
$personnelresult.details | 
Format-Table $personnelresult
Write-Host "----------------------" -ForegroundColor Yellow
Write-Host "Count is limited to 15" -ForegroundColor Yellow
Write-Host "----------------------" -ForegroundColor Yellow
               # End Results

我得到这个:
ID        Name       Atrributes
----      ----       ----------
245       Joe        @{age=23; weight=200; height=73}
423       Brendan    @{age=25; weight=173; height=45}
213       Ashley     @{age=28; weight=350; height=20}

我想要的是这个或类似的东西
ID        Name       Attributes
----      ----       ----------
245       Joe        age=23 weight=200 height=73
423       Brendan    age=25 weight=173 height=45
213       Ashley     age=28 weight=350 height=20

我试过的:
我把这个放在我的 table 前
$personnelresult.details.Attributes = $personnelresult.details.Attributes | 
ForEach-Object {
Foreach($p in psobject.properties)
{
    $p.Name
}
}

而且,我会得到
ID        Name       Atrributes
----      ----       ----------
245       Joe        {System.Int32 age=23, System.Int32 weight=200, System.Int32 height=73}
423       Brendan    {System.Int32 age=25, System.Int32 weight=173, System.Int32 height=45}
213       Ashley     {System.Int32 age=28, System.Int32 weight=350, System.Int32 height=20}

如果在处理这些数据上有更好的做法或建议,请告诉我。此外,我正在处理比原始脚本中更长的字符串。这个想法是在控制台中的单个 ID 上显示所有这些信息。

最佳答案

好吧,如果想法是将所有内容都放在一个 ID 中,您可以这样做:

$personnelresult = @{e={$_.id};l="ID";Width=37},
                   @{e={$_.name};l="Name";Width=32},
                   @{e={$_.Atributes.age};l="Age";Width=32},
                   @{e={$_.Atributes.weight};l="Weight";Width=32},
                   @{e={$_.Atributes.height};l="Height";Width=32}

这仍然会让所有内容都集中在一条线上,并为您提供更好的方式来稍后在管道中操作数据。

关于powershell - 从 pscustomobject 的单列格式化哈希表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41305798/

相关文章:

json - 从JSON创建哈希表

powershell - 将 Powershell 脚本和文件包装在一起?

PowerShell 功能和性能

mysql - "You must provide a value expression on the right-hand side of the ' % ' operator."查询语句错误

azure - Powershell 获取所有 Azure 资源的每月消耗量

debugging - Powershell:如何实现标准开关?

Powershell:get-childitem 不适用于过滤器

regex - 使用正则表达式匹配多个更改

powershell - Get-ADUser 错误 : Invalid enumeration context

Powershell通过网络复制文件