VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Unable to get free space of VM

Hi,

I am unable to get free space of VM level. Please help

function get-mysummary() {

  param(

   [VMware.VimAutomation.ViCore.Types.V1.Inventory.Folder]$Folder,

   [switch]$Recursion

  )

  $report = @()

  $vms = Get-VM -Location $Folder -NoRecursion:(-not $Recursion)

  if($vms.Count -gt 0){

   $report += Get-VM -Location $Folder -NoRecursion:(-not $Recursion) |

  Select Folder, Name,

   @{N = "IP Address"; E = {@($_.guest.IPAddress[0])}},

   @{N="VM PowerState";E={@($_.PowerState)}},

   @{N="OS"; E={@($_.guest.OSFullName)}},

   @{N = "CPU"; E = {@($_.NumCPU)}},

   @{N = "Memory (GB)"; E = {@($_.MemoryGB)}},

   @{N = "Provisioned (GB)"; E = {[math]::Round($_.provisionedspacegb)}}

   @{N="FreeSpace(GB)";E={[math]::Round($_.FreeSpace / 1GB)}},

   @{N="FreeSpace(%)";E={[math]::Round(((100* ($_.FreeSpace))/($_.Capacity)),0)}},

   @{N="UsedSpace(GB)";E={[math]::Round(((($_.Capacity / 1GB))-($_.FreeSpace / 1GB)),0)}})

}

The above command shows blank for Freespace

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

function get-mysummary() {

   param(

   [VMware.VimAutomation.ViCore.Types.V1.Inventory.Folder]$Folder,

   [switch]$Recursion = $false

   )


   $vms = Get-VM -Location $Folder -NoRecursion:(-not $Recursion)


   if ($vms.Count -gt 0) {

   $vms | Select Folder, Name,

   @{N = "IP Address"; E = {@($_.guest.IPAddress[0])}},

   @{N = "VM PowerState"; E = {@($_.PowerState)}},

   @{N = "OS"; E = {@($_.guest.OSFullName)}},

   @{N = "CPU"; E = {@($_.NumCPU)}},

   @{N = "Memory (GB)"; E = {@($_.MemoryGB)}},

   @{N = "Provisioned (GB)"; E = {[math]::Round($_.provisionedspacegb)}},

   @{N = "FreeSpace(GB)"; E = {

   $script:vdiskFree = ($_.Guest.Disks | Measure-Object -Property FreeSpaceGB -Sum).Sum

   $script:vdiskCapacity = ($_.Guest.Disks | Measure-Object -Property CapacityGB -Sum).Sum

   [math]::Round($script:vdiskCapacity, 0)}},

   @{N = "FreeSpace(%)"; E = {[math]::Round($script:vdiskFree / $script:vdiskCapacity * 100, 0)}},

   @{N = "UsedSpace(GB)"; E = {[math]::Round($script:vdiskCapacity - $script:vdiskFree, 0)}}

   }

}


Get-Folder |

   ForEach-Object -Process {

   get-mysummary -Folder $_

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

FreeSpace and Used Space are properties on a Datastore object, not a VM object.
What kind of value do you want to see in this report?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Hi LucD,

I would like to see the total available free space on the VM, along with the provisioned space.

Please help.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

function get-mysummary() {

   param(

   [VMware.VimAutomation.ViCore.Types.V1.Inventory.Folder]$Folder,

   [switch]$Recursion = $false

   )


   $vms = Get-VM -Location $Folder -NoRecursion:(-not $Recursion)


   if ($vms.Count -gt 0) {

   $vms | Select Folder, Name,

   @{N = "IP Address"; E = {@($_.guest.IPAddress[0])}},

   @{N = "VM PowerState"; E = {@($_.PowerState)}},

   @{N = "OS"; E = {@($_.guest.OSFullName)}},

   @{N = "CPU"; E = {@($_.NumCPU)}},

   @{N = "Memory (GB)"; E = {@($_.MemoryGB)}},

   @{N = "Provisioned (GB)"; E = {[math]::Round($_.provisionedspacegb)}},

   @{N = "FreeSpace(GB)"; E = {

   $script:vdiskFree = ($_.Guest.Disks | Measure-Object -Property FreeSpaceGB -Sum).Sum

   $script:vdiskCapacity = ($_.Guest.Disks | Measure-Object -Property CapacityGB -Sum).Sum

   [math]::Round($script:vdiskCapacity, 0)}},

   @{N = "FreeSpace(%)"; E = {[math]::Round($script:vdiskFree / $script:vdiskCapacity * 100, 0)}},

   @{N = "UsedSpace(GB)"; E = {[math]::Round($script:vdiskCapacity - $script:vdiskFree, 0)}}

   }

}


Get-Folder |

   ForEach-Object -Process {

   get-mysummary -Folder $_

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Hi LucD,

As per the output, I am seeing difference between provisioned, free space and used space

          

NameIP AddressVM PowerStateOSCPUMemory (GB)Provisioned (GB)FreeSpace(GB)FreeSpace(%)UsedSpace(GB)
Myvm110.10.10PoweredOnMicrosoft Windows Server 2012 (64-bit)2454506517

when I add freespace and usedspace it is not matching with provisioned space

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is because Provisioned is seen from the outside, it is the space allocated to a vDisk when you created the VM.

The FreeSpace and UsedSpace is from inside the guest OS (obtained through VMware Tools).
When your vDisk is of type Thin, vSphere is not giving you that physical space immediately, it is extended as usage grows.
The guest OS sees the allocated size, not the provisioned space.f

Example:
- a VMDK of type Thin, with size 60 GB

- the VMDK starts with allocated space 0, which grows as you place data on the disk

- the guest OS sees the harddisk always as 60 GB

And don't forget that there is overhead.
A VMDK of 60 GB, will not be exactly 60 GB of usable storage inside the guest OS.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
leem2001
Contributor
Contributor
Jump to solution

Hi, this script gets a VM´s disks and lists total, free and free% space:

# Get VM disk space usage

Clear-Variable -name Output

$Output = @()

$cluster = Get-Cluster "clustername"

ForEach ($VM in $cluster | Get-VM | where-object {($_.powerstate -ne "PoweredOff")})

{

    ForEach ($Drive in $VM.Extensiondata.Guest.Disk)

    {

        $Path = $Drive.DiskPath

        #Calculations

        $Freespace = [math]::Round($Drive.FreeSpace / 1MB)

        $Capacity = [math]::Round($Drive.Capacity/ 1MB)

        $SpaceOverview = "$Freespace" + "/" + "$capacity"

        $PercentFree = [math]::Round(($FreeSpace)/ ($Capacity) * 100)

        $Output = $Output + "Vm: " + $VM.Name + "`n"

        $Output = $Output + "Disk: " + $Path + "`n"

        $OutPut = $Output + "Free(MB): " + $Freespace + "`n"

        $Output = $Output + "Free(%): " + $PercentFree + "`n" 

        $Output = $Output + "Capacity: " + $Capacity + "`n"

        write-host $VM.name $path $Capacity "Total " $Freespace "Free " $PercentFree "%Free "

    }

}

Results look like this:

computername D:\ 40957 Total  37061 Free  90 %Free

computername C:\ 76448 Total  48177 Free  63 %Free

Maybe this works for you?

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Hi LucD,

After replacing the below line from

[math]::Round($script:vdiskCapacity, 0)}},

To

[math]::Round($script:vdiskCapacity - ($script:vdiskCapacity - $script:vdiskFree), 0)}},

I am getting the desired output. Smiley Happy

Thank you very much for your help as always Smiley Happy

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Hi,

I was looking for total VM disk space used and free.

Thanks for your time and help Smiley Happy

0 Kudos