VMware Cloud Community
tdubb123
Expert
Expert
Jump to solution

get-vmguest C drive

I need to find the guestos freespace in GB.

when i do

get-vm xxx | get-vmguest | select *

I see this

Screen Shot 2019-12-30 at 6.47.50 AM.png

how do I get only the C drive which should be "Hard Disk 1" and con vert to GB

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

$results = @()

$vms = get-vm -name (Get-Content .\vms.txt)

ForEach ($VM in $vms) {

$VMview = get-vm $VM | get-view

$result = ""|  select VM, Diskpath, CapacityGB, FreespaceGB, Freespace_percent

$result.VM = $VM.Name

$cDisk = $VMview.guest.disk| where{$_.DiskPath -match "^C:"}

$result.Diskpath = $cDisk.diskpath

$result.CapacityGB = [math]::Round($cDisk.capacity/1GB)

$result.FreespaceGB = [math]::Round($cDisk.freespace/1GB)

$result.Freespace_percent = [math]::Round($cDisk.freespace/$cDisk.capacity*100,1)

$results += $result

}

$results | ft -auto


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

View solution in original post

0 Kudos
11 Replies
T180985
Expert
Expert
Jump to solution

get-vm **** | get-vmguest | select -ExpandProperty disks

will show you all the disks in GB

Please mark helpful or correct if my answer resolved your issue. How to post effectively on VMTN https://communities.vmware.com/people/daphnissov/blog/2018/12/05/how-to-ask-for-help-on-tech-forums
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can do

get-vm xxx | get-vmguest | select -ExpandProperty Disks |

where {$_.Path -match "^C:"} |

Select -ExpandProperty CapacityGB


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

0 Kudos
tdubb123
Expert
Expert
Jump to solution

any idea why I am getting this error with the math function?

$results = @()

$vms = get-vm -name (Get-Content .\vms.txt)

ForEach ($VM in $vms) {

$VMview = get-vm $VM | get-view

$result = ""|  select VM, Diskpath, CapacityGB, FreespaceGB, Freespace_percent

$result.VM = $VM.Name

$result.Diskpath = $VMview.guest.disk.diskpath

$result.CapacityGB = [math]::Round(($VMview.guest.disk.capacity)/ 1GB)

$result.FreespaceGB = [math]::Round(($VMview.guest.disk.freespace)/ 1GB)

$result.Free

$results += $result

}

$results | ft -auto

Screen Shot 2019-12-30 at 7.59.08 AM.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Because the Disk property is an array when there is more than 1 drive in the guest OS.

You could do

$results = @()

$vms = get-vm -name (Get-Content .\vms.txt)

ForEach ($VM in $vms) {

$VMview = get-vm $VM | get-view

$result = ""|  select VM, Diskpath, CapacityGB, FreespaceGB, Freespace_percent

$result.VM = $VM.Name

$result.Diskpath = $VMview.guest.disk.diskpath

$result.CapacityGB = ($VMview.guest.disk.capacity | %{[math]::Round($_/1GB)}) -join '|'

$result.FreespaceGB = ($VMview.guest.disk.freespace | %{[math]::Round($_/1GB)}) -join '|'

$result.Free

$results += $result

}

$results | ft -auto


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

0 Kudos
tdubb123
Expert
Expert
Jump to solution

Can i just target the C Drive?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$results = @()

$vms = get-vm -name (Get-Content .\vms.txt)

ForEach ($VM in $vms) {

$VMview = get-vm $VM | get-view

$result = ""|  select VM, Diskpath, CapacityGB, FreespaceGB, Freespace_percent

$result.VM = $VM.Name

$cDisk = $VMview.guest.disk| where{$_.DiskPath -match "^C:"}

$result.Diskpath = $cDisk.diskpath

$result.CapacityGB = [math]::Round($cDisk.capacity/1GB)

$result.FreespaceGB = [math]::Round($cDisk.freespace/1GB)

$result.Freespace_percent = [math]::Round($cDisk.freespace/$cDisk.capacity*100,1)

$results += $result

}

$results | ft -auto


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

0 Kudos
tdubb123
Expert
Expert
Jump to solution

Thanks Lucd. works perfectly

0 Kudos
udayswami
Contributor
Contributor
Jump to solution

are these powershell commands and what is the pre-requisite to run these commands

0 Kudos
LucD
Leadership
Leadership
Jump to solution

These are PowerShell and PowerCLI cmdlets.
You should have PowerShell and the VMware.PowerCLI module installed.


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

0 Kudos
udayswami
Contributor
Contributor
Jump to solution

how do I install the vmware powercli module

0 Kudos
LucD
Leadership
Leadership
Jump to solution

See VMware Developer Documentation


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

0 Kudos