Show VM, Host, Cluster and Datacenter

This one line script is the first thing I do when joining a company to give me an overview of what is out there in the vCenter estate.

It shows a VM against its Host, Cluster and Datacenter, we all know these can change with DRS and sDRS but as I say its a great overview and can always be a weekly audit task.

The one line script is below, below that we will break it down for understanding

Get-VM | Select Name, @{N=”Host”;E={Get-VmHost -VM $_}}, @{N=”Cluster”;E={Get-Cluster -VM $_}}, @{N=”Datacentre”;E={Get-Datacenter -vm $_}}

Get-VM -> This is the cmdlet that will gather all the information about all the VM’s in the estate due to no filter criteria

Select Name -> Select the VM name

The next section is creating Hashtables which references back to before the pipeline. Each section has a Name/Key and a Equivalent/Value. $_ represents every single object in the pipe.

Below is the same script with an Export line at the bottom

Get-VM | Select Name, @{N=”Host”;E={Get-VmHost -VM $_}}, @{N=”Cluster”;E={Get-Cluster -VM $_}}, @{N=”Datacentre”;E={Get-Datacenter -vm $_}} | Export-CSV C:\vCenterAudit.csv -NoTypeInformation

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.