Hi All,
I recently received an alarm that some of the datastores in my VMware environment were getting close to full. While auditing the environment to see if there was any space to cleared, I noticed that several VMs had thick provisioned disks when they should have been thin provisioned. Since I was dealing with many virtual machines, it would have been way too time consuming to visit the settings if each Virtual Machine and check the hard disks using the vCenter client. Using PowerShell, I was able to pull the hard disk inventory I needed for the VMDK files so I could see where corrections could be made. In this article I will be using the “Get-Harddisk” and the “Export-Csv” cmdlets to gather the harddisk information and exporting it to a csv file for easy viewng. The Get-Harddisk cmdlet is used to gather information for virtual disks on your vSphere serves. To do anything with PowerShell in VMware you will need to install VMware vSphere PowerCLI. You will then need to open PowerCLI and connect to your VMWare vCenter server by typing “Connect-VIServer vCenterServerName”.
First I would like to start by showing you how to get the hard disk information for one virtual machine. Type the following command replacing virtualmachinename with the name of the virtual machine for which you would like to retrieve disk information.
Get-Vm virtualmachinename | Get-Harddisk | Format-List
When executing the command above, it will return the following properties in for each virtual disk associated with the virtual machine specified.
StorageFormat Persistence DiskType Filename CapacityKB CapacityGB ParentId Parent Uid ConnectionState ExtensionData Id Name ClientNow lets say that all this information is not important and you only want to see certain properties. In the following command I will select the “Parent” property to show the virtual machine name, the storage format, and filename to see the VMDK filename. Im am also piping it to the “Export-Csv” cmdlet to send the results to a csv file named “disks.csv” located in the “c:\output” directory. Type the following command replacing virtualmachinename with the name of the virtual machine for which you would like to retrieve disk information.
Get-vm virtualmachinename | get-harddisk | select Parent,StorageFormat,Filename | Export-Csv c:\output\disks.csv –NoTypeInformation
In this next command I get the disk information for all virtual machines with all the default properties and export it to a csv file creating a disk inventory.
get-vm | get-harddisk | Export-Csv c:\output\disks.csv –NoTypeInformation
Finally, as shown earlier, if we do not want to see all properties, we can simply select the ones we need. In the following command I will get the disk information for all virtual vachines but only select the Parent, StorageFormat and Filename properties.
get-vm | get-harddisk | select Parent,StorageFormat,Filename | Export-Csv c:\output\disks.csv –NoTypeInformation
I hope this helps. If you would like any more information about the Get-Harddisk cmdlet, you can simply type “Get-Help Get-Harddisk”. If you have any questions or feedback, please leave a comment.
Related Links:
Using PowerShell to get a list Virtual Machine Snapshots in VMware ESXi 4.1
Using PowerShell to Gracefully Shut Down Multiple Virtual Machines in VMware
Using PowerShell to create a Virtual Machine Inventory in VMware and Export it to a CSV File
Using PowerShell to Create Virtual Machine Snapshots in VMwareUsing
PowerShell to Remove Virtual Machine Snapshots in VMware ESXi 4.1
Using PowerShell to View Datastore and Virtual Machine Space Usage Information in VMware
Thank you very much, your script helped me!