VMware Cloud Community
RajuVCP
Hot Shot
Hot Shot
Jump to solution

Script for VM HDD type, usage and freespace

Hi All,

I was looking for a script which can give VM's StorageType (Thick or Thin), Usage, available space(in GB)

I got the command given below where i can get storagetype, Capacity, But i need the VM's used and free space also..

get-vm | Get-harddisk | Select-Object  -Property Parent, Name, StorageFormat, CapacityGB

Thanks a ton in advance.

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
Tags (4)
Reply
0 Kudos
1 Solution

Accepted Solutions
RajuVCP
Hot Shot
Hot Shot
Jump to solution

Finally,

I can manage to get 2 scripts to get VM with Thin or Thick details and another for VM disk space.

For VM thin for Thick Details

Get-VM | ForEach {Get-harddisk -VM $_ | Select-Object -Property Parent, Name, StorageFormat, CapacityGB, @{N='OS Total Free Space GB of all partitions'; E={ [math]::round((Get-VMGuest $vm | ForEach-Object { ($_.Disks) } | Measure-Object -property FreeSpaceGB -sum).Sum)}}} | convertTo-html | out-file C:\Users\USERA\Desktop\VMThin&Thick.htm

(the output  for above given script will be in html format)

For VM Disk Space

ForEach ($VM in Get-VM ){($VM.Extensiondata.Guest.Disk | Select @{N="Name";E={$VM.Name}},DiskPath, @{N="Capacity(GB)";E={[math]::Round($_.Capacity/ 1GB)}}, @{N="Free Space(GB)";E={[math]::Round($_.FreeSpace / 1GB)}}, @{N="Free Space %";E={[math]::Round(((100* ($_.FreeSpace))/ ($_.Capacity)),0)}})}

NOTE - To get the VM Disk space output in html format use below given lines. (copy above given lines in a txt file and save it as FreeSpace.ps1 (what ever name you can give) and run the below lines in powercli.

FreeSpace.ps1 | ConvertTo-Html | Set-Content C:\Users\USERA\Desktop\FreeSpace.htm

Thanks LucD for your help Smiley Happy

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com

View solution in original post

Reply
0 Kudos
9 Replies
MKguy
Virtuoso
Virtuoso
Jump to solution

If the VMs have VMware tools installed, you can read the free space of the partitions on the OS side with Get-VMGuest like:

PS D:\> (Get-VM MyVM | Get-VMGuest).Disks

CapacityGB      FreeSpaceGB     Path

----------      -----------     ----

29,483          10,998          C:\

9,967           5,845           F:\

9,967           8,552           D:\

9,967           9,371           E:\

The problem is that this only lists formatted partitions, and you can't directly correlate a certain OS partition to a certain virtual disk without adding some substantial logic that would probably involve executing commands inside the Guest OS with Invoke-VMScript to be reliable. It gets even more complicated if there are multiple partitions on the same disk, or there is unpartitioned/unformatted disk space.

The following snippet will output the total free space of all partitions combined (which will be repeated per disk):

Get-VM | ForEach {
  $vm = $_
  Get-harddisk -VM $_ | Select-Object -Property Parent, Name, StorageFormat, CapacityGB,
    @{N='OS Total Free Space GB of all partitions combined'; E={ [math]::round((Get-VMGuest $vm | ForEach-Object { ($_.Disks) } | Measure-Object -property FreeSpaceGB -sum).Sum)}}
} | Format-Table -Autosize

Parent         Name       StorageFormat CapacityGB OS Total Free Space GB of all disks combined
------         ----       ------------- ---------- --------------------------------------------
VM1            Hard disk 1        Thick     30                                       24
VM1            Hard disk 2        Thick     10                                       24
VM2            Hard disk 1         Thin     30                                       35
VM2            Hard disk 2 EagerZeroedThick     10                                       35 35

You might be better off to get the VMware-side and the OS view side values independently if you want per-disk/per partition data.

-- http://alpacapowered.wordpress.com
RajuVCP
Hot Shot
Hot Shot
Jump to solution

Thanks MKguy,

For providing the script. i tried the script but i am getting below given error.. what could be the wrong.

At line:1 char:31

+ Get-VM | ForEach {  $vm = $_  Get-harddisk -VM $_ | Select-Object -Property

Pare ...

+                               ~~~~~~~~~~~~

Unexpected token 'Get-harddisk' in expression or statement.

    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx

   ception

    + FullyQualifiedErrorId : UnexpectedToken

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
Reply
0 Kudos
MKguy
Virtuoso
Virtuoso
Jump to solution

You just didn't copy the line breaks correctly.

-- http://alpacapowered.wordpress.com
Reply
0 Kudos
RajuVCP
Hot Shot
Hot Shot
Jump to solution

I tried with that also , am using below given lines, but still am getting error.:smileyconfused:

Get-VM | ForEach {$vm = $_  Get-harddisk -VM $_ | Select-Object -Property Parent, Name, StorageFormat, CapacityGB,    @{N='OS Total Free Space GB of all partitions combined'; E={ [math]::round((Get-VMGuest $vm | ForEach-Object { ($_.Disks) } | Measure-Object -property FreeSpaceGB -sum).Sum)}}} | Format-Table -Autosize

Not sure where am making mistake here.. MKguy can you please  correct it and attach in notepad here..

Thanks a Ton ...

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
Reply
0 Kudos
RajuVCP
Hot Shot
Hot Shot
Jump to solution

MKguy,

I got the output of the script, but i didnt get FreeSpace of the VM's. Below is the output freespace is missing init. is there any thing am i missing

Parent         Name       StorageFormatCapacityGBOS Total Free Space GB of all Disks Combined
------         ----      
VM1            Hard disk 1        Thick30
VM1            Hard disk 2        Thick30
VM2            Hard disk 1         Thin10
VM2            Hard disk 2 EagerZeroedThick     Thick30
Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
Reply
0 Kudos
RajuVCP
Hot Shot
Hot Shot
Jump to solution

Finally,

I can manage to get 2 scripts to get VM with Thin or Thick details and another for VM disk space.

For VM thin for Thick Details

Get-VM | ForEach {Get-harddisk -VM $_ | Select-Object -Property Parent, Name, StorageFormat, CapacityGB, @{N='OS Total Free Space GB of all partitions'; E={ [math]::round((Get-VMGuest $vm | ForEach-Object { ($_.Disks) } | Measure-Object -property FreeSpaceGB -sum).Sum)}}} | convertTo-html | out-file C:\Users\USERA\Desktop\VMThin&Thick.htm

(the output  for above given script will be in html format)

For VM Disk Space

ForEach ($VM in Get-VM ){($VM.Extensiondata.Guest.Disk | Select @{N="Name";E={$VM.Name}},DiskPath, @{N="Capacity(GB)";E={[math]::Round($_.Capacity/ 1GB)}}, @{N="Free Space(GB)";E={[math]::Round($_.FreeSpace / 1GB)}}, @{N="Free Space %";E={[math]::Round(((100* ($_.FreeSpace))/ ($_.Capacity)),0)}})}

NOTE - To get the VM Disk space output in html format use below given lines. (copy above given lines in a txt file and save it as FreeSpace.ps1 (what ever name you can give) and run the below lines in powercli.

FreeSpace.ps1 | ConvertTo-Html | Set-Content C:\Users\USERA\Desktop\FreeSpace.htm

Thanks LucD for your help Smiley Happy

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
Reply
0 Kudos
rastickland
Enthusiast
Enthusiast
Jump to solution

is there a way to export this to html or .csv? MKguy

Reply
0 Kudos
szemmali
Contributor
Contributor
Jump to solution

VMware code:

https://code.vmware.com/samples/5120

Description:

To make this inventory process less time consuming I began using #PowerShell scripts to collect the information I was interested. Over time these scripts got less messy and more useful, so now I want to share my current script.

This PowerShell script, #vCollect-Storage-ByPartition-ByvCenter.ps1,  will collect Disk Path Information from VMs By #vCenter that includes:

  • vCenter
  • Cluster
  • Host
  • VM
  • Name
  • Disk PATH
  • Capacity(GB)
  • Free Space(GB)
  • Free Space %

Output:

The information is output to a CSV file per server and CSV file for all vCenter.

Reply
0 Kudos
lamn
Contributor
Contributor
Jump to solution

Hi Raju, Your script is working for me. in your second script, is possible to display 'C' drive only in disk path?

Thanks in advance!

Nick

Reply
0 Kudos