aCrazyPenguin
Enthusiast
Enthusiast

Script to check resource pools correct

Jump to solution

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

------------------------- Andy Wood - VCP3 & 4 . MCITP:EA . MCSE:S . CCA . CCNA . Sec+ http://www.acrazypenguin.com If you find this answer useful please consider awarding points by marking the answer correct or helpful
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

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

View solution in original post

0 Kudos
9 Replies
jeveenj
Enthusiast
Enthusiast

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

-If you found this information useful, please consider awarding points for Correct or Helpful.
LucD
Leadership
Leadership

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

0 Kudos
aCrazyPenguin
Enthusiast
Enthusiast

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

------------------------- Andy Wood - VCP3 & 4 . MCITP:EA . MCSE:S . CCA . CCNA . Sec+ http://www.acrazypenguin.com If you find this answer useful please consider awarding points by marking the answer correct or helpful
0 Kudos
aCrazyPenguin
Enthusiast
Enthusiast

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

------------------------- Andy Wood - VCP3 & 4 . MCITP:EA . MCSE:S . CCA . CCNA . Sec+ http://www.acrazypenguin.com If you find this answer useful please consider awarding points by marking the answer correct or helpful
0 Kudos
aCrazyPenguin
Enthusiast
Enthusiast

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

------------------------- Andy Wood - VCP3 & 4 . MCITP:EA . MCSE:S . CCA . CCNA . Sec+ http://www.acrazypenguin.com If you find this answer useful please consider awarding points by marking the answer correct or helpful
0 Kudos
LucD
Leadership
Leadership

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

aCrazyPenguin
Enthusiast
Enthusiast

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

------------------------- Andy Wood - VCP3 & 4 . MCITP:EA . MCSE:S . CCA . CCNA . Sec+ http://www.acrazypenguin.com If you find this answer useful please consider awarding points by marking the answer correct or helpful
0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
aCrazyPenguin
Enthusiast
Enthusiast

Fantastic! Many thanks, just what I needed Smiley Happy

-


a CraZy PeNguIn

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful

------------------------- Andy Wood - VCP3 & 4 . MCITP:EA . MCSE:S . CCA . CCNA . Sec+ http://www.acrazypenguin.com If you find this answer useful please consider awarding points by marking the answer correct or helpful
0 Kudos