Hi guys
Hoping someone can help with a PowerCLI question?
I want to put together a PowerCLI script which will gather the CPU and Memory shares allocated to all the VM's in each Resource Pool, add them together and compare them with the resource information in the resource pool and output those that differ to a CSV file (including how far out each property differs).
The idea being that this script will quickly check across our vast estate for incorrectly configured resource pools (ie. those that have not been updated),
Anyone able to help advise on how I can get this data in PowerCLI from the resource pool and VM's?
Many thanks
-
a CraZy PeNguIn
I'm not sure if the values you suggest are correct for what is intended.
Especially the MaxCpuUsage won't tell a lot since this will normally reflect the total CPU resources available on the host (provided the guest has no limits defined).
The following should give a better picture (I assume)
$dcName = <datacenter-name> $report = @() $dc = Get-View -ViewType Datacenter -Filter @{"Name"=$dcName} Get-View -ViewType ResourcePool -SearchRoot $dc.MoRef | where{$_.Name -ne "Resources"} | %{ $rp = $_ $rpMem = 0 $rpCpu = 0 Get-View -ViewType VirtualMachine -SearchRoot $rp.MoRef | %{ $rpMem += $_.Summary.QuickStats.hostMemoryUsage $rpCpu += $_.Summary.QuickStats.overallCpuUsage } $report += New-Object PSObject -Property @{ Name = $rp.Name "VmCpuTotal (MHz)" = $rpCpu "VmMemTotal (MB)" = $rpMem RpCpu = $rp.Runtime.Cpu.UnreservedForVm RpMem = $rp.Runtime.Memory.UnreservedForVm / 1MB } } $report | Export-Csv "C:\ResPool.csv" -NoTypeInformation -UseCulture
Note that this doesn't take overcommitment into account. For that the script should use the unreservedForPool property instead of the UnreservedForVm property.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi,
This is an algorithm which might help you or give some pointers,
1) Get-VM, store it in a variable suppose in $vm.
2) By using $vm.summary.config.MemorySizeMB you can get memory size
3) By using $vm.summary.runtime.MaxCpuUsage you can get cpu usage
4) For getting information of all the VM, use array.
5) Get-Resourcepool, store it in a variable suppose in $rp.
6) By using $rp.CpuLimitMHz you can get resource pool CPU limit
7) By using $rp.MemLimitMB you can get resource pool memory limit
Hope this helps
I'm not sure if the values you suggest are correct for what is intended.
Especially the MaxCpuUsage won't tell a lot since this will normally reflect the total CPU resources available on the host (provided the guest has no limits defined).
The following should give a better picture (I assume)
$dcName = <datacenter-name> $report = @() $dc = Get-View -ViewType Datacenter -Filter @{"Name"=$dcName} Get-View -ViewType ResourcePool -SearchRoot $dc.MoRef | where{$_.Name -ne "Resources"} | %{ $rp = $_ $rpMem = 0 $rpCpu = 0 Get-View -ViewType VirtualMachine -SearchRoot $rp.MoRef | %{ $rpMem += $_.Summary.QuickStats.hostMemoryUsage $rpCpu += $_.Summary.QuickStats.overallCpuUsage } $report += New-Object PSObject -Property @{ Name = $rp.Name "VmCpuTotal (MHz)" = $rpCpu "VmMemTotal (MB)" = $rpMem RpCpu = $rp.Runtime.Cpu.UnreservedForVm RpMem = $rp.Runtime.Memory.UnreservedForVm / 1MB } } $report | Export-Csv "C:\ResPool.csv" -NoTypeInformation -UseCulture
Note that this doesn't take overcommitment into account. For that the script should use the unreservedForPool property instead of the UnreservedForVm property.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Many thanks! Really appreciated.
-
a CraZy PeNguIn
If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful
Many thanks for the information!
-
a CraZy PeNguIn
If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful
Hi LucD
When I run the scipt I get an error for the 'Property' parameter not being found (on line 12). Any ideas?
Thanks
-
a CraZy PeNguIn
If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful
You're probably not using PowerShell v2 RTM.
That -Property parameter for the New-Object cmdlet is new for PS v2.
Can you upgrade ? Otherwise I can give you a PS v1 version
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi
If you do have a PS1 version available that would be great as I have tried to update to PS2 but it still is giving me the same error for some reason.
Thanks
-
a CraZy PeNguIn
If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful
Surem here you go
$dcName = <datacenter-name> $report = @() $dc = Get-View -ViewType Datacenter -Filter @{"Name"=$dcName} Get-View -ViewType ResourcePool -SearchRoot $dc.MoRef | where{$_.Name -ne "Resources"} | % { $rp = $_ $rpMem = 0 $rpCpu = 0 Get-View -ViewType VirtualMachine -SearchRoot $rp.MoRef | % { $rpMem += $_.Summary.QuickStats.hostMemoryUsage $rpCpu += $_.Summary.QuickStats.overallCpuUsage } $row = "" | Select-Object Name, "VmCpuTotal (MHz)", "VmMemTotal (MB)", RpCpu, RpMem $row.Name = $rp.Name $row."VmCpuTotal (MHz)" = $rpCpu $row."VmMemTotal (MB)" = $rpMem $row.RpCpu = $rp.Runtime.Cpu.UnreservedForVm $row.RpMem = $rp.Runtime.Memory.UnreservedForVm / 1MB $report += $row } $report | Export-Csv "C:\ResPool.csv" -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Fantastic! Many thanks, just what I needed
-
a CraZy PeNguIn
If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful