VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Unable to get VMs list whose drive space is greater than 80%

Hi,

Please help me with below script as I am unable to get VMs list whose drive space is greater than 80%

Below is the script

$VmInfo = ForEach ($VM in Get-VM)

{

($VM.Extensiondata.Guest.Disk | Select @{N="Name";E={$VM.Name}},

@{N="IPAddress";E={$VM.guest.IPAddress[0]}},

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

@{N="Operating System"; E={@($VM.guest.OSFullName)}},

@{N="Folder";E={$VM.Folder.Name}},

@{N="MountPoint";E={$_.DiskPath}},

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

@{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)}})

}

$VmInfo

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

$VmInfo | where{$_.MountPoint -notmatch 'C:\\' -and $_.'Freespace(%)' -le 20 -and $_.'VM PowerState' -eq 'PoweredOn'}


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

View solution in original post

0 Kudos
10 Replies
LucD
Leadership
Leadership
Jump to solution

With $vm.ExtensionData.Guest.Disk you are looking at drives (in a a Windows guest OS) or filesystems (in a Linux guest OS).

If that is what your are looking for, you could do

$VmInfo = ForEach ($VM in Get-VM)

{

    $VM.Extensiondata.Guest.Disk | Select @{N="Name";E={$VM.Name}},

    @{N="IPAddress";E={$VM.guest.IPAddress[0]}},

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

    @{N="Operating System"; E={@($VM.guest.OSFullName)}},

    @{N="Folder";E={$VM.Folder.Name}},

    @{N="MountPoint";E={$_.DiskPath}},

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

    @{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)}}

}

$VmInfo | where{$_.'Freespace(%)' -le 20}


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

ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thanks for your quick reply,

If I execute the below script for only Powered On VMs, I dont get any output Smiley Sad

$VmInfo | where{$_.'Freespace(%)' -le 20 -and $VM_.PowerState -eq 'PoweredOn'}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I think you might have a typo in that line.

Try with

$VmInfo | where{$_.'Freespace(%)' -le 20 -and $_.'VM PowerState' -eq 'PoweredOn'}


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

perfect LucD,

One last question, is there a way to hide C:\ from the list ?

I tried the below

$VmInfo | where{$_.'DiskPath' -ne "C:\" -and $_.'Freespace(%)' -le 20 -and $_.'VM PowerState' -eq 'PoweredOn'}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$VmInfo | where{$_.MountPoint -notmatch 'C:\\' -and $_.'Freespace(%)' -le 20 -and $_.'VM PowerState' -eq 'PoweredOn'}


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

No LucD,

Still I am able to see C:\ in the output Smiley Sad

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Strange, just tested that myself and seems to work for me.

Are you sure nothing went wrong with the copy/paste?

Eventually attach your current script as a file in this thread, so I can have a look.


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD, Please find the script

$VmInfo = ForEach ($VM in Get-VM)

{

    $VM.Extensiondata.Guest.Disk | Select @{N="Name";E={$VM.Name}},

    @{N="IPAddress";E={$VM.guest.IPAddress[0]}},

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

    @{N="Operating System"; E={@($VM.guest.OSFullName)}},

    @{N="Folder";E={$VM.Folder.Name}},

    @{N="MountPoint";E={$_.DiskPath}},

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

    @{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)}}

}

$VmInfo | where{$_.'DiskPath' -NotMatch 'C:\\' -and $_.'Freespace(%)' -le 20 -and $_.'VM PowerState' -eq 'PoweredOn'}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The property to test against is MountPoint, not DiskPath.


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

After I rebooted my system and again I created a new file with the script with the changes you mentioned, It works now.

Not sure, whats wrong Smiley Happy

Thanks a lot for your quick help.

0 Kudos