簡體   English   中英

如何在 PowerShell 中將 JSON 輸出格式化為 Format-Table 的網格

[英]How to format JSON Output into a Grid for Format-Table in PowerShell

下面的代碼顯示了我想要的數據,但沒有很好地對齊。 如何使用數據網格或格式表來對齊數據?

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

如果您只想從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屬性的值為輸入對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM