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
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
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
Thanks for your quick reply,
If I execute the below script for only Powered On VMs, I dont get any output
$VmInfo | where{$_.'Freespace(%)' -le 20 -and $VM_.PowerState -eq 'PoweredOn'}
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
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'}
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
No LucD,
Still I am able to see C:\ in the output
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
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'}
The property to test against is MountPoint, not DiskPath.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
Thanks a lot for your quick help.