POSTS
PowerShell: Get VMs running on a single host
I wanted to perform an action on all of the VMs on a particular host, but using my connection to Virtual Center instead of reconnecting directly to the host. I found something interesting.
This is wrong:
Get-VMhost esx1.local | Get-VM #Not what I expected.
This does not work as I expected. It gets all of the VMs in the cluster instead of just the ones registered with the host. My best guess is that this is because the host is not really a container object when in a cluster.
As a workaround you have to do something like this:
Get-VM | Where-Object {$_.Host.Name -eq " esx1.local"}
To cut down on Get-VM processing time do:
Get-VMhost esx1.local | Get-VM | Where-Object {$_.Host.Name -eq "esx1.local"}
The first part of the pipeline still gets all of the VMs in the cluster, but this is better than all of the VMs in Virtual Center.
I thought I had a good solution and then a co-worker showed me a command line that he got from somewhere (sorry, don’t know where) that cut the processing time in half (Measure-Command is great).
(Get-VMhost esx1.local | Get-View).VM | % {$_ | Get-VIObjectByVIView}
Update 3/5/2009 — As Jeremy indicated in the comments, this is a more appropriate way to use Get-VIObjectByVIView:
(Get-VMhost esx1.local | Get-View).VM | Get-VIObjectByVIView