This script will export following information into a .csv file:

1) name of the virtual machine (VM)
2) datacenter name
3) cluster name
4) hard disc
5)datastore name
6) path and name of the .vmdk file

Note: Change vCenter Server name to your vCenter Server Name

Connect-VIServer  vCenter_server_name

$VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {

ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {

ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {

ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {

“” | Select-Object -Property @{N=”VM”;E={$VM.Name}},

@{N=”Datacenter”;E={$Datacenter.name}},

@{N=”Cluster”;E={$Cluster.Name}},

@{N=”Hard Disk”;E={$HardDisk.Name}},

@{N=”Datastore”;E={$HardDisk.FileName.Split(“]”)[0].TrimStart(“[“)}},

@{N=”VMDKpath”;E={$HardDisk.FileName}}

}

}

}

}

$VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path “C:\VmQuery.csv”

Posted in: Vmware.
Last Modified: January 12, 2017

Leave a reply