VMware Cloud Community
tdubb123
Expert
Expert
Jump to solution

script to sum up total disk usage of vms

how can I sum up the total vmdk sizes (not thin) of a list of vms from vc?

0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, again-

I see your second post.  Yes, you could go that route, too.  You are almost there, just need to add in a Measure-Object call.  A bit cleaned up, it would be like:

$machines = Get-Content c:\servers.txt
Get-VM -Name $machines | Get-HardDisk | `
   
select @{N="VMName";E={$_.Parent.Name}},@{N="Capacity(GB)";E={[math]::truncate($_.CapacityKB / 1MB)}},Filename | `
   
Measure-Object -Property "Capacity(GB)" -Sum

How does that do for you?

View solution in original post

0 Kudos
5 Replies
tdubb123
Expert
Expert
Jump to solution

came up somehting like this. could be better

$machines = get-content c:\servers.txt
foreach ($machines in $machines) {
get-vm -name $machines | Sort-Object Name | Get-HardDisk | select @{N="VM Name";E={$_.Parent.Name}},@{N="Capacity(GB)";E={[math]::truncate($_.CapacityKB / 1000KB)}},Filename | ft -AutoSize
}

I want to get a sum of the total usage of all vms.

0 Kudos
mattboren
Expert
Expert
Jump to solution

Hello, tdubb123-

There are several ways that you could get the sum of the sizes of the given VMs' disks.  One such way would be like:

## get the total GB of given VMs' disks
#
# if certain that all disks are thick
$arrVMNamesToCheck = Get-Content c:\temp\VMNames.txt
$(
$arrVMNamesToCheck | %{Get-View -ViewType VirtualMachine -Property Name, Config.Hardware.Device -Filter @{"Name" = $_}} | %{
   
$_.Config.Hardware.Device | ?{$_ -is [VMware.Vim.VirtualDisk]} | %{$_.CapacityInKB / 1MB}
} |
Measure-Object -Sum).Sum

Enjoy

0 Kudos
mattboren
Expert
Expert
Jump to solution

Hello, again-

I see your second post.  Yes, you could go that route, too.  You are almost there, just need to add in a Measure-Object call.  A bit cleaned up, it would be like:

$machines = Get-Content c:\servers.txt
Get-VM -Name $machines | Get-HardDisk | `
   
select @{N="VMName";E={$_.Parent.Name}},@{N="Capacity(GB)";E={[math]::truncate($_.CapacityKB / 1MB)}},Filename | `
   
Measure-Object -Property "Capacity(GB)" -Sum

How does that do for you?

0 Kudos
tdubb123
Expert
Expert
Jump to solution

hi mattboren,

yes this works great. for my second post. how do I have

select @{N="VMName";E={$_.Parent.Name}},@{N="Capacity(GB)"

show up just once and everything under it. I gues I have to move this out of the loop

thanks

0 Kudos
mattboren
Expert
Expert
Jump to solution

Hello-

It sounds like you are saying that you just want the Format-Table to happen once, instead of for each VM.  Makes sense.  You could do so with the first example below by basically moving the Format-Table outside of the foreach loop.  But, really, you do not need a foreach loop here, since Get-VM will take an array of VM names as a value for the -Name parameter.

You can just pass the entire array of VM names to one Get-VM call, and continue on with the pipeline.  See the second example for how that looks (both assume you have already created the $machines array with the Get-Content line).

first way:

## using a foreach loop, passing one VM name at a time to Get-VM
&{foreach ($machine in $machines) {
   
Get-VM -Name $machine | Get-HardDisk | `
   
select @{N="VM Name";E={$_.Parent.Name}},@{N="Capacity(GB)";E={[math]::truncate($_.CapacityKB / 1MB)}},Filename
}} |
ft -AutoSize

second (probably better, definitely cleaner) way:

## passing all VM names at the same time to Get-VM (passing the array/collection)
Get-VM -Name $machines | Get-HardDisk | `
   
select @{N="VM Name";E={$_.Parent.Name}},@{N="Capacity(GB)";E={[math]::truncate($_.CapacityKB / 1MB)}},Filename |
   
ft -AutoSize

Work out ok for you?

0 Kudos