Hello,
I need to list all VM:s and their provisioned disk space. I can do it with this command:
get-vm * | foreach-object {write-host "$_`t" (Get-HardDisk -vm $_ | Measure-Object -Property CapacityGB -Sum).sum.ToString()}
(If there is more elegant way to do this, please feel free to enlight me )
The problem is that I'm unable to export this data. Adding
| export-csv ... or
> list.txt
to end of line creates empty file and prints output to console.
How could I make export work correctly (or way I want)?
Ah, you are after the total size of just the VM's hard disks, not VMs' provisioned size on a datastore. Then, yes, you could continue to use the Get-HardDisk cmdlet as you had in your first example, but written as such to allow for further operations on the result:
Get-VM | Select-Object Name,@{n="HardDiskSizeGB"; e={(Get-HardDisk -VM $_ | Measure-Object -Sum CapacityGB).Sum}}
This route may be somewhat slow, especially as your environment is larger. If too slow, then we can employ everyone's bestest buddy, Get-View.
Anyway, does that do it for you?
Instead of using Write-Host to write the output to the console, you would want to use the Select-Object cmdlet, so that you can then have output in the pipeline when it comes time for Export-Csv.
And, you can use the handy property of VirtualMachine objects, "ProvisionedSpaceGB". Like:
Get-VM | Select-Object Name,ProvisionedSpaceGB | Export-Csv -NoTypeInformation -UseCulture c:\temp\myVMInfo.csv
How does that do for you?
Hello,
thank you for the answer, unfortunately that does not help me. ProvisionedSpaceGB does not have correct value, for it has VM's hard disks, swap file and other configuration files.
I need just a size of disks, for we use that as a billing metric.
Ah, you are after the total size of just the VM's hard disks, not VMs' provisioned size on a datastore. Then, yes, you could continue to use the Get-HardDisk cmdlet as you had in your first example, but written as such to allow for further operations on the result:
Get-VM | Select-Object Name,@{n="HardDiskSizeGB"; e={(Get-HardDisk -VM $_ | Measure-Object -Sum CapacityGB).Sum}}
This route may be somewhat slow, especially as your environment is larger. If too slow, then we can employ everyone's bestest buddy, Get-View.
Anyway, does that do it for you?
Thank you, this is exactly what I need. Speed is not issue, environment is quite small, just under 200 VM.
I'll look into Get-View anyway.
Just the answer I was looking for - thanks!