Reputation: 73
I need to extract a Get command results into a CSV. The order column should be automatically generated upon a call and give each object its counter upon its appearance in the list. Would this be possible?
For example, when I'd do something like
Get-VMHost | Select @{N="Order";E={$suborder++}}, Name, Version, Build | Export-Csv -path .\smth.csv
I would like to get a result like
Order Name Version Build
----- ---- ------- -----
1 servername1 1.1.1 11111111
2 servername2 1.1.1 11111111
3 servername3 1.1.1 11111111
Would this be possible without using an array?
Upvotes: 2
Views: 127
Reputation: 174485
There are two problems with the current approach:
++
doesn't output anything by default$suborder
, you're creating a new local variable every time.The first problem can be solved by wrapping the operation in the grouping operator (...)
:
... | Select @{N="Order";E={($suborder++)}}, ...
The second problem can be solved by obtaining a reference to an object that exposes the suborder value as a property.
You can either use a hashtable or a custom object to "host" the counter:
$counter = @{ suborder = 1 }
... | Select @{N="Order";E={($counter['suborder']++)}}
... or you can make PowerShell wrap the original variable in a PSRef-wrapper by using the [ref]
keyword:
$suborder = 1
... | Select @{N="Order";E={(([ref]$suborder).Value++)}}
Upvotes: 4