Virtually everything is poshable
PowerCLI: Virtual Machine disk usage
Recently I have had a few comments on an old post I wrote showing a one-liner to get the VM’s disk sizes for each VM, I was asked if there was a way to export this information into a CSV file.
The easy answer is yes but what you need to do is build up a container and then add each part of the information to the container, this is quite common practice in PowerShell, its a great technique where you can basically build and populate your own information and then just add to it, once you are completed you can then take your container and export it to whatever format you wish, for example:
The one-liner mentioned in my previous post simply outputted the data one VM at a time to the screen, whilst this was great for looking at the information, when we try and export it things start to go wrong !
The new script, below, may take up more lines but is far more efficient and adaptable.
The script:
Connect-VIServer MYVISERVER
$MyCollection = @()
$AllVMs = Get-View -ViewType VirtualMachine | Where {-not $_.Config.Template}
$SortedVMs = $AllVMs | Select *, @{N="NumDisks";E={@($_.Guest.Disk.Length)}} | Sort-Object -Descending NumDisks
ForEach ($VM in $SortedVMs){
$Details = New-object PSObject
$Details | Add-Member -Name Name -Value $VM.name -Membertype NoteProperty
$DiskNum = 0
Foreach ($disk in $VM.Guest.Disk){
$Details | Add-Member -Name "Disk$($DiskNum)path" -MemberType NoteProperty -Value $Disk.DiskPath
$Details | Add-Member -Name "Disk$($DiskNum)Capacity(MB)" -MemberType NoteProperty -Value ([math]::Round($disk.Capacity/ 1MB))
$Details | Add-Member -Name "Disk$($DiskNum)FreeSpace(MB)" -MemberType NoteProperty -Value ([math]::Round($disk.FreeSpace / 1MB))
$DiskNum++
}
$MyCollection += $Details
}
$MyCollection | Out-GridView
# Export-Csv, ConvertTo-Html or ConvertTo-Xml can be used above instead of Out-Gridview
The output is listed in a strange order due to an issue with this method of using PowerShell as mentioned here.
Just a reminder that this information can also be viewed, filtered and exported using my VESI/PowerGUI PowerPack which you can download here. – Nearly 5000 downloads so far !
| Print article | This entry was posted by Virtu-Al on January 27, 2010 at 23:22, and is filed under PowerCLI, PowerGUI, VESI, powershell, vmware. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |







(3)
(13)
(0)
about 7 months ago
Thanks, just what i’m looking for.
about 7 months ago
Thanks. Works great.
Not sure if it’s my environment only, but can it show linux partitions (mount names)? Seems to show only Windows drive letters…
about 7 months ago
@Kayser
Thanks for the comment, Yes this also works on Linux VM’s but you will need to make sure you have VMTools installed and running for it to grab the data.
about 7 months ago
Just installed vmtools on the linux vms this script is perfect. You’re the man, thanks very much.
about 6 months ago
This is awesome — but whats the trick to get the VMId? I’m sure its another
“$Details | Add-Member -Name VMId -Value $VM.SomeDataMember -Membertype NoteProperty”
But every data member I’ve tried thusfar comes back empty …
about 5 months ago
Which ID are you talking about, can you give me an example please ?
about 5 months ago
The “vmid” — the one like ‘VirtualMachine-vm-4087′
about 5 months ago
As written the script will grab every vm guest disk size for all vm’s seen by the VI server.
How can I limit this to specific datacenters?
I made a modification others may find useful: I added a member named UsedSpace(MB) and calculated the guest vm used disk space (Total – Free).
about 5 months ago
@Don Maslanka
On line 5 where it reads:
$AllVMs = Get-View -ViewType VirtualMachine | Where {-not $_.Config.Template}
Chage it to:
$AllVMs = Get-Datacenter “My Datacenter” | Get-VM | Get-View | Where {-not $_.Config.Template}
about 5 months ago
@Chris
You can add a line after line 7 which reads:
$Details | Add-Member -Name VMID -Value $VM.moref -Membertype NoteProperty
about 5 months ago
Thanks for the assist, that worked great!
about 5 months ago
Hi, is there any way I can also see the datastore information?
about 4 months ago
Hey I am a beginner and I have Powercli working but I cant the script to run. I copied and paste this in the editor and I received a MyCollection error. Please help
about 3 months ago
@Don Maslanka @Virtu-Al
“I made a modification others may find useful: I added a member named UsedSpace(MB) and calculated the guest vm used disk space (Total – Free).”
Could you provide the code for this? This is exactly what i’m looking to add!
On another note: I just came across these great scripts and definitely appreciate that you are sharing these with everyone.