Hi,
I need to collect the amount of disk space used in each VM, however, I need to know the Virtual Center folder name.
The problem is the script is showing at least 4 times each VM: in "Datacenter" folder, in "vm" folder, in "host" folder and in all levels of folder created for VMs.
How do I only get the Virtual Center folder where the VM is allocated (the top level)?
$report = @()
Get-Folder | foreach-object {
$folder = $_.Name
$_ | Get-VM | foreach-object {
$row = "" | select FolderName,VMName,MemoryMB,NumCPU,TotalDiskGB,VirtualCenter
$row.FolderName = $folder
$row.VMName = $_.Name
$row.MemoryMb = $_.MemoryMb
$row.NumCpu = $_.NumCpu
$_.HardDisks | %{
$totaldisk = ( $totaldisk + $_.CapacityKB )
}
$row.TotalDiskGB = ((( $totaldisk / 1024 ) + $row.MemoryMb ) / 1024 )
$row.VirtualCenter = $DefaultVIServer.Name
$report += $row
$totaldisk = 0
}
}
$report |sort -property VMName,FolderName|Export-Csv "c:\temp\report.csv" -noTypeInformation
The "vm" and "host" folders are 2 hidden folders that are the roots of the yellow and blue folders you see in the VIC.
The Get-Folder cmdlet doesn't allow you to specify if you want to blue or yellow folders.
Hence you will see the same guest twice.
If you change the script slightly and get the foldername in a different way you won't see any double entries.
$report = @() Get-VM | foreach-object { $row = "" | select FolderName,VMName,MemoryMB,NumCPU,TotalDiskGB,VirtualCenter $row.FolderName = (Get-Folder -Id $_.FolderId).Name $row.VMName = $_.Name $row.MemoryMb = $_.MemoryMb $row.NumCpu = $_.NumCpu $_.HardDisks | % { $totaldisk = ( $totaldisk + $_.CapacityKB ) } $row.TotalDiskGB = ((( $totaldisk / 1024 ) + $row.MemoryMb ) / 1024 ) $row.VirtualCenter = $DefaultVIServer.Name $report += $row $totaldisk = 0 } $report |sort -property VMName,FolderName|Export-Csv "c:\temp\report.csv" -noTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
I already tried this form but the object returned by function Get-VM don't have FolderID property, causing the error bellow:
Cannot validate argument on parameter 'Id'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again. At :line:4 char:23
+ Get-VM | foreach-object <<<< {
You're right, the FolderId property doesn't exist in build 162509. My mistake
Try replacing that line with this one
$row.FolderName = (Get-View ($_ | Get-View).Parent).Name
It will be a bit slower, but it should do the trick.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Now It works fine!!!
Thank you!
Gabriel
great script! worked perfectly right off the bat. i am new to powershell; can someone advise the correct lines to add that would add templates to the report, show the host the vm\template is on, and show the amount of used disk space for each vm? thanks very much in advance..
-sal