下面的代码显示了我想要的数据,但是它没有很好地对齐。如何使用数据网格或格式表来对齐数据?
$response = Invoke-RestMethod @params
foreach ( $row in $response )
{
Write-host "id=$($row.uuid) uuid=$($row.uuid) Subject=$($row.subject) "
}
现在的输出示例:
id=new-template_1 uuid=new-template_1 Subject=Welcome!
id=new-template_3 uuid=new-template_3 Subject=Welcome!
所需:
id uuid subject
new_template_1 new_template_1 Welcome!
new_template_3 new_template_3 Welcome!
如果你只是想要一个"trimmed"从Invoke-RestMethod
输出的对象中获取属性集,使用Select-Object
:
$response = Invoke-RestMethod @params
$trimmedResponse = $response |Select-Object @{Name='id';Expression='uuid'},uuid,subject
# This will now default to table view formatting
$trimmedResponse
# But you can also explicitly pipe to `Format-Table`
$trimmedResponse |Format-Table
# ... allowing you greater control over the formatting behavior
$trimmedResponse |Sort-Object Subject |Format-Table -GroupBy Subject
传递给Select-Object
(@{Name='id';Expression='uuid'}
)的第一个参数被称为计算属性-在本例中,它定义了一个名为id
的新属性,其值为输入对象的uuid
属性。