Answered by:
Get output back from previous command in pipeline

Question
-
I've been trying to get a output table sorted nicely but haven't found a good way of doing it. Maybe someone here already knows a trick to help?
Get-VM can list a list of VMs.
Get-VMHost can list what host the VM is running on. So if I run the following, I would get a list of hosts, that run all the VMs:
Get-VM | Get-VMHost
This is all good, but when list is long, you don't know which VM is really running on which host that returned as the Get-VMHost only returns a list of hosts, which has no association to the VMs. So what I would like is to have a list of both VM and Host names. I've tried -outvariable and -pipelinevariable from the Get-VM but none worked properly. Maybe I missed something?
as an example I did:
get-vm -outvariable ds | get-vmhost | ft $ds.name,name
Thursday, November 21, 2019 11:02 PM
Answers
-
I made it work. The use of -pipelinevariable was actually the right route. It's just how it is outputted by the end needed a tune up. Seems the passed variable needs to be presented with a custom object. Anyway, here is how it works, in case anyone is also having the same problem and interested in the trick:
- Marked as answer by PerhapsCloud Friday, November 22, 2019 6:21 PM
Friday, November 22, 2019 12:18 AM
All replies
-
Get-VmHost | Get-VM | Select ComputerName, Name
\_(ツ)_/
Thursday, November 21, 2019 11:15 PM -
Thanks.
It didn't work, the computername column is empty. Also I'm looking for the list of host based on a given list of VM, so it should be Get-VM | Get-VmHost, but either way, the ComputerName property didn't show anything.
Thursday, November 21, 2019 11:19 PM -
You will have to look for a consultant to help you sort this out or look in the Gallery for scripts that can do what you ask.
\_(ツ)_/
Thursday, November 21, 2019 11:26 PM -
I made it work. The use of -pipelinevariable was actually the right route. It's just how it is outputted by the end needed a tune up. Seems the passed variable needs to be presented with a custom object. Anyway, here is how it works, in case anyone is also having the same problem and interested in the trick:
- Marked as answer by PerhapsCloud Friday, November 22, 2019 6:21 PM
Friday, November 22, 2019 12:18 AM