簡體   English   中英

如何在PowerShell中枚舉Format-Table條目?

[英]How to enumerate a Format-Table entry in PowerShell?

我有一個哈希數組,我希望將該數組格式化為一個表,枚舉每個條目而不實際添加哈希項來包含此信息。 例如

$id = 0
$items = @( @{ item='a' }, @{ item='b' }, @{ item='c' })
$items |
 %{ new-object PSObject -Property $_ } |
  Format-Table @{ n=""; e={ "{0}" -f ++$id }; a="left" },item

我期待着:

  item
- ----
1 a   
2 b   
3 c   

但得到:

  item
- ----
1 a   
1 b   
1 c   

似乎$id是按值傳遞的。 需要使用[ref]強制它作為參考,它會起作用。 例如:

[ref]$id = 0
$items = @( @{ item='a' }, @{ item='b' }, @{ item='c' })
$items | %{ new-object PSObject -Property $_ } |
 Format-Table @{ n=""; e={ "{0}" -f ++$id.value }; a="left" },item

暫無
暫無

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

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