Powershell 从pscustomobject的单个列格式化哈希表
我无法从哈希表JSON对象中提取键值Powershell 从pscustomobject的单个列格式化哈希表,powershell,Powershell,我无法从哈希表JSON对象中提取键值 $personnelresult = Invoke-RestMethod -Method Get www.url.com -WebSession $currentsession # Create Table Format $personnelresult = @{expression={$_.id};Label="ID";Width=37}, @{expressio
$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
我所尝试的:
我把这个放在桌子前面
$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}
这仍然可以将所有内容都放在一行上,并为以后在管道中处理数据提供更好的方法。除了下面的答案,您还可以查看Out GridView。