In this post I will show you how to query the datastore name given Virtual Machine name in VMware.
Let us see what is involved in querying the data store name of a VM hard disk. At first glance you might think that below command will give you the information you need and there is no complexity. But the actual fact is that, below command returns the datastore name where the VM is stored(that is VMX file) but not the location of hard disks.
Get-DataStore -VM (Get-VM -Name MyVM1)
So, what is the approach should be? Per my research, there is no straight forward way to get this information, but what we can do is derive this from one of the existing properties. Let us see how to do that.
The Get-HardDisk cmdlet returns array of harddisk objects and each object will have a property called filename. This file name property contains path to HDD file in the datastore. Extracting the datastore name from the FileName property is the only way to get datastore name of the hard disks.
$VM = Get-VM -Name MyVM1 $Disks = Get-HardDisks -VM $VM foreach($disk in $Disks) { $FileName = $Disk.FileName $Diskname = $Disk.Name $datastore = $FileName.split("]")[0].split("[")[1] "{0} : {1}" -f $Diskname,$datastore }
Hope this helps… Happy learning…