Hi All,
I have put the following script together to list the VM's Name, Memory (limit or if unlimited actual), CPU MHz(limit or if unlimited vCPUs * 3000Hz and it's resource pool, it runs however very very slowly:-
$VM=(Get-VM -location VDS* | Select Name, NumCpu, MemoryMB)
$report = @()
foreach ($vms in $VM) {
$row = "" | Select Name, CPUMHz, Memory, resourcepool, imagetype
$row.Name = $vms.Name
$resourcearray = Get-VMResourceConfiguration -VM $vms.Name
foreach ($resource in $resourcearray){
if (($resource.MemlimitMB -eq -1) -or ($resource.MemlimitMB -ge $vms.MemoryMB)){
$row.Memory = $vms.MemoryMB}
else{
$row.Memory = $resource.MemlimitMB}
if (($resource.CPUlimitMHz -eq -1) -or ($resource.CPUlimitMHz -ge
($vms.NumCpu*3000))){
$row.CPUMHz = $vms.NumCpu*3000}
else{
$row.CPUMHz = $resource.CPUlimitMHz}
}
$resourcepoolarray=Get-ResourcePool -VM $vms.Name | Select Name
foreach ($resourcepool in $resourcepoolarray){
$row.resourcepool = $resourcepool.Name
}
if (($row.CPUMHz -cle 1024) -and ($row.Memory -cle 1024)){
$row.imagetype=69}
elseif (($row.CPUMHz -cle 2048) -and ($row.Memory -cle 2048)){
$row.imagetype=104}
else {
$row.imagetype=unknown}
$row
$report += $row
}
Does anyone have any ideas how to speed it up?
Thanks,
James
There are a few improvements that can be made that could speed up the script.
1) Instead of passing the VM's name to Get-VMResourceConfiguration and Get-ResourcePool, pass the VirtualMachineImpl object. This avoids that those 2 cmdlets have to do (internally) a Get-Vm -Name <VM-name>
2) Drop the Select on the first line. Those properties are not used anymore further on
3) Remove the 2 foreach loops. Get-VMResourceConfiguration and Get-ResourcePool return both a single object, not an array
4) Don't use a case-sensitive comparison for numbers.
Try the following version.
It ran 4 times faster in my test environment.
$vms = Get-VM -location VDS* $report = @() foreach ($vm in $vms) { $row = "" | Select Name, CPUMHz, Memory, resourcepool, imagetype $row.Name = $vm.Name $resource = Get-VMResourceConfiguration -VM $vm if (($resource.MemlimitMB -eq -1) -or ($resource.MemlimitMB -ge $vm.MemoryMB)){ $row.Memory = $vm.MemoryMB} else{ $row.Memory = $resource.MemlimitMB} if (($resource.CPUlimitMHz -eq - 1) -or ($resource.CPUlimitMHz -ge ($vm.NumCpu*3000))){ $row.CPUMHz = $vm.NumCpu*3000} else{ $row.CPUMHz = $resource.CPUlimitMHz} $resourcepool = Get-ResourcePool -VM $vm $row.resourcepool = $resourcepool.Name if (($row.CPUMHz -le 1024) -and ($row.Memory -le 1024)){ $row.imagetype=69} elseif (($row.CPUMHz -le 2048) -and ($row.Memory -le 2048)){ $row.imagetype=104} else { $row.imagetype="unknown"} $report += $row } $report
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD,
That is amazing, really appreciated,