VMware Cloud Community
lorried82
Enthusiast
Enthusiast

powercli script for checking format of disk

anyone have a script to query datastores to find out the VM and the hard disks it has with the format?

for example

VM, Hard Disk, DataStore Name, Type (format)

I would like to be able to run this by Datacenter and Cluster.

Is that possible?

Thanks

Reply
0 Kudos
3 Replies
mattboren
Expert
Expert

Hello, lorried82-

Sure is.  Here's a snippet that uses the -PipelineVariable parameter available since PowerShell v4 to get such info:

Get-Datacenter -PipelineVariable oThisDatacenter | %{
   
## get all of the clusters in this datacenter
    Get-Cluster -Location $oThisDatacenter -PipelineVariable oThisCluster | %{
       
## get all of the VMs in this cluster, and their HDisks
        Get-VM -Location $oThisCluster | Get-HardDisk |
           
Select @{n="VM"; e={$_.Parent}},
                @{n
="HardDisk"; e={$_}},
                @{n
="Datastore"; e={$_.Filename.Split("]")[0].Trim("[")}},
                StorageFormat,
                @{n
="Cluster"; e={$oThisCluster}},
                @{n
="DataCenter"; e={$oThisDatacenter}}
    }

}

And, I added the Cluster and Datastore properties for each object, assuming that would be useful at some point.  If not, just remove those items from the Select statement, of course.  How's that do for you?

Reply
0 Kudos
lorried82
Enthusiast
Enthusiast

thanks Matt this looks great but I am not sure - what should I be changing here? Do I run this by cluster or datastore name?

Reply
0 Kudos
mattboren
Expert
Expert

Hello, lorried82-

Well, that depends upon the things upon which you want to report.  As written, the code will:

  1. get all datacenters
  2. for each datacenter, get all clusters
  3. for each cluster, get all VMs
  4. for each VM, get its hard disks
  5. for each hard disk, return a selected object that includes info like the VM of the hard disk, the datastore on which the hard disk resides, the storage format of the hard disk, and the cluster and datacenter of the VM

If you wanted to narrow this down, you could add a datacenter name in the Get-Datacenter portion, or adjust it a bit to just report on a particular cluster (remove the Get-Datacenter portion altogether, and the DataCenter calculated property on the Select statement), and so on.

Is that helpful, or do you have a specific/different way in which you want to use the code?

Reply
0 Kudos