PowerCLI

 View Only
  • 1.  Generate list of Linux & Windows VMs, sort by memory size

    Posted Oct 19, 2021 07:19 PM

    I would like to generate separate list of Windows VM and Linux (non-windows) VM with the following properties:

    memory, cpu, total consumed storage size (not provision HDD size but what is being used).

    Thank you!



  • 2.  RE: Generate list of Linux & Windows VMs, sort by memory size

    Posted Oct 19, 2021 07:32 PM

    What do you already have?



  • 3.  RE: Generate list of Linux & Windows VMs, sort by memory size

    Posted Oct 19, 2021 07:40 PM

    Well, I've started with getting the total storage used by windows(or non-windows) VM with this.

    ((get-vm | Where-object{$_.Guest.OSFullName -like "*Windows*"}).UsedSpaceGB | measure-Object -Sum).Sum

    I need to generate a list of VM, sorted by memory and include CPU count and total space used by each VM.

     

    thank you!



  • 4.  RE: Generate list of Linux & Windows VMs, sort by memory size

    Posted Oct 19, 2021 08:11 PM

    Think I put together something close to what I am looking for but need a little tweak.

    Get-VM |
    Where-object{$_.Guest.OSFullName -like "*Windows*"}|
    select @{N='VM';E={$_.Name}},

    NumCpu,MemoryGB,

    @{N='CapacityGB';E={(Get-HardDisk -VM $_).CapacityGB -join ','}} |

    Format-Table -AutoSize

     

    1. how can I sort the list by memory size?

    2. for disk size, how can this be changed to total consumed?  above shows each disk size and i need actual space used.

     

    thank you.



  • 5.  RE: Generate list of Linux & Windows VMs, sort by memory size

    Posted Oct 19, 2021 08:32 PM

    The object returned by Get-VM already has a property named UsedSpaceGB.
    But you can also use the Measure-Object cmdlet.

    Sorting is just piping the objects to Sort-Object and specifying the property on which to sort.

    Get-VM |
    Where-object{$_.Guest.OSFullName -like "*Windows*"}|
    select @{N='VM';E={$_.Name}},
    NumCpu,MemoryGB,
    UsedSpaceGB,
    @{N='CapacityGB';E={ (Get-HardDisk -VM $_ | Measure-Object -Property CapacityGB -Sum).Sum}} |
    Sort-Object -Property MemoryGB |
    Format-Table -AutoSize

     



  • 6.  RE: Generate list of Linux & Windows VMs, sort by memory size

    Posted Oct 19, 2021 08:49 PM

    Thank you LucD!

    Is there way to round up (or truncate) decimal output to 2 places for MemoryGB, UsedSpaceGB, CapacityGB

    wk0812_0-1634676537657.png

     



  • 7.  RE: Generate list of Linux & Windows VMs, sort by memory size

    Posted Oct 19, 2021 08:54 PM

    Sure, use the Math method Round in a calculated property.
    For example

    @{N='UsedSpaceGB';E={[math]::Round($_.UsedSpaceGB,2)}},
    

     



  • 8.  RE: Generate list of Linux & Windows VMs, sort by memory size
    Best Answer

    Posted Oct 19, 2021 09:29 PM

    Perfect.  This is what I needed.  Got rid of the decimals and rounded up to whole number. much cleaner!

    Get-VM |
    Where-object{$_.Guest.OSFullName -like "*Windows*"}|
    select @{N='VM';E={$_.Name}},
    NumCpu,
    @{N='MemoryGB';E={[math]::Round($_.MemoryGB)}},
    @{N='UsedSpaceGB';E={[math]::Round($_.UsedSpaceGB)}},
    @{N='CapacityGB';E={[math]::Round((Get-HardDisk -VM $_ | Measure-Object -Property CapacityGB -Sum).Sum)}} |
    Sort-Object -Property MemoryGB |
    Format-Table -AutoSize

    One last question, looking at the VM and verifying the "CapacityGB" and "UsedSpaceGB", I'm noticing that the VM is showing more space available than what the "UsedSpaceGB" output is reporting.  That is, for example, output shows capacity at 50gb and usage close to 50gb. however when I do a visual check on the VM itself, it shows drive only at 35gb used.  Where is the discrepancy coming from?

    thank you.



  • 9.  RE: Generate list of Linux & Windows VMs, sort by memory size

    Posted Oct 20, 2021 05:53 AM

    Have a look at Solved: Re: Meaning of usedspaceGB - VMware Technology Network VMTN

    That might explain what you are seeing.