0

下面的代码是显示我想要的数据,但它没有很好地对齐。如何使用数据网格或格式表来对齐数据?

$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!  
4

1 回答 1

2

如果您只想从 输出的对象中“修剪”一组属性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输入对象的属性值。

于 2021-10-08T18:00:05.993 回答