Good morning!
Struggling with the math calculations in the following Powercli report script. The math calculations only work for the first entry vm in the report not sure why. Can't figure how to add annotations to this report. Ideally would like to be able to display percent free space for each drive path, would be fine with math calculations working along annotation display for each vm. Have included a sample of the current report output.
Any help would be greatly appreciated.
#vm_info_datacenter_test.ps1
#https://communities.vmware.com/thread/540442
#Credits LUCD@communities.vmware.com for report foundation. Minor adds by nlong
#VM data per datacenter
#Datacenter can be wildcarded ($dcName)
$dcName = 'wvhq'
$report = @()
$dc = Get-View -ViewType Datacenter -Property Name,HostFolder -Filter @{'Name'="^$($dcName)$"}
foreach($cluster in (Get-View -Id ((Get-View -Id $dc.HostFolder -Property ChildEntity).ChildEntity | where{$_.Type -eq 'ClusterComputeResource'}) -Property Name,Host)){
foreach($esx in Get-View -Id $cluster.Host -Property Name,VM){
if($esx.VM){
$report += Get-View -Id $esx.VM -Property Name,Guest,Runtime,Config.Hardware,Summary.Config |
Where{!$_.Config.Template} |
Select Name,
@{N='Powerstate'; E={$_.Runtime.Powerstate}},
@{N='CPUs'; E={$_.Config.Hardware.NumCpu}},
@{N='Sockets'; E={$_.Config.Hardware.NumCpu/$_.Config.Hardware.NumCoresPerSocket}},
@{N='Cores p/s'; E={$_.Config.Hardware.NumCoresPerSocket}},
@{N='Datacenter';E={$dc.Name}},
@{N='Cluster';E={$cluster.Name}},
@{N='Host';E={$esx.Name}},
@{N='OS' ;E={$_.summary.config.guestfullname}},
@{N="VM_MemoryMB";E={$_.Config.Hardware.MemoryMB}},
@{N="Disk Path";E={$_.Guest.Disk.DiskPath}},
@{N="VM_Disk_Capacity";E={$_.Guest.Disk.Capacity}}, ## Once Math calculation works will remove this line.
@{N="VM_Disk_FreeSpace";E={$_.Guest.Disk.FreeSpace}}, ## Once Math calculation works will remove this line.
@{N="VM_Disk_CapacityGB";E={[math]::Round(($_.Guest.Disk.Capacity/1GB))}},
@{N="VM_Disk_FreeSpaceGB";E={[math]::Round(($_.Guest.Disk.FreeSpace/1GB))}},
@{N="VM_IP#";E={$_.Guest.IPAddress}},
@{N="VLAN";E={$_.Guest.Net.Network}}
##@{N='VM_Annotations';E={(Get-Annotation -Entity $esx.VM).Value}}
}
}
}
$report | Export-Csv D:\Powershell_temp\VM_info_datacenter.csv -NoTypeInformation -UseCulture
When there multiple guest OS partitions the script will have to get the sum for capacity and freespace.
Something like this
#vm_info_datacenter_test.ps1
#https://communities.vmware.com/thread/540442
#Credits LUCD@communities.vmware.com for report foundation. Minor adds by nlong
#VM data per datacenter
#Datacenter can be wildcarded ($dcName)
$dcName = 'wvhq'
$report = @()
$dc = Get-View -ViewType Datacenter -Property Name,HostFolder -Filter @{'Name'="^$($dcName)$"}
foreach($cluster in (Get-View -Id ((Get-View -Id $dc.HostFolder -Property ChildEntity).ChildEntity | where{$_.Type -eq 'ClusterComputeResource'}) -Property Name,Host)){
foreach($esx in Get-View -Id $cluster.Host -Property Name,VM){
if($esx.VM){
$report += Get-View -Id $esx.VM -Property Name,Guest,Runtime,Config.Hardware,Config.Annotation,Summary.Config |
Where{!$_.Config.Template} |
Select Name,
@{N='Powerstate'; E={$_.Runtime.Powerstate}},
@{N='CPUs'; E={$_.Config.Hardware.NumCpu}},
@{N='Sockets'; E={$_.Config.Hardware.NumCpu/$_.Config.Hardware.NumCoresPerSocket}},
@{N='Cores p/s'; E={$_.Config.Hardware.NumCoresPerSocket}},
@{N='Datacenter';E={$dc.Name}},
@{N='Cluster';E={$cluster.Name}},
@{N='Host';E={$esx.Name}},
@{N='OS' ;E={$_.summary.config.guestfullname}},
@{N="VM_MemoryMB";E={$_.Config.Hardware.MemoryMB}},
@{N="Disk Path";E={$_.Guest.Disk.DiskPath}},
@{N="VM_Disk_CapacityGB";E={[math]::Round((($_.Guest.Disk | Measure-Object -Property Capacity -Sum).Sum/1GB))}},
@{N="VM_Disk_FreeSpaceGB";E={[math]::Round((($_.Guest.Disk | Measure-Object -Property FreeSpace -Sum).Sum/1GB))}},
@{N="VM_IP#";E={$_.Guest.IPAddress}},
@{N="VLAN";E={$_.Guest.Net.Network}},
@{N='VM_Annotations';E={$_.Config.Annotation}}
}
}
}
$report | Export-Csv D:\Powershell_temp\VM_info_datacenter.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
When there multiple guest OS partitions the script will have to get the sum for capacity and freespace.
Something like this
#vm_info_datacenter_test.ps1
#https://communities.vmware.com/thread/540442
#Credits LUCD@communities.vmware.com for report foundation. Minor adds by nlong
#VM data per datacenter
#Datacenter can be wildcarded ($dcName)
$dcName = 'wvhq'
$report = @()
$dc = Get-View -ViewType Datacenter -Property Name,HostFolder -Filter @{'Name'="^$($dcName)$"}
foreach($cluster in (Get-View -Id ((Get-View -Id $dc.HostFolder -Property ChildEntity).ChildEntity | where{$_.Type -eq 'ClusterComputeResource'}) -Property Name,Host)){
foreach($esx in Get-View -Id $cluster.Host -Property Name,VM){
if($esx.VM){
$report += Get-View -Id $esx.VM -Property Name,Guest,Runtime,Config.Hardware,Config.Annotation,Summary.Config |
Where{!$_.Config.Template} |
Select Name,
@{N='Powerstate'; E={$_.Runtime.Powerstate}},
@{N='CPUs'; E={$_.Config.Hardware.NumCpu}},
@{N='Sockets'; E={$_.Config.Hardware.NumCpu/$_.Config.Hardware.NumCoresPerSocket}},
@{N='Cores p/s'; E={$_.Config.Hardware.NumCoresPerSocket}},
@{N='Datacenter';E={$dc.Name}},
@{N='Cluster';E={$cluster.Name}},
@{N='Host';E={$esx.Name}},
@{N='OS' ;E={$_.summary.config.guestfullname}},
@{N="VM_MemoryMB";E={$_.Config.Hardware.MemoryMB}},
@{N="Disk Path";E={$_.Guest.Disk.DiskPath}},
@{N="VM_Disk_CapacityGB";E={[math]::Round((($_.Guest.Disk | Measure-Object -Property Capacity -Sum).Sum/1GB))}},
@{N="VM_Disk_FreeSpaceGB";E={[math]::Round((($_.Guest.Disk | Measure-Object -Property FreeSpace -Sum).Sum/1GB))}},
@{N="VM_IP#";E={$_.Guest.IPAddress}},
@{N="VLAN";E={$_.Guest.Net.Network}},
@{N='VM_Annotations';E={$_.Config.Annotation}}
}
}
}
$report | Export-Csv D:\Powershell_temp\VM_info_datacenter.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello Lucd;
Thank you for your answer. Did you have a chance to answer my annotation question? Can't figure out how to add annotations to the report.
Norm
Oops, I must have missed that part of the question.
I have updated the code above.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello Lucd;
Shucks the annotations are not appearing on the report.
Norm
Hi Lucd;
My error, the annotations are working.
Thank you for input.
Norm
Hi Lucd;
Sorry to bother you again, attempting to do the math to get the output of @{N="VM_MemoryMB";E={$_.Config.Hardware.MemoryMB}}, in GB's and have failed miserably. Would you mind showing me how to do the math for this? Planning on this being my last question.
Thanks in advance.
Norm
Replace that line with
@{N="VM_MemoryGB";E={$_.Config.Hardware.MemoryMB/1KB}},
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Lucd;
Just want to say thank you!!
Norm
You're welcome
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference